| DaPasta (34) | |||
So im planning on making a fortune teller program. I got a basic idea of it but when i compile it i get an error. "ISO C++ forbids comparison between pointer and integer."
If there is an easier way to do this please tell me. | |||
|
Last edited on
|
|||
| darkhorse (19) | |
The way you declare choice1 indicates that you are looking for a single character. However, you are then attempting to put multiple characters into a single char variable which isn't possible. Instead of declaring char choice1;include this : #include <cstring> and declare char* choice1; this way you will be able to store their answer correctly, not sure if that will fix your error, but it will fix a syntactical error that i picked up, post back if you get the same error XD | |
|
|
|
| computerquip (1842) | |
| Well, you have obvious syntax mistakes. I would suggest getting a book on beginning C++. | |
|
|
|
| DaPasta (34) | |||
Ok, now i don't get any errors but now I can't get the program to execute the else statement. It has something to do with the while statement because if i take it away, it works (with no loop though).
| |||
|
Last edited on
|
|||
| Xander314 (1381) | |
You are comparing your strings incorrectly.char* is a pointer to a character. It gets special treatment in C so we can use it as a string. However, this:choice1 == "Yes"checks if the memory address of choice1 equals the memory address of the string literal "Yes". You don't need to #include <cstring> to use character strings, only to get at functions for working with C strings. There is one such function for comparing strings, but I shan't tell you what it is because there is a better way. You are using C strings. If you #include <string>, you can use C++ strings. You declare them like this: std::string choice1;(you can, of course, omit the std:: as you've got using namespace std;).You can compare strings and string literals like this: choice1 == "Yes"and it should work correctly. | |
|
|
|
| DaPasta (34) | |
| Well, i no longer need help on the if statement. The problem there was the parentheses. Apparently i put them in the wrong places. The problem i have now is the while loop. When it asks you, "Do you like chicken nuggets:" and you answer "asddl" (for example), the while statement does not initiate and the program ends. When i take the while statement out, it works, just that there is no loop. | |
|
|
|
| Xander314 (1381) | |
|
Believe me, if 'choice1' is still a char* then comparting them like that won't work. In fact, that may well be why the while loop isn't working. Unless you have a good reason not to, use C++ strings as I suggested :) EDIT: Both Visual C++ and MinGW generate a warning message if you try to compare C strings in this way. | |
|
Last edited on
|
|
| DaPasta (34) | |||
I don't really get how you are comparing them. If you could explain how to compare the while statement (below), that would be more helpful.
| |||
|
|
|||
| DaPasta (34) | |
|
Um...I don't see any warnings. I'm using code::blocks (does that mean anything?). EDIT: Ok, so I declare choice1 like this : string choice1;So, I try the loop again and i still get the same problem. I'm thinking to post this inside the beginner section because they seem to have more people reading posts. | |
|
Last edited on
|
|
| Xander314 (1381) | |
|
If your Code::Blocks version is up to date then it could only be the warning level of your compiler, which you can change under Settings -> Compiler and Debugger. Do you know how to use a debugger? You could step through the code, looking at the values of variables at each stage and see where your program logic breaks down. | |
|
|
|