Hey yall.
This program is supposed to take a string, check it for numbers or letter, then spit them out if found.
I'm getting an error with the i < p.length part. It looks correct to me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string p;
cin >> p;
string r = "";
int i =0;
while (i < p.length()) {
char c = p[i];
if (isalnum(c))
r += c;
i++;
}}
You're not getting an error, you're getting a warning (assuming you have proper warnings enabled). (Edit: Well, there is an option to make all warnings become errors, so perhaps that's what you have, regardless, see the rest of the post.)
In function 'int main()':
13:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
length() returns a type of size_t, which is an unsigned type (used to represent indices).
If you want to remove the warning, change int i; to size_t i;
Also please use proper indentation. This example is short and sweet, but for longer code it makes it unbearable to read without proper indentation.
e.g. the code on the line under the if statement on line 15 should be indented.
The closing brace for the while loop should be indented and be on its own line.
1 2 3 4 5 6 7
int main()
{
while (condition) {
if (thing)
blah;
}
}