使用的c++初始化变量
C++ unitialized variable used
我一直得到"未初始化变量'y'使用"answers"未初始化变量'x'使用"。我试了很多方法,但似乎就是治不好。如有任何意见,将不胜感激。请记住,我还没有完全完成代码。我希望解决这个问题之前,我移动到无效Mulfloats(无效);谢谢,这是我的代码。
#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 Menun";
cout << "*******************************n";
cout << " H Helpn";
cout << " S Subintegern";
cout << " M MullFloatsn";
cout << " Q Quitn";
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 numbersn";
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, 4n";
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;
}
仅仅因为您在main
或getInteger
中读取x
和y
的值并不意味着它改变了具有此名称的所有变量的值。具有相同名称但在不同作用域的不同变量是完全独立的实体,因此SubIntegers
中的x
和y
不会被初始化。
这实际上是一个非常基本的误解,你应该找一本好书,从头到尾读一遍。
首先按@BaummitAugen所述初始化变量
int x = 0, y = 0;
第二,你的代码是错误的
int getInteger(void){
int x;
cin >> x;
return x;
int y; // unreachable code
cin >> y; // unreachable code
return y; // unreachable code
}
改为:
int getInteger(void)
{
int x;
cin>>x;
return x;
}
您在比较两个数字时输入错误。您声明了一个函数int getInteger(void);
但是当在函数SubIntegers()
中使用它时,您忘记了该函数有一个返回类型int,并且它的返回值需要存储在int类型的变量中。
接下来,在函数getInteger()
中,您在没有任何条件的情况下放置了2条return语句,并假设函数在接受输入后将返回两个变量。但这是函数的属性,一旦遇到返回语句,它就会结束。所以你的函数的最后3行是不可访问的,正如@Vishal所提到的。
结合这些,如果你这样编辑代码,它将工作:
void SubIntegers(void)
{
int x, y;
{
cout << "Enter two integers to compare" << endl;
x = getInteger();
y = getInteger();
displayIntegers(x, y);
}
}
int getInteger(void)
{
int x;
cin >> x;
return x;
}
相关文章:
- 初始值设定项列表是否只接受使用相同类型的值初始化变量?
- 在 C++ 中访问 lambda 捕获初始化变量
- 为什么C++不支持对未初始化变量进行智能分析?
- 使用 clang++ 和 g++ 在C++中初始化变量
- C++使用 lambda 初始化变量
- 如何在初始化列表中的构造函数之后初始化变量/对象?
- C++ - 输出与初始化变量不同?
- C++/Win32 构造函数不使用从对话框获取的字符串初始化变量
- 在C++中,为什么int可以使用new运算符初始化变量,而double不能
- 具有静态存储持续时间的常量初始化变量的初始化顺序
- C++ 中的初始化变量
- C++中未初始化变量的值
- 如何在需要提及需要循环声明的其他类的类中初始化变量?
- 我想知道在构造函数中初始化变量时的生命周期
- 读取恰好具有良好值的未初始化变量
- 如何在 getter 的父类中初始化变量的情况下访问子类中的变量
- 为什么在 c++ 中有多种初始化变量的方法
- 没有参数的默认构造函数是否总是初始化变量?
- 是否可以使用 lambda 初始化变量(删除复制 ctor 时)
- 使用构造函数跳闸UB的新放置后使用初始化变量