我通常是一名 Java 程序员,几乎专门使用 textmate,但最近我开始使用 C++。但是当我使用最基本的程序并包含 cin 关键字并运行该程序时,我没有机会在运行时输入任何内容,有时它会自己插入随机值!例如,如果我在 textmate 中运行它:
#include <iostream>
int stonetolb(int);
int main() {
using namespace std;
int stone;
cout << "enter the weight in stone";
cin >> stone;
int pounds = stonetolb(stone);
cout << stone << "stone = ";
cout << pounds <<" pounds.";
return 0;
}
int stonetolb(int sts) {
return 14 * sts;
}
我会输出:
enter the weight in stone32767stone = 458738 pounds.
为什么会发生这种情况,我该如何阻止它?
最佳答案
最有可能的是,输入语句 cin >> stone
失败,并且 stone 具有未定义的值。您需要使用 if (cin >> stone) { ... } else {//input failure }
检查输入失败。至于为什么这样一个简单的程序会表现出失败的行为,我不知道 - 你必须检查 textmate 文档。
关于c++ - 在 textmate 中使用 cin 和 cout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8203212/