2012-02-02 139 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也指链接列表和其他更多的东西。