Need help with a string

After the user enters the integers, they get a message about winning or losing. How do i keep i going ask if they want to play again. If they type yes keep going, if no stop. I am not sure why this isn't working Thanks


#include <iostream>
using namespace std;
int main()
{
bool program_continue = true;
char quit;

while (program_continue)
{
int i, j, k;
std::string exit_no("no");
std::string exit_yes("yes");
cout << "Please enter a number: ";
cin >> i;
cout << "Please enter a number: ";
cin >> j;
cout << "Please enter a number between " << i << " and " << j << ": ";
cin >> k;

if (k > i && k < j) ///if i < k < j
{
cout << "YOU WIN!" << endl;
}
else
{
cout << "YOU LOSE!" << endl;
}

cout << "Do you want to play again (yes or no)";
cin >> quit;

if(exit_no.compare(quit) != 0)
{
program_continue = false;
}
else
{
program_continue == true;
}
}

return 0;
}
1. To make your code readable put into code tags please. (See this little "<>"-button at the right side of the message composing window).

2. What does
isn't working
mean?
Found a couple of problems wrong with it

First: I switched the true and false around in the if statement. When entering yes the program would exit and when no was entered the program would continue.

Second: Changed char quit to string quit. Since you are using strings the program wasn't able to compare quit while it was a char.

Third: You didn't have #include <string> this will allow you to use strings.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string> // Need to have this when using strings
using namespace std;
int main()
{
bool program_continue = true;
string quit;  

while (program_continue)
{
int i, j, k;
std::string exit_no("no");
std::string exit_yes("yes");
cout << "Please enter a number: ";
cin >> i;
cout << "Please enter a number: ";
cin >> j;
cout << "Please enter a number between " << i << " and " << j << ": ";
cin >> k;

if (k > i && k < j) ///if i < k < j
{
cout << "YOU WIN!" << endl;
}
else
{
cout << "YOU LOSE!" << endl;
}

cout << "Do you want to play again (yes or no)";
cin >> quit;

if(exit_no.compare(quit) != 0)
{
program_continue = true;
}
else
{
program_continue = false;
}
}

return 0;
}
Thanks so much Hp of Legend. This is perfect!
Topic archived. No new replies allowed.