【问题标题】:What is wrong with this C++ code [closed]这个 C++ 代码有什么问题[关闭]
【发布时间】:2014-05-22 23:41:22
【问题描述】:

我是编程新手,我决定尝试制作一个计算器,它可以做简单算术以外的事情。我还没有完成,我只是在测试它是否工作到目前为止。当我运行它并按 1 进行算术运算时,它就停止了。有人可以告诉我我做错了什么吗?谢谢。

#include <iostream>

using namespace std;

int main()
{
    int frsnum
    int secnum
    int arithchoice;
    int answer;
    int x;
    cout << "Welcome to the advanced calculator!" << endl;
    cout << "What are you trying to calculate: Simple Arithmetic < 1 >" << endl;
    cout << "                                  Systems of Equations < 2 >" << endl;
    cout << "                                  Matrices < 3 >" << endl;
    cin >> x;

    if(x == 1)
        {
            cout << "Add <1>|Subtract <2>|Multiply <3>|Divide <4>";
            cin << arithchoice;
        }

    if(arithchoice == 1)
        {
            cout << "Whats the first number: "
            cin >> frsnum;
            cout << "And the second number: "
            cin >> secnum;
            answer = frsnum + secnum;
            cout << "That would be: " answer << endl
        }


    system("PAUSE");
    return 0;
}

【问题讨论】:

  • 可能是cin &lt;&lt; arithchoice
  • 我注意到的第一件事是 fstnumsecnum 的声明周围缺少分号。

标签: c++ calculator


【解决方案1】:

此语句中的箭头不正确。

cin << arithchoice;

应该用这个语句代替

cin>> arithchoice;

更新

记住与 Cin 和 Cout 一起使用哪些箭头的最佳方法是,在输入值时,您从外部指向计算机。 类似地,对于 cout,您将值从计算机扔到外部世界。

所以现在如果你想将值从现实世界传递到计算机你会使用哪个箭头 >> cin 类似地,将结果从计算机提供给现实世界(用户)“

              ----------------
              |               |
Real world    | <--- computer |
              |_______________|

【讨论】:

  • 愚蠢的错误,谢谢。很抱歉在离题中发帖
  • 它发生了!...每个人都在逐渐学习。
【解决方案2】:

我注意到的第一件事是在(x==1) if 块中,cin 的箭头方向错误。

【讨论】:

    猜你喜欢
    • 2015-02-23
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 2014-08-12
    相关资源
    最近更新 更多