【问题标题】:Program exits after taking input输入后程序退出
【发布时间】:2018-12-04 09:43:37
【问题描述】:

我一直在学习 C++ Primer Plus,我正在使用 Visual Studio Code 解决其中的编程挑战,因为 Visual Studio 2017 存在一些奇怪的问题。我看过很多其他类似的帖子,但那里的解决方案没有不适合我。例如,我尝试在 main() 函数的右花括号处放置一个断点,在 return 0 之前将 cin.ignore() 放在末尾。但这些都不起作用。这是代码。

#include <iostream>

using namespace std;

int main() {
    int inInput;
    cout << "Enter your height in inches." << endl;
    cin >> inInput;
    int feet = inInput/12;
    int inches = inInput%12;
    cout << feet << inches << " is your height." << endl;
    cin.ignore();
    return 0;

}

编辑:

尝试了将其标记为重复的解决方案,但没有成功。

【问题讨论】:

  • 您是否在 Debug 中构建了您的程序?
  • Googling the title of your question 在第一个链接中给了我答案;)
  • 1st 我在调试中构建时没有任何反应,它只是打开首选项@MichałWalenciak
  • @FourFrame 我很难想象构建一个项目会如何打开偏好...
  • 如果你想编写控制台程序,你应该从控制台运行它们。

标签: c++ input visual-studio-code user-input


【解决方案1】:

我尝试启动您的代码,最后一行(返回前)我输入了cin.get(); 行。 之后我的窗口没有关闭。

另外,您可以再添加一行cin.ignore(); 或将现有的一行更改为cin.ignore(2),它也会对您有所帮助。 之所以关闭,是因为在这行执行之后

cin >> inInput;

\n 仍然在输入缓冲区内。所以首先cin.ignore() 只是忽略一个\n 并以自然方式关闭控制台应用程序。

附言更多等待方式:

这只是控制台应用程序的默认模式:执行完程序后关闭。

希望它会有所帮助!祝你好运!

【讨论】:

  • 它不起作用所以我添加了 2 cin.get();和一个 cin.ignore(2);但这也不起作用
猜你喜欢
  • 2023-04-10
  • 2015-04-07
  • 1970-01-01
  • 2019-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-09
  • 2018-07-23
相关资源
最近更新 更多