使用C ++单位化变量

时间:2015-10-15 02:37:45

标签: c++

我一直在使用“未初始化的变量'y'使用”和“未初始化的变量'x'。”我尝试了很多东西,似乎无法修复它。任何投入将不胜感激。请记住,我还没有完全完成代码。我想在解决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;
}

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

首先按@BaummitAugen

所述初始化变量
int getInteger(void){
int x;
cin >> x;
return x;
int y;     // unreachable code  
cin >> y;  // unreachable code 
return y;  // unreachable code 

}

inside void SubIntegers(void)

其次,你的代码错了

int getInteger(void)
{
int x;
cin>>x;
return x;
}

将其更改为:

psndrv1.f

答案 2 :(得分:0)

你正在搞砸输入来比较两个数字。你声明了一个函数int getInteger(void); 但是在函数SubIntegers()中使用它时,您忘记了该函数的返回类型为int,并且其返回值需要存储在int类型的变量中。

接下来,在函数getInteger()中,放置2个没有任何条件的return语句,并假设函数在输入后返回两个变量。但它是函数的属性,它会在遇到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;
}
相关问题