Need in a loop

I have the code to ask a user to enter 3 numbers then they win or lose. How do I add the code to ask them if they want to continue and if they say yes then start over asking for new integers? Thanks!

#include <iostream>
using namespace std;
int main()
{
int i,j,k;
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;
}
return 0;
}
You can create a while(1) loop so that the program repeats itself. Ask the user if he wants to continue or not, if he selects no, break the loop.
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
45
46
47
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>


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

	while (program_continue)
	{
		int i, j, k;
		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 << "Enter C for continue";
		cin >> quit;

		if (quit == 'C' || quit == 'c')
		{
			program_continue = true;
		}
		else
		{
			program_continue = false;
		}
	}

	return 0;
}
Last edited on
Thanks this is great rafae11. The only thing missing is if they don't want to continue. Can it have something like enter no if you don't want to continue? THanks!
if you type a value other than C it will stop the loop.
but if you want the user to type yes or no then you have to use a string.

change quit variable to a string and then compare the if you want a yes or no answer

for example
1
2

if (quit == 'C' || quit == 'c')


std::string exit_no("no");
std::string exit_yes("yes");

// becomes
if(exit_no.compare(quit) != 0)
{
program_continue = false;
}
[/code]
Last edited on
Thanks - i must have something wrong:


#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
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
45
46
47
48
49
50
51
52
53
54
// ConsoleApplication5.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<string>

#include <iostream>
using namespace std;
int main()
{
	bool program_continue = true;
	std::string exit_no("no");
   std::string exit_yes("yes");
	std::string quit;

	while (program_continue)
	{
		int i, j, k;
		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 continue (yes or no)";
		cin >> quit;

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

	return 0;
}
Topic archived. No new replies allowed.