c++ - for循环输出不显示

标签 c++ loops struct for-loop

此代码接受学生身份及其当前余额的输入。输入 -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/

相关文章:

c++ - 覆盖指向成员函数的指针

javascript - 单击按钮时如何生成一组数组而不消失第一个数组?

成员的 C 结构顺序

c++ STL设置差异

c++ - 我需要重载的最少一组运算符是什么?

c++ - 使用 C++ 中的流操纵器在固定宽度字段中居中文本

algorithm - 功能替代品?

python - 如何停止这个循环?告诉用户他们输入的最长字符串的最佳方式是什么?

c# - 在游戏循环中使用结构体与引用变量

c++ - 有序双链表