2013-10-30 25 views
0

當我傳遞給函數的整數的函數經由可變(下面即X = 1 PrintAccntInfo(x, bank_name)它總是由不管其實際值的功能讀出0,但如果I型在值即直接PrintAccntInfo(1, bank_name)功能工作正常,有人能向我解釋這是怎麼回事感謝變量傳遞給賦予意想不到值

void Bank::PrintAccntInfo(int accntnum, Bank bank_name) { 
    int num_transactions = 0; 
    transaction_node *temp; 
    temp = bank_name.accounts[accntnum].head; 
    ....... 

accntnum是問題

編輯:?!

這裏是我的代碼調用函數f ROM(resp是從用戶讀入的字符串):

if (stoi(resp)) { 
         int resp_int = stoi(resp); 

         if (resp_int = 0) { 
          for (int i=1;i<21;i++) //print all the account transactions      
           PrintAccntInfo(i,our_bank); 
          badinputchk = false; 
         } else { 
          PrintAccntInfo(resp_int,our_bank); 
          badinputchk = false; 
         } 
    } 
+4

取代它可以顯示你設置變量和C代碼所有PrintAccntInfo? – nurettin

+2

如果你認爲這個*調用者並不重要,那麼你錯了。 – WhozCraig

+0

@nurettin好吧,我已經加了。 – Adam

回答

3

你之所以總是在功能上0是條件

if (resp_int = 0) 

resp_int爲0,計算結果爲false,所以它總是去哪裏函數被調用,resp_int裏面的「其他」 (這是0)

你應該if (resp_int == 0)

0

我認爲x的值超出了範圍。最好你可以展示如何調用PrintAccntInfo()函數和x的定義。

+0

好吧,我在代碼中添加。你介意再看一次嗎? – Adam

0

注意變量具有「範圍」。

int i = 10; 

int func(int i) { 
    if (i > 0) { 
     int i = 23 + i; 
     std::cout << "inside func, inside the if, the i here is " << i << std::endl; 
    } 
    return i; 
} 

int main() { 
    int i = 15; 
    if (i == 15) { 
     int i = func(100); 
     std::cout << "in this part of main, i is " << i << std::endl; 
    } 
    std::cout << "But in the end, the outer i is " << i << std::endl; 
}