Infinite loop with letters....

why is my program comes to an infinite loop when a user input is not an integer?
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include<iostream>
#include<conio.h>
#include<cmath>
using namespace std;

int main()

{//start of program

int prog=0;

	do{

cout<<"\t\t\t**WORK AND POWER CALCULATOR**\n"<<endl;
cout<<"[1] Work Calculator\n"<<endl;
cout<<"[2] Power Calculator\n"<<endl;
cout<<"[3] Exit\n"<<endl;
cout<<"Select Program from 1-3: ";

cin>>prog;
cout<<endl;
		{
	if(prog>=1 && prog<=3)
	{
		switch(prog)
			{//switch starts
				{
					case 1: 
						
						float F=0, d=0, theta=0, W=0, angle=0;
						
						cout<<"\t\t\t     **WORK CALCULATOR**\n"<<endl;
						
						getch();
					
						cout<<"Use positive values...\n"<<endl;					
				
						getch();
						
						do
						{				
					

						cout<<"Enter value for F (force in N): ";
						cin>>F;
						cout<<endl;
						}while(F<=-1);
					
						do
						{
						cout<<"Enter value for d (displacement in m): ";
						cin>>d;
						cout<<endl;
						}while(d<=-1);

						do
						{
						cout<<"Enter value for theta (angle in deg.)\nnote: use 0 if there is no angle: ";
						cin>>theta;
						cout<<endl;
						}while(theta<=-1);
						
						if(theta>0)
						{
					
						angle=theta*(3.1416/180);
						
						double W=F*d*cos(angle);

						cout<<"Work is : "<<W<<" J (joule)\n"<<endl;
						getch();
						break;

						}else W=F*d;
					
						cout<<"Work is : "<<W<<" J (joule)\n"<<endl;
						getch();
					break;

				}//case 1 ends
	
				{
					case 2: 
						
						float F=0, v=0, Pw=0, Php=0;
						
						cout<<"\t\t\t     **POWER CALCULATOR**\n"<<endl;
					
						getch();
					
						cout<<"Use positive values...\n"<<endl;
						
						getch();

						do
						{
						cout<<"Enter value for F (force in N): ";
						cin>>F;				
						cout<<endl;
						}while(F<=-1);
					
						do
						{
						cout<<"Enter value for v (velocity in m/s): ";
						cin>>v;
						cout<<endl;
						}while(v<=-1);

						Pw=F*v;

						Php=Pw/745.699872;
						cout<<"Power is : "<<Pw<<" watts or "<<Php<<" hp\n"<<endl;
						getch();
					break;
				}//case 2 ends

				{	
					case 3: cout<<"\t\t   **THANK YOU FOR USING THIS CALCULATOR!**\n"<<endl;
					getch();
					return 0;

					break;
					
			
				}//case 3 ends


			}//switch ends
	
	}else cout<<"\t\t    Invalid selection!!! Please try again... \n"<<endl;
	
		}

	}while(prog!=3);


getch();
return 0;

	


}//end of program
Last edited on
1. Shouting is impolite.

2. An error when user input is not an integer?
1
2
3
4
5
6
7
  int prog=0;
  do
    {
      cin>>prog;
      if (prog>=1 && prog<=3) {}
      else cout<<"Please try again... \n"<<endl;
    } while (prog!=3);

Bad input does set the failbit for cin on line 4 and prog certainly will not be one of 1, 2, or 3.
On the following iterations the failbit is already set, so operator>> on line 4 will automatically fail.

You have to check for errors and clear them before processing more input.
sorry, i already edit my post ...


yes, the problem is when a user input is not an integer the program comes to an infinite loop.. I cant trace what the problem is..


btw thanks for the response..
Topic archived. No new replies allowed.