【发布时间】:2014-06-03 22:48:22
【问题描述】:
我最近使用 Ubuntu 14.04 LTS 对我的 Windows PC 进行了双启动,我使用 Code::Blocks 作为我选择的 IDE,并使用 GCC 作为我的编译器。
我是一名学习 C++ 的 Java 程序员,我编写了一个简单的加法程序,它将两个数字相加。当我运行程序时,我输入第一个数字,然后输入第二个数字,而不是告诉我我的答案,它跳过了那个,我只是按 Enter 键并结束程序。
这是我的代码:
#include <iostream>
using namespace std;
int main()
{
int thisisanumber;
int thisisanothernumber;
int outputnumber;
cout<<"Please enter first number: ";
cin>>thisisanumber;
cin.ignore();
cout<<"Please enter second number: ";
cin>>thisisanothernumber;
cin.ignore();
outputnumber = thisisanumber + thisisanothernumber;
cout<<"Your answer is: "<< outputnumber <<"\n";
}
【问题讨论】:
-
这听起来更像是代码中的错误,而不是编译不正确(或与 Ubuntu 或 GCC 有关)。
-
@DrewDormann 终端窗口保持打开状态,直到我点击返回。
-
打印“你的答案是\n”,还是什么都没有?
-
您是否尝试过使用
cout << "something" << endl;endl刷新输出缓冲区,我发现输出可能会丢失。我刚刚在 Codeblocks 和 OpenSuse Linux 中尝试了您的代码,它对我有用。 -
ladm@ash:~/src/test> g++ -Wall !$ g++ -Wall main.cpp ladm@ash:~/src/test> ./a.out 请输入第一个数字:2请输入第二个数字:6 你的答案是:8