Yes/No Qustions code help

I'm trying to create a funny program for my friend

This is it so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<string>
#include<stdio.h>

using namespace std;

int main(){

    char response;
    cout << "Is Vigo stupid?(Yes or no?)" << endl;
    cin >> response;

    if(response == toupper('yes'));
    cout << "You are coorect!" << endl;

    if(response == toupper('no'));
    cout << "You are wrong :DDDDDDD" << endl;

    return 0;
}


When I type yes or no, the "You are coorect!" and "You are wrong :DDDDDDD" appear at the same time, how do I make my program make the text appear when I type yes?
remove the semicolons in lines 13 and 16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<string>
#include<stdio.h>

using namespace std;

int main(){

    char response;
    cout << "Is Vigo stupid?(Yes or no?)" << endl;
    cin >> response;

    if(response == toupper('yes'));
    cout << "You are coorect!" << endl;

    else(response == toupper('no'));
    cout << "You are wrong :DDDDDDD" << endl;

    return 0;
}


You need an else statement. Should do it.
http://www.cplusplus.com/reference/cctype/toupper/

toupper() only works on a single character. Also, if it did work the way you thought it did, you'd want to test if( toupper( response ) == "YES" ).

Also, single quotes denote single characters. strings use double quotes.
Hey cory244, I get an error message saying "error: 'else' without a previous 'if'."
Last edited on
remove the ; like PCrumley said, and change your tests to something like if( toupper( response ) == 'Y' )

the 'else' suggestion will be irrelevant then.
Last edited on
EDIT: Wow a lot of people beat me to the punch I guess ^_^

The way you have it with the semi-colon after your if statements, the program does this:

reads in the first char of the response (y or n)
is the response 'y'? if so, do nothing.
Now....
print "You are coorect!" to console.
is the response 'n'? if so, do nothing.
Now....
print "You are wrong :DDDDD" to screen.

screen ends up saying:
1
2
You are coorect!"
You are wrong :DDDDD 

and exits.

Do you understand the flow of logic there?
It is more important to understand than to get the right answer.
I fixed it for you. Have fun.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<string> // why include <stdio.h> ??
using namespace std;

int main()
{

char response;
cout << "Is Vigo stupid?(Y or N?)" << endl;
cin >> response;
response = toupper(response); // make it toupper to begin with, so you know it will be upper when compared below

if (response == 'Y') // no ';' after these!
cout << "You are coorect!" << endl;

else if (response == 'N') // again, no ';'
cout << "You are wrong :DDDDDDD" << endl;
cin.get();
cout << "Press enter to continue"; // formalities
cin.get(); // pause console

return(0);	
}
Last edited on
Thanks everyone, just a quick question, why remove the ';'?
Thanks everyone, just a quick question, why remove the ';'?

You're welcome.
check my post!
Last edited on
Ohh and I included <stdio.h> because I use getchar() at the last bit of my program (before return 0;) but thanks for your awesome help and clearing things up with the logic!
Topic archived. No new replies allowed.