使用的c++初始化变量

C++ unitialized variable used

本文关键字:初始化 变量 c++      更新时间:2023-10-16

我一直得到"未初始化变量'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;
}

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

这实际上是一个非常基本的误解,你应该找一本好书,从头到尾读一遍。

首先按@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;
}