Problem with loops and void

Greetings, this is an assignment which the purpose is too calculate an angle value in form of trigonometric functions. These are the codes that I've wrote so far.

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <cstdlib>
using namespace std;

void menu(double &value)
 {
	system("cls");
	cout<<"*****Trigonometry Program*****"<<endl;
	cout<<"Please enter an angle value => ";
	cin>>value; 
 }

void choice()
 {
 	
 	cout<<"    Type D if it is in Degree"<<endl;
	cout<<"    Type R if it is in Radian"<<endl;
	cout<<"Your response => ";
 }

void degree(bool &stopSystem2, double value)
 {
	cout<<value<<endl; //Just to test
	cout<<endl;
	stopSystem2=true;
 }

void radian(bool &stopSystem2, double value)
 {
	cout<<value<<endl; //Just to test
	cout<<endl;
	stopSystem2=true;
 }

void continueOrNot()
 {
	cout<<"Do you want to continue?"<<endl;
	cout<<"    Type Y to continue"<<endl;
	cout<<"    Type any other key to stop"<<endl;
	cout<<"Your response => ";
 }

int main()
 {
	double value;  
	char degOrRad;
	char noContinue;

	bool stopSystem1=false;
	while (!stopSystem1)
	 {
		menu(value);
		cout<<"Is the angle in Degree or Radian?"<<endl;
		
		bool stopSystem2=false;
		while(!stopSystem2)
		 {
			choice();
			cin>>degOrRad;
			degOrRad=toupper(degOrRad);

			switch (degOrRad)
			 {
				case 'D' : degree(stopSystem2, value);
				break;
				case 'R' : radian(stopSystem2, value);
				break;
				default : cout<<"Invalid response, try again!"<<endl;
				break;
			 }
		 }
		
		continueOrNot();
		cin>>noContinue;
		
		if((noContinue!='Y') && (noContinue!='y'))
		 {		
			stopSystem1=true;
		 }
		else
		 {
		 }
	
	 }
	cout<<"Thank you, goodbye!!"<<endl<<endl;
 }


I have completed the codes for the interface part. Before I proceed with the formula for the trigonometric functions, I would like to make sure the program is Error-free, which if there is accidental invalid input from the user, the program would the user to enter another input until it is a valid response.

The only problem I have encountered for this matter was in menu(value)

If I enter an integer, the program will proceed without error. However, If I enter a character, the program will slip into an endless loop which constantly shows this
1
2
3
4
5
6
7
8
9
10
*****Trigonometry Program*****
Please enter an angle value => Is the angle in Degree or Radian?
    Type D if it is in Degree
    Type R if it is in Radian
Your response=> 0 //my initial input for value

Do you want to continue?
    Type Y to continue
    Type any other key to stop
Your response =>


Can anyone tell me where is the source of the problem? I'm pretty sure it's the loop, but I don't know what to do.
input steam became corrupted after trying to assign string to double type. Because of that, another use of cin, wont work, thus degOrRad will never change and while loop will never end.

here is a little code i wrote:
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
#include <iostream>

using namespace std;

int main()
 {
	double value;

	char retry=0;

	do
	{
		retry =0;

		system("cls");
		cout << "enter value:" << endl;
		cin >> value;

		if (cin.fail()) //this is code that should interest you
		{ //read details about it on this site
			cout << "Non-numerical value entered!" <<endl;
			cin.clear();
			cin.sync();
		}

		cout << "retry? (y/n)" << endl;
		cin >> retry;
	} while (retry=='y' || retry == 'Y');

  system("pause");
  return 0;
}


experiment with it to fix your problem.

edit:
test it with number and letter as an input
Last edited on
Thank you! :D
Topic archived. No new replies allowed.