looping help

Hi, I've written the code below and everything works fine, but my question is how can I loop everything to ask the user if they wish to enter another set of packets sent instead of having to run the program over and over again?
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
//Write a program that asks for the post name (a string) and the packets per second each post sends to GC3.  
//The program should then display the PCL of the post.


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

int main()
{
	//Declarations
	string name;
	double packetsPerSecSent = 0.0;

	//Asking the user for input
	cout << "Please enter Post name: ";
	getline(cin, name);
	cout << "Please enter the number of packets per second each post sends to GC3: ";
	cin >> packetsPerSecSent;

	//Using if statements to test each condition depending on user input
	if (packetsPerSecSent >= 10000)
	{
		cout << "\nPacket Communication: Level I" << endl;
	}
	else if (5000 < packetsPerSecSent && packetsPerSecSent <= 10000)
	{
		cout << "\nPacket Communication: Level II " << endl;
	}
	else if (1000 < packetsPerSecSent && packetsPerSecSent <= 5000)
	{
		cout << "\nPacket Communication: Level III " << endl;
	}
	else if (packetsPerSecSent <= 1000)
	{
		cout << "\nPacket Communication: Level I " << endl;
	}
	//End of program
	cout << "Please press ENTER to end program.";
	cin.sync();
	getchar();
	return 0;
}
Last edited on
Use a do while loop.

1
2
3
4
5
6
7
8
char answer;
do {
     /*
      *do something here
      */
     cout << "do you want to exit? ";
     cin >> answer;
} while (answer == 'n' || answer == 'N')

Maybe do some error handling to make sure they only exit or loop when Y or N is pressed.
Something like this?

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

        bool userChoice = false;
	bool validInput = false;
	char userInput = ' ';

do
	{
		cout << "\n\nDo you want enter one more? (y/n): ";
		cin >> userInput;

		validInput = (userInput == 'y') || (userInput == 'n');

	} while (!validInput);

	userChoice = (userInput == 'y');

	return userChoice;
Topic archived. No new replies allowed.