gpt4 book ai didi

使用了 C++ 未初始化的变量

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

我不断收到“使用了未初始化的变量‘y’”和“使用了未初始化的变量‘x’”。我已经尝试了很多东西,但我似乎无法修复它。任何投入将不胜感激。请记住,我还没有完全完成代码。在我继续使用 void Mulfloats (void) 之前,我希望解决这个问题;谢谢,这是我的代码。

    #include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

void help(void);
void SubIntegers(void);
int getInteger(void);
void displayIntegers(int n1, int n2);
void Mulfloats(void);
int getFloat(void);
void displayIntegers(float& n1, float& n2, float& s);
int main(void)
{
char choice;
while (1)
{
cout << "\tSelection Menu\n";
cout << "*******************************\n";
cout << " H Help\n";
cout << " S Subinteger\n";
cout << " M MullFloats\n";
cout << " Q Quit\n";
cout << " Input your choice and press Enter: ";

cin >> choice;
switch (choice)
{
case 'h':
case 'H':
help();
break;
case 's':
case 'S':
SubIntegers();
break;
case 'm':
case 'M':
float x, y;
{
cout << "Enter two valid float numbers\n";
cin >> x >> y;
cout << "x=" << x << endl;
cout << "y=" << y << endl;
cout << setw(8) << setprecision(6) << "The Product of the two float numbers is " << (x*y) << endl;
}
break;
case 'q':
case 'Q':
cout << "The program terminated per the user request...\n";
exit(0);
default:
cout << "\tNot a Valid Choice. \n";
cout << "\tValid choices are 1, 2, 3, 4\n";
cin >> choice;
}
}
return EXIT_SUCCESS; // the program returns to the first line
}
void help(void)
{
cout << "Press h or H to access Help Menu," << endl
<< "Press s or S to access Subinteger Menu," << endl
<< "Press m or M to access MullFloat Menu," << endl
<< "Press q or Q to terminate the program." << endl;
}
void SubIntegers(void)
{
int x, y;
{
cout << "Enter two integers to compare" << endl;
getInteger();
getInteger();
displayIntegers(x, y);
}
}
int getInteger(void)
{
int x;
cin >> x;
return x;
int y;
cin >> y;
return y;
}
void displayIntegers(int n1, int n2)
{

cout << "x=" << n1 << endl
<< "y=" << n2 << endl
<< "The difference of the two integers is " << (n1 - n2) << endl;
}

最佳答案

仅仅因为您在 maingetInteger 中将值读入 xy 并不意味着它会改变具有此名称的所有变量的值。具有相同名称但在不同范围的不同变量是完全独立的实体,因此 SubIntegers 中的 xy 没有被初始化。

这实际上是一个非常基本的误解,你应该得到一个 good book并从头到尾阅读。

关于使用了 C++ 未初始化的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33138883/

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