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

  • Context: Comp Sci 
  • Thread starter Thread starter wahaj
  • Start date Start date
  • Tags Tags
    C++ Program
Click For Summary
SUMMARY

The discussion focuses on implementing a user prompt in a C++ program using Visual C++ 2008 Express, allowing users to repeat calculations or exit the program. The initial approach using if statements led to premature program termination, while a do-while loop effectively resolved the issue by maintaining the program's execution until the user opts to exit. The final solution involves a loop that continues to execute calculations as long as the user inputs 'y', and gracefully exits upon entering 'n' or pressing enter.

PREREQUISITES
  • Basic understanding of C++ syntax and structure
  • Familiarity with control flow statements (if, while, do-while)
  • Knowledge of user input handling using cin
  • Experience with Visual C++ 2008 Express environment
NEXT STEPS
  • Explore advanced C++ control flow techniques, focusing on loops and conditionals
  • Learn about error handling and user input validation in C++
  • Investigate the use of functions to modularize code for better readability
  • Study best practices for user interface design in console applications
USEFUL FOR

Beginner C++ programmers, students working on homework assignments, and developers looking to improve user interaction in console applications.

wahaj
Messages
154
Reaction score
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
do while has the logic you want here.

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

Similar threads

Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K