此代码接受学生身份及其当前余额的输入。输入 -999 作为 A 编号以打破循环,否则它将一直运行到输入 30 名学生为止。
我在程序底部的 for 循环应该以与输入相反的顺序列出输入的 A 号、学生姓名和他们的余额。虽然没有列出任何内容。只是 A-Number:、Student: 和 Balance: 的标题
我知道有一个简单的解释,但我想不出,我希望有人能为我指出...
#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;
}
最佳答案
你的第二个循环永远不会运行,因为 count
已经太小了(count == maxStudents - 1
如果循环一直运行,所以你可能会通过一次).您的循环条件需要是 count >= 0
。
关于c++ - for循环输出不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9121216/