2012-02-02 103 views
0

此代碼接受學生識別和他們當前餘額的輸入。 -999被輸入爲A號碼以打破循環,否則它將一直運行,直到輸入30名學生。循環輸出不顯示

我在程序底部的for循環應該按輸入的相反順序列出輸入的A編號,學生名稱和它們的平衡。儘管沒有列出任何內容。只是頭號的A - 編號:,學生:,和平衡:

我知道有一個簡單的解釋,但我只是想不到,我希望有人可以爲我指出...

#include <iostream> 
#include <string> 
#include <iomanip> 
using namespace std; 

int main(){ 
    const int maxStudents = 30; 
    struct Students{ 
     string studentName; 
     int aNumber; 
     double outstandingBalance;}; 

    Students students[maxStudents]; 

    int count = 0; 
    for(; count < maxStudents-1; count++) 
    { 
     cout<<"\nA-Number:"; 
     cin>>students[count].aNumber; 
     if(students[count].aNumber == -999) 
       break; 
      cout<<"Student Name:"; 
      cin.ignore(); 
      getline(cin,students[count].studentName); 
      cout<<"\nOutstanding Balance:"; 
      cin>>students[count].outstandingBalance; 
    } 

    cout<<setw(20)<<"\nA-Number "<<"Name "<<"Balance "; 

    for(; count >= maxStudents-1; count--) 
     cout<<setw(20)<<students[count].aNumber<<" "<<students[count].studentName<<" "<<students[count].outstandingBalance<<endl; 

    system("pause"); 
    return 0; 
} 
+3

「請*裸*與我」 - 但我甚至不知道你! – 2012-02-02 22:55:50

+0

假設你想從當前的倒計數開始迭代,只要它大於或等於零:你的計數永遠不會比最大數量的學生大。 – 2012-02-02 22:59:03

+0

iostreams通常被緩衝,所以你可能想看看刷新以確保輸出寫入時,你也期望它... – 2012-02-02 23:12:39

回答

4

由於count已經太小(count == maxStudents - 1,如果循環一直運行,所以你可能會得到一個通道),你的第二個循環永遠不會運行。您的環路條件需要爲count >= 0

+0

謝謝我知道這是簡單的,但盯着所有的代碼是現在對我來說很模糊。 – sircrisp 2012-02-02 23:17:50

0

你的for循環最後是錯誤的。使用方法:

for (int i=count; i=>0; i--) 

請仔細看看是否要按相反順序顯示第(i)條記錄。然後你的循環應該從學生人數開始。它應該運行多久?只要你的第0條記錄還沒有達到。隨着每次接班,你減少1。最後使用這個「我」(計數器)作爲您嘗試顯示的記錄號。

未來的建議是使用DISPLAY或COUT等術語代替LIST。而LIST也指鏈接列表和其他更多的東西。