I'm new at programming and I'm trying to make a code that checks if a number inside of a string equals to the length of a string. I don't understand why it doesn't work. Can somebody explain what's wrong with my code?
#include <iostream>
using namespace std;
int main()
{
string str;
cin >> str;
int length = str.length();
for (int i = 0; i < str.size(); i++)
{
if (('0'<str[i])&&(str[i]<='9'))
{
cout<<"the length is: "<<length<<endl;
cout<<"the num is: "<<str[i]<<endl;
cout<<"the current string is: "<<str<<endl;
if (length == str[i])
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
}
}
return 0;
}
Here is an output:
aa5aa
the length is: 5
the num is: 5
the current string is: aa5aa
no