【问题标题】:C++ Troubleshooting my command line calculatorC++ 对我的命令行计算器进行故障排除
【发布时间】:2013-08-22 03:34:46
【问题描述】:

我正在为我的 c++ 使用 xcode。它是一个简单的命令行计算器。 这是我目前所拥有的:

//
//  main.cpp
//  test
//
//  Created by Henry Bernard Margulies on 8/21/13.
//  Copyright (c) 2013 Henry Bernard Margulies. All rights reserved.
//
#include <iostream>
#include <string>

using namespace std;

int main()
{
    bool loopy = true;  
    cout << "\nCalculator\n";       
    while (loopy == true)
    {
            bool gooy;          
            double answ;            // answer
            double fn;             // first number
            double sn;             // second number
            string opersym;     // operation symbol
            string oper;        // operation
            string more;        // rerun the program or not
            cout << endl << "Operation please (+, - , x or d): ";  //Problem1
            cin >> oper;                                        
            if (oper == "+")                    //makes sure operation is viable
            {
                gooy = true;
            }
            if (oper == "-")
            {
                gooy = true;
            }
            if (oper == "x")
            {
                gooy = true;
            }
            if (oper == "d")
            {
                gooy = true;
            }                                   //does the above
            else                
            {
                cout << endl << "Enter a real operation";       //complains if oper not viable
                gooy = false;
                continue;
            }
            if (gooy == true)                      
                cout << endl << "First number please: ";        
                if(!(cin >> fn))                                //makes sure it is a number
                {
                    cerr  << endl << "Enter a number next time, please try again"; //complaint
                    gooy = false;
                    loopy = true;
                    break;                            //Problem2
                }
                if (gooy == true)     
                {
                    cout << endl << "Next number: ";                                    
                    if(!(cin >> sn))                        
                    {
                        cerr  << endl << "Enter a number next time, please try again";
                        gooy = false;
                        loopy = true;
                        break;                  //Problem2                       
                    }
                    if (gooy == true)
                    {
                        opersym = oper;
                        if (oper == "+")
                            answ = fn + sn;
                        if (oper == "-")
                            answ = fn - sn;
                        if (oper == "x")
                            answ = fn * sn;
                        if (oper == "d")
                        {
                            opersym = "÷";
                            answ = fn / sn;
                        }
                        cout << endl << "You entered: " << fn << " " << opersym << " " << sn << ". And it equals " << answ;
                        cout << endl << "Want more? y/n: ";
                        cin >> more;
                        if (more == "n")
                        {
                            cout << endl << "Okay, I'm not wanted. Shutting down. :(";
                            return(0);
                        }
                        if (more == "y")
                        {   
                            cout << endl << "Back to work!";
                        }
                        else
                        {
                            cout << endl << "Since you can not be bothered to type it right, I'll take it as a no. :(";
                            return(0);
                        }
                    }
                }

    }
    return 0;
}

我有几个要求:

  1. 首先,似乎只有除法有效。检查 main 的第一部分,它要求操作并确认它。它不想为 +、- 或 x 工作,而只为 d 工作

2.检查名为problem2 的两个cmets。在这些部分继续;并打破;不要正确重启计算器。我想回到while循环的开头,goto据说不稳定而且不好。

3.您能更正我的代码吗?我不是专家,整个事情做得很肮脏。请向我展示更好的逻辑,以使代码更短、更快、更稳定。

谢谢! 附言。我是一个 12 岁的孩子,正在网上自学 C++,所以请放慢我的脚步,像在和一只小狗说话一样解释一些事情。

【问题讨论】:

  • goto 本身并不是不稳定或坏的。它只是最被滥用的流量控制结构之一。避免误用的方法之一是将代码分成更小的函数。您标记为“问题2”的break; 语句的问题在于它们都应该是continue;。基于operif 语句的第一部分应在第一个语句之后使用else if,直到else

标签: c++ calculator


【解决方案1】:

你的问题是if (oper == "d")之后的else如果操作不是d,else子句将激活,即使之前选择了一个操作。试试这个吧。

if (oper == "+")
{
    gooy = true;
}
else if (oper == "-")
{
    gooy = true;
}
else if (oper == "x")
{
    gooy = true;
}
else if (oper == "d")
{
    gooy = true;
}
else                
{
    cout << endl << "Enter a real operation";       //complains if oper not viable
    gooy = false;
    continue;
}

现在最后的 else 只会激活,前提是所有之前的 else 子句都被激活了。

或者

if (oper == "+" || oper == "-" || oper == "x" || oper == "d")
{
    gooy = true;
}
else                
{
    cout << endl << "Enter a real operation";       //complains if oper not viable
    gooy = false;
    continue;
}

break 退出它所在的循环。改用continue。它回到循环的顶部,如果条件为真,则重新开始。

尝试将变量声明得更接近它们的使用位置。例如 answ 和 opersym 直到循环的后期才使用。您可以在 if (gooy == true) 的 if 语句块中声明它们

【讨论】:

  • 谢谢,我明白我的第一个问题。但是对于我的第二个问题继续;无法正常工作。输出都很时髦
【解决方案2】:

首先,祝你学习 C++ 好运。我相信你很快就会学会它:)这是一个基本的计算器。它不理想但更短。

#include <iostream>
#include <string>

int main()
{
    using namespace std; //use namespace only within the scope of main()

    //ask user to choose operation
    string operation = "";//good to initialize local variable. otherwise C++ assigns them garbage values
    while(operation != "+" && operation != "-" && operation != "*" && operation != "/")
    {
        cout << "Please enter a mathematical operation. Options are: + or - or * or /" << endl;
        cin >> operation;
    }
    cout << "You entered " <<  operation << endl << endl;

    //ask user to enter two numbers
    double number1 = 0, number2 = 0, result = 0;
    bool success = false;//true if calculation carried out successfully

    //keep looping till calculation carried out successfully
    while(success!=true)
    {
        cout << "Please enter the first number: " << endl;
        cin >> number1;
        cout << "Please enter the second number: " << endl;
        cin >> number2;

        if(operation == "+") result = number1 + number2;
        else if(operation == "-") result = number1 - number2;
        else if(operation == "*") result = number1*number2;
        else if(operation == "/" && number2 != 0) result = number1/number2;
        else 
        {
            cout << "Please enter non-zero value for number2 since carrying out division" << endl;
            continue;
        }
        success = true;
        cout << "Result is: " << number1 << " " << operation << " " << number2 << " = " << result << endl;
    }
    return(0);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 2014-01-07
    • 2020-10-02
    • 2011-02-07
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多