【问题标题】:Program Not Compiling Correctly In Ubuntu 14.04 and GCC程序在 Ubuntu 14.04 和 GCC 中无法正确编译
【发布时间】: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 &lt;&lt; "something" &lt;&lt; endl; endl 刷新输出缓冲区,我发现输出可能会丢失。我刚刚在 Codeblocks 和 OpenSuse Linux 中尝试了您的代码,它对我有用。
  • ladm@ash:~/src/test> g++ -Wall !$ g++ -Wall main.cpp ladm@ash:~/src/test> ./a.out 请输入第一个数字:2请输入第二个数字:6 你的答案是:8

标签: c++ ubuntu gcc


【解决方案1】:

听起来您正在一个环境中运行您的程序,该环境会在程序完成后关闭终端窗口。如果您使用的是 Code::Blocks,则需要采取这些步骤。

转到Project -&gt; Properties -&gt; Build targets

确保您的目标是Console project 类型并且您已检查Pause when execution ends

【讨论】:

  • 我粘贴了你的代码,它可以在 Code::Blocks 和 Linux 下运行
  • @Rob11311 奇怪的是它不适合我。我想我会重新启动我的电脑,看看它是否有效。
猜你喜欢
  • 2014-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-05
相关资源
最近更新 更多