计算器使用函数

时间:2019-11-16 05:06:45

标签: c++ visual-studio function

我是一名正在学习c ++的大学生,所以我正在使用函数制作一个简单的计算器。该程序已完成并且摆脱了错误,但是由于某种原因我遇到了2个错误。我试图修复,但仍然不知道要修复它。错误和我的代码在下面。有人可以帮助我吗?告诉我如何解决此问题?我使用Visual Studio2017。如果需要,请帮助修改我的代码

错误:

Errors 1 C4700  uninitialized local variable 'num1' Line 31
Errors 2 C4700  uninitialized local variable 'num2' Line 31 

代码:

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>

double AddNum(int num1, int num2);
double SubNum(int num1, int num2);
double MultNum(int num1, int num2);
double DiviNum(int num1, int num2);


using namespace std;

int main()
{
    int num1, num2, option;

    string MENU = "Menu \n1 = Add\n2 = Subtract\n3 = Multiply\n4 = Divide\n5 = Raise to the Power\n6 = Even or Odd\n7 Quit";// this will output a list of usable programs to use
    cout << MENU << "\n Please select your option (#1-7)" << endl;
    cin >> option;
    switch (option)
    {
    case 1:
    {
     AddNum(num1, num2);
    cout << "Please enter an integer:";
    cin >> num1;

    cout << "Please enter another integer:  ";
    cin >> num2;

    cout << "The sum of " << num1 << " and " << num2 << " is " << num1 + num2 << endl;
    break;
    }
    case 2:
    { SubNum(num1, num2);
    cout << "Please enter an integer:  ";
    cin >> num1;

    cout << "Please enter another integer:  ";
    cin >> num2;

    cout << "The difference of " << num1 << " and " << num2 << " is " << num1 - num2 << endl;
    break;
    }
    case 3:
    {
         MultNum(num1, num2);
        cout << "Please enter an integer:  ";
        cin >> num1;

        cout << "Please enter another integer:  ";
        cin >> num2;

        cout << "The product of " << num1 << " and " << num2 << " is " << num1 * num2 << endl;
        break;
    }
    case 4:
    {
        DiviNum(num1, num2);
    cout << "Please enter an integer:  ";
    cin >> num1;

    cout << "Please enter another integer:  ";
    cin >> num2;
    cout << "The quotient of " << num1 << " and " << num2 << " is " << num1 / num2 << endl;
    break;
    }


        case 5:
        {
            cout << "Please enter an integer:  ";
            cin >> num1;

            cout << "Please enter another integer:  ";
            cin >> num2;

            double x = num1;
            cout << pow(x, num2);
            cout << "The Number of" << x << " raised to the power of" << num2 << "is" << pow(x, num2) << endl;
            break;
        }

        case 6:
        {
            cout << "Please enter an integer:  ";
            cin >> num1;

            if (num1 % 2 == 0)
                cout << "The Number Is EVEN.";

            else
                cout << "The Number Is ODD";
            break;
        }

        case 7:
        {
            cout << "You are now Leaving the Calculator, GOODBYE !!! " << endl;
            break;
        }

        default:
        {cout << "Invalid Entry!\nPlease re-run the program and " << endl;
        cout << "enter a valid menu choice.\n";
        break;
        }
    }


       system("pause");
        return 0;
}

4 个答案:

答案 0 :(得分:3)

在设置值之前,请尝试使用没有值的变量。那不行,而且代码中的逻辑是错误的:

case 4:
{
    DiviNum(num1, num2);
cout << "Please enter an integer:  ";
cin >> num1;

cout << "Please enter another integer:  ";
cin >> num2;
cout << "The quotient of " << num1 << " and " << num2 << " is " << num1 / num2 << endl;
break;
}

您要先致电DiviNum,然后再要求输入号码。为什么?您需要先要求数字,然后再进行计算。您的错误消失了,代码真正起作用了。因此,将呼叫移动到所有计算中的正确位置。

您的代码实际上也不会对声明的函数执行任何操作,因为您不会保存结果并直接内联计算结果。可能不是想要的。

除非实际需要和合理的设置,否则请不要将默认值设置为其他人建议的变量,否则您将很难发现问题。

答案 1 :(得分:0)

”错误1 C4700未初始化的局部变量'num1'第31行 错误2 C4700未初始化的局部变量'num2'第31行“

编译器警告C4700:使用了未初始化的局部变量“名称”。在为其分配值之前,已使用局部变量名,即从中读取。在C和C ++中,默认情况下不初始化局部变量。未初始化的变量可以包含任何值,使用它们会导致未定义的行为。

要解决此问题,可以在声明局部变量时初始化它们,或在使用它们之前为其分配一个值。函数可以用于初始化作为参考参数传递的变量,或者当其地址作为指针参数传递时的变量。

我同意萨米·库莫宁(Sami Kuhmonen)的观点,您应该先输入“ num1”和“ num2”。然后调用运算符(AddNum,SubNum,MultNum,DiviNum)。

这是我的代码:

df_expanded_list = []
for coln in df_to_expand.columns:
    _df = df_to_expand[coln].apply(lambda x: pd.Series(eval(x), index=[coln + '_' + str(i) for i in range(len(eval(x)))]))
    df_expanded_list.append(_df)

df_expanded = pd.concat(df_expanded_list, axis=1)

答案 2 :(得分:-1)

简单的答案是编译器警告正确。声明了num1,num2和option,但未初始化。作为整数,通常将它们初始化为0。

query=""" SELECT PID, FNAMELNAMEMNAMEGENDERDOB, levenshtein(FNAMELNAMEMNAMEGENDERDOB, 'MarcHRobertM2000') as sim
    FROM table
    WHERE sim < 4 """

答案 3 :(得分:-1)

您在int num1, num2, option;中声明了这些变量,但是没有将它们初始化为任何值。只要做:

int num1 = 0, num2 = 0, option = 0;

此类原始类型的非静态局部变量不会自动初始化,必须手动进行。

相关问题