How to make my switch statement repeat?

// I just want my program to repeat the original question after a Switch statement has been performed. I tried using a for while loop but i couldnt get it to work...
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
#include<iostream> 
#include<cmath>
#include<iomanip>
using namespace std;
void main()
 {
	 
	int num1;
	double num2;
	char select;
	cout << "Welcome to my Trigonometric Function Calculator ! " << endl;
	cout << "Please select a selection to be performed \n\n S : Sine, \n C : Cosine,\n T : Tangent, \n E : Exit \n\n ";
	cout << "Please enter your selection  ";
	cin >> select;
	switch (select)
	{
	case 'S' :
		cout << " You selected Sine function" << endl;
		cout << " Please Enter a number in degrees ";
		cin >> num1;
		num2 = (sin(num1 * 0.0174533));
		cout << " sin(" << num1 << "degrees ) = "  << fixed << setprecision(4) << num2 <<endl;
		break;
	case 'C':
		cout << " You selected Cosine function" << endl;
		cout << " Please Enter a number in degrees ";
		cin >> num1;
		num2 = (cos(num1 * 0.0174533));
		cout << " cos(" << num1 << "degrees ) = " << fixed << setprecision(4) << num2 << endl;
		break;
	case 'T':
		cout << " You selected Tangent function" << endl;
		cout << " Please Enter a number in degrees ";
		cin >> num1;
		num2 = (tan(num1 * 0.0174533));
		cout << " tan(" << num1 << "degrees ) = " << fixed << setprecision(4) << num2 << endl;
		break;
	case 'E':
		cout << " You chose to exit program. Adios!" << endl;
		break;
	default:
		cout << "You typed in an invalid number." << endl;
		break;
	}
}

Last edited on
1) Please use code tags when posting code, to make it readable:

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

2) I don't see any while loop or for loop in the code you've posted. If you don't show us the code that isn't working, how can we help you fix it?
I tried doing it on my own , but removed it so it would look alittle cleaner for others to see.
As of now i dont have any loops. but i want to implement them
To repeat the switch statement declare a bool variable, say fQuit, initialized with value false

Then run a while loop before your first cout statment:

while(!fQuit){
// your code goes in here
}
And for case 'E' set fQuit = true so that if you choose E your loop will be terminated

Edit: your main() function should return int btw
Last edited on
I've edited your code... And it's working fine on Visual Studio compiler..

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
#include<iostream> 
#include<cmath>
#include<iomanip>
#include <conio.h>

using namespace std;
void main()
 {
	 
	int num1;
	double num2;
	char select;
	cout << "Welcome to my Trigonometric Function Calculator ! " << endl;

do{		// Openning bracket of do-while loop.

	cout << "Please select a selection to be performed \n\n S : Sine, \n C : Cosine,\n T : Tangent, \n E : Exit \n\n ";
	cout << "Please enter your selection  ";
	cin >> select;
	switch (select)
	{
	case 'S' :
		cout << " You selected Sine function" << endl;
		cout << " Please Enter a number in degrees ";
		cin >> num1;
		num2 = (sin(num1 * 0.0174533));
		cout << " sin(" << num1 << "degrees ) = "  << fixed << setprecision(4) << num2 <<endl;
		break;
	case 'C':
		cout << " You selected Cosine function" << endl;
		cout << " Please Enter a number in degrees ";
		cin >> num1;
		num2 = (cos(num1 * 0.0174533));
		cout << " cos(" << num1 << "degrees ) = " << fixed << setprecision(4) << num2 << endl;
		break;
	case 'T':
		cout << " You selected Tangent function" << endl;
		cout << " Please Enter a number in degrees ";
		cin >> num1;
		num2 = (tan(num1 * 0.0174533));
		cout << " tan(" << num1 << "degrees ) = " << fixed << setprecision(4) << num2 << endl;
		break;
	case 'E':
		cout << " You chose to exit program. Adios!" << endl;
		break;
	default:
		cout << "You typed in an invalid number." << endl;
		break;
	}
	cout << endl << "Press 'e' to re-execute the program." << endl;
	cout << endl;
} while (getch()=='e');			// closing bracket of do-while loop.
}
Topic archived. No new replies allowed.