【问题标题】:Why doesn't my calculator doesn't print out anything? [closed]为什么我的计算器不打印任何东西? [关闭]
【发布时间】:2013-07-28 01:51:37
【问题描述】:

由于某种原因,它不会打印我已经尝试过所有事情的退货声明,但我就是无法正确处理。

//calculator
#include <iostream>
using namespace std;
int input1;
int input2;

int add(int input1, int input2)
{
    cout<<"Enter two numbers to add: ";
    cin>> input1,input2;
    return  (input1 +  input2);
}
int subtract(int input1, int input2)
{
    cout<<"Enter first number to subtract: ";
    cin>> input1;
    cout<<"Enter second number to subtract: ";
    cin>> input2;
    return (input1 -  input2);
}
int multiply(int input1, int input2)
{
cout<<"Enter two numbers to multiply: ";
cin>>  input1, input2;
return (input1 * input2);
}

int main()
{
    cout<<"what do you want to do: ";
    int selection;
    cout<<"1.add\n";
    cout<<"2.subtract\n";
    cout<<"3.multiply\n";
    cin>>selection;
    if (selection ==  1) {
        return add(input1, input2);
        return input1 + input2;
    }
    else if (selection ==  2) {
        return subtract(input1, input2);
        return input1 - input2;
    }
    else if (selection ==  3) {
        return multiply( input1, input2);
        return input1 * input2;
    }
    else{
        cout<<"Error choice not available";
    }
    cin.get();
    system("pause");
}

【问题讨论】:

    标签: c++ visual-c++ calculator


    【解决方案1】:

    “由于某种原因,它不会打印我的退货声明”。

    这是因为你没有打印任何东西,你只是从main返回函数的结果。

    这是你的问题:

    if (selection ==  1) {
        return add(input1, input2);
        return input1 + input2;
        // no printing statment
    }
    else if (selection ==  2) {
        return subtract(input1, input2);
        return input1 - input2;
        // no printing statment here as well
    }
    else if (selection ==  3) {
        return multiply( input1, input2);
        return input1 * input2;
        // nither here 
    }
    

    你应该这样打印:

    if (selection ==  1) {
        cout << add(input1, input2) << endl;
    }
    else if (selection ==  2) {
        cout << subtract(input1, input2) << endl;
    }
    else if (selection ==  3) {
        cout << multiply( input1, input2) << endl;
    }
    

    您还需要像在减法函数中那样从用户那里获取输入,即更改:

    cout<<"Enter two numbers to add: ";
    cin>> input1,input2;
    

    cout<<"Enter two numbers to multiply: ";
    cin>>  input1, input2;
    

    到这里:

    cout<<"Enter first number to subtract: ";
    cin>> input1;
    cout<<"Enter second number to subtract: ";
    cin>> input2;
    

    【讨论】:

    • 你还应该添加一件事:他应该写cin&gt;&gt; input1,input2;而不是cin&gt;&gt; input1 &gt;&gt; input2;
    • 你说得对,错过了因为我测试他的代码只是为了减法
    • 非常感谢大家,它成功了。我想我会非常喜欢 c++
    • @user2626734 你好。如果它解决了您的问题,请接受答案。
    【解决方案2】:

    基本上,您看不到返回值,因为您从未将它们打印到屏幕上。 要么在你的每个例程中这样做,要么在你的 main 例程中这样做。

    您可能还想删除主程序中的所有return 语句;他们每个人都立即结束正在运行的程序。 (提示:删除 一个 是不够的。)

    【讨论】:

      【解决方案3】:

      只是为了给出一个更完整的解释——你在 main 中的 return 语句与它们在任何其他函数中所做的完全相同——返回一个值。正如 Ran Eldan 提到的,您需要使用 cout 打印到控制台,就像您要求输入一样。当你在 main 中返回一个 int 时,你实际上是将这个数字返回给操作系统,作为程序执行后检查的状态码(“0”通常是“一切顺利”的代码)。实际上,您可以使用“echo $?”在 UNIX 终端(Mac OSX 或 Linux)中检查此值。 (无引号)程序完成后。使用您当前的程序结构,您可以通过这样做获得结果,但这显然不是您想要的。

      【讨论】:

      • 在windows中你可以使用以下命令echo %ERRORLEVEL%
      【解决方案4】:

      return in main 将立即停止执行程序并将退出代码返回给操作系统。此退出代码可用于错误检查、结果检查等...但操作系统不会将其打印到控制台

      在您的代码中,它将在第一次函数调用后停止,并且永远不会到达下一个 return 语句。你会收到很多关于无法访问代码的警告。 始终启用编译器中的所有警告并阅读它们,这对帮助您解决许多错误非常有帮助

      但即使到达下一条语句,除了将数字返回给系统,您可以通过在 cmd 中运行 echo %errorlevel% 或在 bash 中运行 echo $? 来检查,什么都不会发生

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-30
        • 2014-07-03
        • 1970-01-01
        • 1970-01-01
        • 2011-05-27
        • 2011-10-27
        • 1970-01-01
        • 2018-04-01
        相关资源
        最近更新 更多