问题描述
来自 Turbo C++ 的 C++ INTRO14.CPP 示例
#include <iostream.h>
int main ()
{
int your_number;
cout << "Enter a whole number: ";
cin >> your_number;
if (your_number % 2 == 0)
cout << "\nYour number is even\n";
if (your_number % 2 != )
{
cout << "Your number is odd.\n";
}
cout << "That's all!\n";
}
我只收到消息“输入一个整数”。当我输入奇数或偶数时,程序似乎终止了。
只有在我返回 DOS shell 后,我才会收到消息“您的号码是偶数”(或奇数)。
当我从 IDE 中点击 RUN 时,为什么我只能得到部分输出?
解决方法
如评论中所述,在程序末尾使用getch()
。
现在为什么要使用它?
S简答
因为输出会在几毫秒内显示到控制台。输出已显示,但速度太快,用户无法看到。
Turbo C++ 无法控制它。
所以你必须使用 getch()
语句。
如评论中所述,有时单个 getch()
不起作用。所以我用了三遍。
它应该是这样的:-
#include <iostream.h>
int main ()
{
int your_number;
cout << "Enter a whole number: ";
cin >> your_number;
if (your_number % 2 == 0)
cout << "\nYour number is even\n";
if (your_number % 2 != )
{
cout << "Your number is odd.\n";
}
cout << "That's all!\n";
getch();
getch();
getch();
}
还有另一种选择:-
getchar()