Help on fortune teller

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."
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Fortune Teller
#include <iostream>

using namespace std;

int main()
{
   //first choice
   char choice1;
   cout << "Do you like chicken nuggets: ";
   cin >> choice1;
   if(choice1 == ("Yes") || (choice1 == ("yes") || (choice1 == ("No") || (choice1 == ("No"));
   {}

   else
   {
       while (choice1 != ("Yes") || ("yes") || ("No") || ("no"));
       {
           cout << "Sorry, your answer did not seem to qualify. You must answer Yes or No. Please try again" << endl;
           cout << "Do you like chicken nuggets: ";
           cin >> choice1;
       }
   }
   return 0;
}


If there is an easier way to do this please tell me.
Last edited on
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
closed account (S6k9GNh0)
Well, you have obvious syntax mistakes. I would suggest getting a book on beginning C++.
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).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Fortune Teller
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
   //first choice
   char* choice1;
   cout << "Do you like chicken nuggets: ";
   cin >> choice1;
   if (choice1 == "Yes" || choice1 == "yes")
   {}
   else
   {
       while (choice1 != "Yes" || choice1 != "yes");
       {
           cout << "Sorry, your answer did not seem to qualify. You must answer Yes or No. Please try again" << endl;
           cout << "Do you like chicken nuggets: ";
           cin >> choice1;
       }
   }
   return 0;
}
Last edited on
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.
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.
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
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.
 
while (choice1 != "Yes" || choice1 != "yes" || choice1 != "No" || choice1 != "no");

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
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.
Topic archived. No new replies allowed.