如何在第一次输入后立即停止C ++控制台关闭

时间:2014-12-19 23:39:27

标签: c++ visual-studio visual-studio-2013 console

我最近决定学习c ++并且它起初非常有趣我在输出后立即关闭控制台的问题可以通过添加系统(“暂停”)解决现在我有一个程序有两个输入,在第一次输入后控制台关闭而没有显示任何输出,我无法用系统修复它(“暂停”)。

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

4 个答案:

答案 0 :(得分:2)

在Visual Studio中,通过 Ctrl + F5 运行程序。

答案 1 :(得分:0)

我认为你仍然想使用System(“PAUSE”)...

尝试:

#include <cstdlib>

cout << endl << "Okay, how old are you ? ";
cin >> age;

cout << "Nice to meet you, " << name << "!" endl;
system("PAUSE");

答案 2 :(得分: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.

答案 3 :(得分:0)

您也可以在“返回0”之前在程序中使用getch()。按下某个键后系统将退出。

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
  cout << "Hello";
  getch();
  return 0;
}