0
Use getch() method which will wait(block until) for user input infinitely. Once you pressed any key this method will return the corresponding ascii code which you can ignore it. So you can utilise this method. Eg.
main()
{
.....
.....
// Your code
.....
getch(); // Which will wait for user input or in your case it will prevent your app to close.
}
0
main(){
// initialize variables
while(1)
switch(choice){
case 1:
//your code
break;
default:
return 0;
break;
}
}
0
You need to use/include conio.h header file for getch()
otherwise use cin to get input char like this
cin >> charvariable;
0
@Paul Kabira
As per your logic, his code/main pgm will execute infinitely instead for once. So it won't be good to use your logic in Styen case
0
@Venkatesh(Venki)
I can't believe someone would post an answer before reading the whole code.
My logic gives an option to run the code as many times as the developer wishes. as soon as he/she is done with the program, he needs to input any number other than '1' so it runs the default case which is 'return 0;' which stops further execution of the the code.
Try to read the whole program before judging others.
0
@Paul Kabira,
You first posted the snippet, my reply is based on your snippet. But then you posted the complete program by the time my app didn't update this page/question. I am not judging here just posting my thoughts. Also I will read througly before answering.
let's use this for improve our self instead of arguing.
Your approach will WORK but it's not efficient/advisable. Because user will not be happy to enter extra input. In this program, we are getting some input and print some text if it's greater than 4. So if you are asking to enter 1 initially itself here it's unnecessary/not useful from their perspective.
Your thoughts?
- 1
use infinite "while" loop with a "switch" condition that break s the while loop
- 1
Go to 'Project View' -> 'Code' -> click on 'Code'. Now you should see your Code.
- 1
#include <iostream>
using namespace std;
int main()
{
int antwoord,choice;
while(1)
{
cout << "\nEnter 1 to run again\n" ;
cin >> choice;
switch(choice){
case 1:
cout << "Hoeveel jaar hev je medicijnen gestudeerd" << endl;
cin >> antwoord;
if (antwoord > 4) {
cout << "Helaas je moet nog even door studeren" << endl;
}
break;
default:
return 0;
break;
}
}
return 0;
}



