This code accepts input for student identification and their current balances. -999 is typed as the A-Number to break the loop or it will run until 30 students have been entered.
My for loop at the bottom of the program is supposed to list the entered A-Number, Student Name, and their Balance in opposite order that it is inputted. Nothing is being listed though. Just the header of A-Number:, Student:, and Balance:
I know there is a simple explanation but I just can’t think and I’m hoping someone can point it out for me…
#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;
}
Your second loop never runs because
countis already too small (count == maxStudents - 1if the loop ran all the way through, so you might get one pass). Your loop condition needs to becount >= 0.