Beginner, How to loop to the beginning of the program from an IF statement.

Hey guys, I have been playing around with c++ for about 3 months now. I am not very skilled at the moment ( and certainly not on the level of most people here). I wanted to write a program that would basically calculate d=vt using IF statements ( Displacement= Velocity* Time) and all its variants. The user chooses what they want to solve for. After they choose what they want to solve for, the user inputs their values for D, T, or V depending on what they choose.
What code do I write to allow the users to have a choice between terminating the program, or continuing from the start?

#include <iostream>
using namespace std;
int main()

{
float Di;
float Ve;
float Ti;
int a;
float time;
float displacement;
float velocity;
char choice;

cout << " Hello! Welcome to my basic physics syntax program" << endl;
system ("PAUSE");
cout << " Please choose the values in which you are seeking.
1 Time 5= Displacement 10= Velocity" << endl;

cin >> a;

if ( a < 5 ) {
cout << " You have Chosen to solve for TIME\n";
system ("PAUSE");
cout << " Enter the amount of displacement\n";
system ("PAUSE");
cin >> Di;
cout << " Enter the amount of Velocity\n";
system ("PAUSE");
cin >> Ve;
time = Di/Ve;
cout << " Your time is .. In seconds " << time << endl;
system ("PAUSE");
cout << "Do you want to find other values? (y/n)" << endl;
cin >> choice;}
(choice != 'n'); // What code do I write here to give me the option to terminate the program, or reset the program to the start?


}
else if (a==5) {
cout << " You have chosen to solve for DISPLACEMENT\n";
cout << " Enter the value for Time\n";
system ("PAUSE");
cin >> Ti;
cout << " Enter the amount of Velocity\n";
system ("PAUSE");
cin >> Ve;
displacement = Ve*Ti;
cout << " Your displacement is .. In meters " << displacement << endl;
system ("PAUSE");
cout << "Do you want to find other values? (y/n)" << endl;
cin >> choice;
while(choice != 'y'); // What code do I write here to give me the option to terminate the program, or reset the program to the start?
}
else {
cout << " You have chosen to solve for VELOCITY\n";
cout << " Enter the value for Time\n";
system ("PAUSE");
cin >> Ti;
cout << " Enter the amount of Displacement\n";
system ("PAUSE");
cin >> Di;
velocity = Di/Ti;
cout << " Your velocity is .. In meters per second" << velocity << endl;
system ("PAUSE");
cout << "Do you want to find other values? (y/n)" << endl;
cin >> choice;
while(choice != 'n'); // What code do I write here to give me the option to terminate the program, or reset the program to the start?
}

return 0;
}
Last edited on
Why do you use system("PAUSE") before every input statement. Also, I would suggest using a switch statement in order to make your code more concise. Also, use a do/while loop or a while(true) loop to give the user the option to start from the beginning or to terminate the program.
I was going to put up a disclaimer stating why I have an abundance of system ("PAUSE"), but I figured someone would make a comment. For some reason on visual studio express 2012, whenever I have an input statement I need to put system ("PAUSE") for the result to be visible for more than a few moments, or seconds. When I put system ("PAUSE") it allows the user to actually read what is said on the screen. Also, could you show how to use a do/while loop in this case? please and thank you.
I agree with Group of Eight.

From what I know, a switch would be a much better choice here. Also, when posting, try posting within the "Put your code here" phrase so that we can respond to certain line numbers.

It's a hassle to try to refer to your code until it is referenced by line.
Instead of system("PAUSE"); , use _getch();. You must include "conio.h" though.

What code do I write to allow the users to have a choice between terminating the program, or continuing from the start?


Well, you could use a while loop paired with a variable as follows:
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()

{
	char keepLoop = 'Y';

	while(keepLoop == 'Y')
	{

		//*ALL CODE IN BETWEEN*

		cout << "Would you like to continue? (Y/N): "; //at the end of the code
		cin >> keepLoop;
	}
	return 0;
}

Thanks guys! I'll rewrite this simple program with the switch statements. This solved my problem.
Topic archived. No new replies allowed.