Allowing the user to re run a program in c++

  • Comp Sci
  • Thread starter wahaj
  • Start date
  • Tags
    C++ Program
In summary, the person is making mini programs that involve calculations and asks the user if they want to repeat the calculations or not. They are using visual c++ 2008 express and are new to c++. They are facing a problem where the program ends even if the user enters y, and have found a solution by using a do-while loop instead of if statements. They also want the ability for the user to close the window by pressing enter.
  • #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.
 
Physics news on Phys.org
  • #2
do while has the logic you want here.

Code:
do {
  //do stuff
  //now ask for user input
  cin>> repeat;
} while (repeat=='y');
 
  • #3
ah thanks that worked
 

Related to Allowing the user to re run a program in c++

1. How do I allow the user to re-run a program in C++?

To allow the user to re-run a program in C++, you can use a loop structure such as a while or do-while loop. Within the loop, prompt the user if they want to run the program again, and if they respond with a positive answer, the loop will continue and the program will run again. If the user responds with a negative answer, the loop will end and the program will terminate.

2. Can I use recursion to allow the user to re-run a program in C++?

Yes, recursion can also be used to allow the user to re-run a program in C++. In this case, the program would call itself within the function, and each time the user responds with a positive answer to run the program again, the function will call itself again until the user responds with a negative answer.

3. How can I make sure the program runs again without losing the user's input?

To ensure that the program runs again without losing the user's input, you can use variables to store the user's input and use them in the loop or recursive function. This way, the user's input will be saved and used each time the program runs again.

4. Is it necessary to use a loop or recursion to allow the user to re-run a program in C++?

No, it is not necessary to use a loop or recursion to allow the user to re-run a program in C++. Another option is to use a goto statement, which will allow the program to jump back to a specific point in the code and run again.

5. Can I limit the number of times the user can re-run the program?

Yes, you can limit the number of times the user can re-run the program by using a counter variable in the loop or recursive function. Once the counter reaches a certain number, the loop will end and the program will terminate.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
568
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
Back
Top