1

I keep getting "uninitialized variable 'y' used" and "uninitialized variable 'x' used." I've tried many things, and I can't seem to fix it. any input would be greatly appreciated. Please keep in mind, I am not yet completely finished with the code. I am looking to fix this before I move on to void Mulfloats (void); Thanks, here is my code.

    #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;
}
  • Not the problem in your code but should *always* check whether input was successful: `if (std::cin >> x) { ... }`. Your actual problem is that you discard the result of `getInteger()`: the variable set inside this function doesn't affect any object named the same elsewhere (and, of course, a C++ function doesn't carry on after you return from it, even if you call it again, so anything involving `y` isn't executed ever). – Dietmar Kühl Oct 15 '15 at 02:42

3 Answers3

2

Just because you read values into x and y in your main or getInteger does not mean it changes the value of all variables with this name. Different variables with the same name but at a different scope are completely independent entities, so x and y in SubIntegers are not initialized.

This is actually a very basic misconception, you should get a good book and read it start to end.

Community
  • 1
  • 1
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
1

Firstly initialize variables as mentioned by @BaummitAugen

int x = 0, y = 0;

inside void SubIntegers(void)

Second, your code is wrong

int getInteger(void){
int x;
cin >> x;
return x;
int y;     // unreachable code  
cin >> y;  // unreachable code 
return y;  // unreachable code 

}

change it to:

int getInteger(void)
{
int x;
cin>>x;
return x;
}
vishal
  • 2,258
  • 1
  • 18
  • 27
0

You are messing up in taking input for comparing two numbers. You declared a function int getInteger(void); but while using it in the function SubIntegers(), you forgot that the function has a return type int, and its return value needs to be stored in a variable of type int.

Next, in function getInteger(), you put 2 return statements without any condition, and are assuming that the function will return both variables after taking input. But it's the property of a function that it ends as soon as a return statement is encountered. So your function's last 3 lines are unreachable, as mentioned by @Vishal.

Incorporating these, if you edit the code this way, it will work :

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;
}
Sahil Arora
  • 875
  • 2
  • 8
  • 27