Reputation: 1
I have lately decided to take up learning c++ and it's been quite fun at first I encountered the problem of the console closing immediately after an output which could be solved by adding System("pause") now i have a program that has two inputs and after the first input the console closes without showing me any output and I couldn't fix it with system("pause").
#include <iostream>
using namespace std;
int main(){
char name[50];
int age;
cout << "Please enter your name? ";
cin >> name;
cout << endl << "Okay, how old are you ? ";
cin >> age;
cout << "Nice to meet you, " << name << "!" endl;
if (age < 10) cout << "Oh, you're quite young aren't you!" << endl; else
if (age < 20) cout << "Embrace your teens they won't last long!" << endl else
if (age < 40) cout << "Ah, I see you're still in your prime" << endl else
if (age < 60) cout << "That's nice" << endl;
return 0;
}
Upvotes: 0
Views: 8158
Reputation: 33
You can also use getch() in your program before the "return 0". The system will exit after you press a key.
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
cout << "Hello";
getch();
return 0;
}
Upvotes: 0
Reputation: 300
int main () {
while (true) {
system ("CLS"); //this will clear the screen of any text from prior run
cin.clear(); //this will clear any values remain in cin from prior run
body of your program goes here
system ("PAUSE");
} // this ends while loop
return 0;
} // this ends your main function.
I hope this helps.
Upvotes: 1
Reputation: 1098
I think you still want to use System("PAUSE")...
Try:
#include <cstdlib>
cout << endl << "Okay, how old are you ? ";
cin >> age;
cout << "Nice to meet you, " << name << "!" endl;
system("PAUSE");
Upvotes: 0