c++ - 在 textmate 中使用 cin 和 cout

标签 c++ textmate

我通常是一名 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/

相关文章:

c++ - 在 “function name” 运算符后面键入参数 <T>

svn - 苹果电脑 : Setting up SVN for personal development

textmate - Ruby 1.9 哈希语法的 bundle ,或者修改现有匹配的方法?

bash - 运行 osascript -e 'tell app "TextMate 时出错“重新加载包”

c++ - 值(value)没有正确增加

c++ - 在 C++ 中计算标准差和方差

c++ - gcc 支持 set_value_at_thread_exit() 吗?

c++ - 自定义 glBlendFunc 比原生慢很多

regex - 格式化字符串的 Textmate 片段(正则表达式?)

python - 将 Python 3.1 与 TextMate 一起使用