How to do yes / no question?

Im very new here...can anyone shed light? VERY NEW PLEASE UNDERSTAND ;)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 //random9

#include <iostream>
#include <string>

int main()
 {
 	int yes, no;
	 using namespace std;
 	char response;
 	cout << "Do you like me?\n";
 	cin >> response;
 	if (cin >> response == 'yes')
 	 cout << "Good.\n"; 	
 }
closed account (EwCjE3v7)
Sorry why are you using a char for getting a string response, he is the fix

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 //random9

#include <iostream>
#include <string>
using namespace::std

int main()
 {
 	// what are the ints for
 	string response; // getting a string input
 	cout << "Do you like me?" << endl;
 	cin >> response; // Only take first word as input
 	if (response == "yes")  { // why the cin? And double quotes thanks Disch
 	   cout << "Good." << endl;
        }
        return 0;
 }


hope I helped
Last edited on
One other minor tweak:

if (response == "yes") { // <- double quotes for "yes"

closed account (EwCjE3v7)
ThNk you Disch, I didn't see that and have edited
This is another way you could use 'char'. Also, int is used when returning a value, so in this case, void main would be preferable.

#include <iostream>
using namespace std;
void main()
{
char response;

cout << "Do you like me? ('y' for yes and 'n' for no) \n";
cin >> response;
if (response == toupper('y'))
{
cout << "Good.\n";
}
}

//toupper is used in case the user inputs capital 'Y'
@Stephanie
You should have int as return type for the main function to tell the operating system of success or not. Main function with no return type is also non-standard.
closed account (EwCjE3v7)
Yep, return 0 is success and other is false
Topic archived. No new replies allowed.