我不斷收到「未初始化的變量的‘y’使用」和「未初始化用變量‘X’。」我已經嘗試了很多東西,而我似乎無法修復它。任何投入將不勝感激。請記住,我還沒有完全完成代碼。我正在尋找解決這個問題之前,我將繼續無效Mulfloats(void); 謝謝,這是我的代碼。C++未初始化變量使用
#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;
}
沒有這個問題在你的代碼,但應該始終* *檢查輸入是否成功:'如果(給std :: cin> > x){...}'。你的實際問題是你放棄了getInteger()的結果:在這個函數中設置的變量不會影響任何在別處命名的對象(當然,從返回後C++函數不會執行它,即使你再次調用它,所以涉及'y'的任何事都不會被執行)。 –