- #1
wahaj
- 156
- 2
Homework Statement
I am making mini programs where there some calculations going on and when all that is done my program is to ask the user if he wants to repeat the calculations or not. If he does then the program is to restart and if he doesn't then he can close the window by pressing the enter key.
I am using visual c++ 2008 express which closes the window when the program executes its last line of code. I am also very new to c++.
Homework Equations
The Attempt at a Solution
Code:
char repeat = 'y';
//repeat is the variable which stores y or n in order to determine
//whether to repeat the calculations or not
if (repeat == 'y')
{
*All the calculations code here*
}
cout<< "Would you like to repeat the calculations? ";
cin>> repeat;
if (repeat=='n')
{
cout<<"press enter to close the window.";
}
The problem is that if I do this then the program ends even if I enter y. A solution around this is to replace the if statements with while loops. If I do that however if the user enters n the program goes into an infinite loop. But the program works fine if I just get rid of
Code:
cout<< "Would you like to repeat the calculations? ";
cin>> repeat;
if (repeat=='n')
{
cout<<"press enter to close the window.";
}
I want the program to give the user the ability the close the window by pressing enter. So what do you think is happening and how can I get around that? My compiler isn't giving me any errors so its my code.