制作一个基本的 4 功能计算器,获得"error c4700: uninitialized local variable 'answ' used"
Making a basic 4 function calculator, getting "error c4700: uninitialized local variable 'answ' used"
我在制作一个基本的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语句中添加break,否则您将检查每个条件,并每次都达到默认条件。
接下来,在函数中,通过值而不是引用传递参数。这会导致函数复制您传入的变量。因此,当您在运行计算后尝试打印答案时,您正试图打印一个未初始化的变量。通过引用传递参数实际上会使用您传递的变量。
或者,如果您喜欢通过值传递,您可以将从函数返回的值分配给"answersw",但您没有这样做。
下面是一个通过引用传递的示例。
#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++ - 在我尝试制作一个简单的计算器时有一个"uninitialized local variable y used"警告
- C++ "Using Uninitialized Memory.. (variable name) "
- Visual Studio生成"Error: uninitialized local variable 'x' "而在线编译器不会 - 为什么?
- C++ Functions & Arrays - Visual Studios 中的错误 C4700
- 错误 C4700:使用未初始化的局部变量'result'
- 如何在Visual Studio 2019中修复"[member variable that is a vulkan struct] is uninitialized. Always initiali
- 如何安全地实施"Using Uninitialized Memory For Fun And Profit"?
- 错误 C4700:使用未初始化的局部变量'enemyHealth'
- 在折叠表达式中使用 lambda 时"Uninitialized captured reference"错误 - clang vs gcc
- 虽然循环错误:"Uninitialized local variable used"
- 使用 new: "potentially uninitialized pointer"将对象数组动态分配给指针
- "Uninitialized Local Variable Used"
- C /VisualStudio2017:覆盖C4700编译错误
- 为什么"Warning X4000: use of potentially uninitialized variable"显示多个常用方法的用法?
- 析构函数提供删除资源的"may be used uninitialized"
- 为什么我会收到来自 g++ 'uninitialized'警告
- 叮叮当当 警告:"warning: unknown warning option '-Wno-maybe-uninitialized'"
- 可视C++编程错误:C4703、C4700
- 如何在发布模式下使'C4700: uninitialized local variable'成为错误
- 制作一个基本的 4 功能计算器,获得"error c4700: uninitialized local variable 'answ' used"