Results 1 to 5 of 5
I'm trying to write a program that repeatedly asks for characters until "$" is entered. The program then outputs the number of characters entered before the $ was entered.
So ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 09-06-2011 #1Just Joined!
- Join Date
- Aug 2011
- Posts
- 38
Why isn't this working (C++)?
I'm trying to write a program that repeatedly asks for characters until "$" is entered. The program then outputs the number of characters entered before the $ was entered.
So I did this:
#include <iostream>
using namespace std;
int main() {
int b;
char a;
b = 0;
do{ cout << "Enter character: ";
cin >> a;
b++;
} while ( a != "$" );
cout << b << "\n";
return 0;
}
I called this "dollar.cpp".
Unfortunately, the program doesn't compile. I get this as an error:
dollar.cpp:23:20: warning: comparison with string literal results in unspecified behaviour
dollar.cpp:23:20: error: ISO C++ forbids comparison between pointer and integer
What's wrong here?
- 09-06-2011 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 10,160
Try single quotes around the dollar sign in the while() condition. IE:
This is a pretty common sort of mistake for beginning programmers.Code:} while (a != '$');
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 09-07-2011 #3Just Joined!
- Join Date
- Aug 2011
- Posts
- 38
Thanks, it works!!
Can I just ask, what is the difference between " and '?
When i'm using cout, i'm using " so what's the general rule for using one or the other?
- 09-07-2011 #4
Single quotes are for a character and double quotes are for a string, which more accurately would be a pointer to a string.
If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
My new blog. It's probably not as good as I think it is.
The Fifth Continent reborn
- 09-07-2011 #5Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 10,160
What elija said. This may clarify the "rule":
Are we confused yet?Code:char stuff[] = { 'a', 'b', 'c', 0 }; // an initialized string/char array variable. char a = stuff[0]; char b = stuff[1]; char c = stuff[2]; char* astring = stuff; // Or, char* astring = &stuff[0]; - same thing. if (a == 'a') printf("ok, a == 'a'\n"); if (b != 'b') printf("not ok - b != 'b' should not compute since b (stuff[1]) == 'b'\n"); if (c == "c") printf("not ok - c is not a string, it's a char\n"); // This would cause your compiler error. if (astring == stuff) printf("ok - astring points to stuff\n"); if (astring == "abc") printf("not ok - astring (alias stuff) has same data, but not same address as \"abc\" string literal\n"); if (strcmp(astring, "abc") == 0) printf("ok - astring contains same data as \"abc\" string literal\n");
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote

