0

how to stop infinite loop in this program

int main() { clrscr(); int x,y,ans,guess; srand ( time(NULL) ); x = rand() %10; y = rand() %10; cout<<" How much is "<<x<<" times "<<y<<" ?(type E to END): "; cin>>guess; ans=x*y; while (ans==guess) { { cout<<"Very Good!\n"; srand ( time(NULL) ); x = rand() %10; y = rand() %10; cout<<" How much is "<<x<<" times "<<y<<" ?(type 111 to END): "; cin>>guess; ans=x*y; } while (ans!=guess) { cout<<"No, Please try again.\n"; cout<<" How much is "<<x<<" times "<<y<<" ?(type 111 to END

22nd Oct 2016, 5:17 AM
KG Feignh
KG Feignh - avatar
1 Answer
0
The code is not whole, so I did not test if the algorytmus is OK - and the loop would be ended alone for some number of cycles. But you write here : "type 111 to END", so you have to check in code if the value 111 was entered:. after line cin>>guess; add next line if (guess==111) break; //breaks loop, but program continues next command or this if (guess==111) return; // it leaves the function main, so program is ended Btw not (type E to END), because int variable takes only number values.
22nd Oct 2016, 5:51 AM
Petr Hatina
Petr Hatina - avatar