0

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;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • I would recommend you use eclipse instead of micrsoft vs 2013 since eclipse doesn't close after this. Instead, it has a console at the bottom where stuff doesn't disappear after the program finishes. – bobtheboy Dec 19 '14 at 23:43
  • Please note your code doesn't compile (missing ';') its important posting compile-able code so the readers could run it – Shmil The Cat Dec 19 '14 at 23:45
  • Thanks for the reply I had tried eclipse but i got errors with cin, cout and std saying that they could not be resolved – Maple Syrup Facial Dec 19 '14 at 23:49
  • @MapleSyrupFacial Try using eclipse again. I have used both of them and found eclipse more accommodating and less of those small little "mistakes" I have found with microsoft vs. Eclipse doesn't have any bad things such as this that I have found out. – bobtheboy Dec 20 '14 at 00:17
  • Okay but when i use eclipse it tells me that cin, cout and std do not work and can not be resolved if you can tell me how to fix that i would probably use the program. – Maple Syrup Facial Dec 20 '14 at 00:31

4 Answers4

2

In Visual Studio, run your program via Ctrl+F5.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

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");
DigitalNinja
  • 1,078
  • 9
  • 17
  • 1
    There is no advantage to `system("PAUSE")` (or equivalent), and a number of problems. The problems include portability, inconvenient command line usage and obstruction of automation. – Cheers and hth. - Alf Dec 20 '14 at 00:15
0
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.
cryptotrader
  • 180
  • 1
  • 4
  • 13
0

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;
}