How To Ask User To Repeat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
using namespace std;
int main()
{
int number;
int counter=1;
int fact=1;
cout<<"enter number \n";
cin>>number;

while(counter<=number)
{
fact*=counter;
counter++;
}
cout<<fact<<endl;

}[code]
Last edited on
Hello mahmoud adel,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



What part would you like the user to repeat?

Andy
I'm click on formatting button then click submit but i don't notice any changes..
I want to ask the user.. cout<<"do you want again (y) yes or (n) no";
And if he click y the program will start again
Hello mahmoud adel,

You can try something like this:
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
55
#include <cctype>
#include <iostream>
#include <limits>

//using namespace std;  // <--- Best not to use.
// The most recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/

int main()
{
	//  A better Choice
	using std::cin;
	using std::cout;
	using std::endl;

	bool cont{ true };
	char choice{};
	int number{}; // <--- Not needed to be initialized, but a good idea.
	int counter{ 1 };
	unsigned long long fact{ 1 };
	
	do
	{
		cout << "\n Enter number (1 to 65): ";
		cin >> number;

		std::cout << '\n';

		while (counter <= number)
		{
			fact *= counter;
			counter++;
		}

		cout << "\n The answer is: " << fact << endl;

		std::cout << "\n  Check another number?: ";
		std::cin >> choice;

		if (std::toupper(choice) == 'Y')
		{
			counter = 1;  // <--- Reset these variables.
			fact = 1;
		}
		else
			cont = false;
	} while (cont);

	// A fair C++ replacement for "system("pause")". Or a way to pause the program.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;  // <--- Not required, but makes a good break point.
}

Notice that I changed the type for "fact". It does not take much to exceed the maximum number that an "int" can hold.

The other comments should explain the rest.

See what you think.

Andy
THANKS BROOOOO
Topic archived. No new replies allowed.