2015-10-15 412 views
1

我不斷收到「未初始化的變量的‘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; 
} 
+0

沒有這個問題在你的代碼,但應該始終* *檢查輸入是否成功:'如果(給std :: cin> > x){...}'。你的實際問題是你放棄了getInteger()的結果:在這個函數中設置的變量不會影響任何在別處命名的對象(當然,從返回後C++函數不會執行它,即使你再次調用它,所以涉及'y'的任何事都不會被執行)。 –

回答

0

你正在搞比較兩個數字的輸入。你聲明的函數int getInteger(void); 但同時在功能SubIntegers()使用它,你忘了該函數有一個返回類型爲int,並且需要被存儲在一個int類型的變量它的返回值。

接下來,在功能getInteger(),你把2條return語句不帶任何條件,並假設該函數將採取輸入後返回這兩個變量。但是,只要遇到return語句,它就是函數的屬性。所以你的函數的最後3行是無法訪問的,就像@Vishal所提到的那樣。

結合這些,如果你編輯代碼這種方式,它會工作:

+0

謝謝,這正是我需要做的。我非常感謝幫助。 – JaysonJones1290

2

僅僅因爲你看在你maingetInteger值到xy並不意味着它改變了這個名字的所有變量的值。不同的變量具有相同的名稱,但在不同的範圍內是完全獨立的實體,所以xySubIntegers不被初始化。

這其實是一個很基本的誤解,你應該得到一個good book閱讀它開始結束。由@BaummitAugen

提到
int x = 0, y = 0; 

無效SubIntegers內(無效)

1

首先初始化變量,你的代碼是錯誤的

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; 
} 
+0

謝謝你的幫助。這真的在我的代碼中發揮了作用。 – JaysonJones1290