【发布时间】:2015-04-29 11:25:17
【问题描述】:
所以我正在使用 MVS,我编写了以下程序
// istream::ignore example
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
char first, middle, last;
std::cout << "Please, enter your first name and your surname: ";
first = cin.get(); // get one character
cin.ignore(256, ' '); // ignore until space
middle = cin.get(); //get one character
cin.ignore(256, ' '); //ignore until space
last = std::cin.get(); // get one character
std::cout << "Your initials are " << first << middle << last << endl;
cin.get();
return 0;
}
程序按应有的方式运行,但一执行就立即关闭。有人可以解释一下是什么让程序这样做,以及我该如何解决它?
【问题讨论】:
-
你的意思是它在你提供输入之前就结束了吗?你能描述一下你是如何运行它的吗?你提供了什么输入(如果有的话)?
-
“MVS”是 Visual Studio 的首字母缩写词,还是别的什么?
-
只要我提供输入,程序就会显示正确的结果,然后立即终止。
-
这是正常的。当程序结束时,操作系统不会保留它。如果您按 ctrl-f5,Visual Studio GUI 将提示您关闭窗口。或者在最后一条语句上放一个断点。或者从 cmd.exe 窗口运行它。我预计这个问题有 1000 个重复项。
标签: c++