Issues with if else statement

Jan 27, 2018 at 5:03am
I am having issues with the code I am currently writing. I am trying to get and if else statement to work with the word True, and then if true count up a value. I'll post what I have so far, I took a break because I was frustrated. I just changed True, False, and answer to string, but previously tried char, and bool, but nothing has worked. I also cannot find info, because i just keep getting basic if true blah if false blah info on google. Any help would be appreciated. Thanks! Oh also the project is supposed to be a guessing game where they guess true or false otherwise I would have just used 1 and 2 or something.

#include <iostream>
#include <iomanip>

using namespace std;


int main ()
{
int x;
int y;
int answer;
string True;
string False;
string Answer;

x=0;
y=0;

cout<< "Would you like to play a game? Type 1 to play, or 2 to terminate."<< endl;

cin >> answer;

if (answer == 1)
{printf("Excellent.\n");}
else {printf("Bummer.\n"); return 0;}

cout << "Evaluate the folowing proverbs. Answer True if the proverb is correct, or False if the proverb is incorrect then hit enter. At the end of the game we will rate your performance.\n" << endl;

cout << "True or False: The squeaky wheel gets the grease.\n" << endl;
cin >> Answer;

if (Answer = "True")
{y = x++;}
else {y= x+0;}
cout << "Your final score is: " << y << endl;

return 0;
}
Last edited on Jan 27, 2018 at 5:04am
Jan 27, 2018 at 5:05am
Change if (Answer = "True") to if (Answer == "True")

Get ride of the first return 0; because it'll end main.
Last edited on Jan 27, 2018 at 5:08am
Jan 27, 2018 at 5:10am
I just did that, but it still throws me a 0 for the value of y. Did i do the cout statement wrong? Should i use printf(y);
Jan 27, 2018 at 5:14am
You're using post-increment which will set the value of y to be x before increment. Use pre-increment.
Jan 27, 2018 at 5:16am
so the first return 0 is if they type 2, which works, and the program continues if you type 1. Can you give me an example of pre-increment? I am taking a C++ class, and we haven't used the counting up stuff yet.
Jan 27, 2018 at 5:18am
nvm found it in my text book
Jan 27, 2018 at 5:20am
1
2
++value; // Pre-increment
value++; // Post-increment 


You will need to nest your second user input block into your first if statement so that it'll ask the user if the answer is "1"
Jan 27, 2018 at 5:20am
Cool it now works correctly. Am I right to assume, that I could accomplish this only with x and whenever the question is answered correctly x= ++X? so on and so on?
Jan 27, 2018 at 5:23am
You don't need to use another variable to set the integer.

1
2
3
4
5
6
7
8
9
10
11
// Example program
#include <iostream>

int main()
{
    int x = 0;
    int y = 0;
    std::cout << ++x << '\n';
    std::cout << y++ << '\n';
}
Topic archived. No new replies allowed.