gpt4 book ai didi

c++ - 制作基本的四功能计算器,使用 “error c4700: uninitialized local variable ' answ”

转载 作者:行者123 更新时间:2023-12-02 11:06:54 25 4
gpt4 key购买 nike

我在尝试制作基本的4功能计算器时遇到此错误。我对C++相当陌生,因此可能会有更多错误,但是我主要希望专注于错误,因为我无法对其进行编译。感谢您的帮助!

#include <iostream>
using namespace std;

float fadd(float num1, float num2, float answ);
float fsub(float num1, float num2, float answ);
float fmul(float num1, float num2, float answ);
float fdiv(float num1, float num2, float answ);
char contOption();

int main()
{
float answ, num1, num2;
char oper, cont;
cout << "Student name: Jose Gomez" << endl;
cout << "Student number: 900724015" << endl << endl << endl;
do
{
cout << "Please enter first number, operator & second number: ";
cin >> num1 >> oper >> num2;
switch (oper)
{
case '+':
fadd(num1, num2, answ); //the 'answ' here is what is giving me the
//error and i do not know how to fix it
cout << endl << "Answer = " << answ;
case '-':
fsub(num1, num2, answ);
cout << endl << "Answer = " << answ;
case '*':
fmul(num1, num2, answ);
cout << endl << "Answer = " << answ;
case '/':
fdiv(num1, num2, answ);
cout << endl << "Answer = " << answ;
default:
cout << "Sorry, illegal operation. Only '+', '-', '*', '/' are allowed" << endl << endl;
cout << "Please enter first number, operator & second number: ";
cin >> num1 >> oper >> num2;
}
cont = contOption();

} while (cont == 'y' || cont == 'Y');


cin.ignore();
cin.get();
return 0;
}

float fadd(float num1, float num2, float answ)
{
answ = num1 + num2;
return answ;
}

float fsub(float num1, float num2, float answ)
{
answ = num1 - num2;
return answ;
}

float fmul(float num1, float num2, float answ)
{
answ = num1*num2;
return answ;
}

float fdiv(float num1, float num2, float answ)
{
answ = num1 / num2;
if (num2 == 0)
cout << "Sorry, divide by 0 is an illegal operation";
else if (num2 != 0)
return answ;

}

char contOption()
{
char cont;
cout << endl << "Would you like to perform another calculation? (y / n): ";
cin >> cont;
return cont;

}

最佳答案

首先,您需要初始化变量:

float answ, num1, num2;
answ = num1 = num2 = 0.0;

否则,它们是不确定的。其次,您应该在switch语句中添加中断,否则您将检查每个条件并每次都达到默认条件。

接下来,在函数中,您是通过值而不是引用来传递参数。这会使您的函数复制您传入的变量。因此,在运行计算后尝试打印答案时,您将尝试打印未初始化的变量。通过引用传递参数实际上将使用您传递的变量。

另外,如果您希望按值传递,则可以为“answ”分配从函数返回的值,但您不这样做。

这是一个通过引用传递的示例。
#include <iostream>
using namespace std;

void fadd(float& num1, float& num2, float& answ);
void fsub(float& num1, float& num2, float& answ);
void fmul(float& num1, float& num2, float& answ);
void fdiv(float& num1, float& num2, float& answ);
char contOption();

int main()
{
float answ, num1, num2;
answ = num1 = num2 = 0.0;
char oper, cont;
cout << "Student name: Jose Gomez" << endl;
cout << "Student number: 900724015" << endl << endl << endl;
do
{
cout << "Please enter first number, operator & second number: ";
cin >> num1 >> oper >> num2;
switch (oper)
{
case '+':
fadd(num1, num2, answ); //the 'answ' here is what is giving me the
//error and i do not know how to fix it
cout << endl << "Answer = " << answ;
break;
case '-':
fsub(num1, num2, answ);
cout << endl << "Answer = " << answ;
break;
case '*':
fmul(num1, num2, answ);
cout << endl << "Answer = " << answ;
break;
case '/':
fdiv(num1, num2, answ);
cout << endl << "Answer = " << answ;
break;
default:
cout << "Sorry, illegal operation. Only '+', '-', '*', '/' are allowed" << endl << endl;

cout << "Please enter first number, operator & second number: ";
cin >> num1 >> oper >> num2;
}

cont = contOption();

} while (cont == 'y' || cont == 'Y');


cin.ignore();
cin.get();
return 0;
}

void fadd(float& num1, float& num2, float& answ)
{
answ = num1 + num2;
}

void fsub(float& num1, float& num2, float& answ)
{
answ = num1 - num2;
}

void fmul(float& num1, float& num2, float& answ)
{
answ = num1*num2;
}

void fdiv(float& num1, float& num2, float& answ)
{
answ = num1 / num2;
if (num2 == 0)
cout << "Sorry, divide by 0 is an illegal operation";
}

char contOption()
{
char cont;
cout << endl << "Would you like to perform another calculation? (y / n): ";
cin >> cont;
return cont;
}

关于c++ - 制作基本的四功能计算器,使用 “error c4700: uninitialized local variable ' answ”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33535465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com