在textmate中使用cin和cout

using cin and cout in textmate

本文关键字:cin cout textmate      更新时间:2023-10-16

我通常是一个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;
}

我将输出:

输入stone32767stone = 458738磅的重量。

为什么会发生这种情况,我该如何阻止它?

很可能是输入语句cin >> stone失败,stone的值未定义。需要使用if (cin >> stone) { ... } else { // input failure }检查输入是否失败。至于为什么这样一个简单的程序会表现出失败的行为,我不知道—您必须检查textmate文档。