Hello, I am trying to input an unknown amount of numbers (exam scores) and output whether they are passed, failed, or invalid scores. Passed is any score greater or equal to 70, and failed is 69 and below. Invalid scores are out of the range between 0-100.

Sample Input: 77 -45 33 92 105

Sample Output:

***Exam Report***
77 Passed
-45 Invalid Input
33 Failed
92 Passed
105 Invalid Input

here is my code that i am having trouble with:

int grades[30]

cout << "Enter grades: ";

for ( int c = 0; c < n; c++)
    cin >> grades[c];

cout << "*** Exam Report***" << endl;

     if (grade[c] >= 70)
     cout << grade[c] << setw(20) << "Passed" << endl;
    else if (grade[c] <=69)
     cout << grade[c] << setw(20) << "Failed" << endl;
     cout << grade[c] << setw(20) << "Invalid exam" << endl;

The problem is both my input and output, i cannot figure out how you are able to enter any amount of unknown numbers and then output those numbers in columns like in the sample output/input. Any tips or suggestions?

Edited by WaltP: Added CODE Tags

6 Years
Discussion Span
Last Post by WaltP

It is not necessary to use an array. There is no requirement to display the results in columns. I would save the grades in a text file then when done entering the grades read the data file and display the results.


actually, my teacher would like the output to be displayed as shown in the sample, so do i have to use an array to do so then?

Edited by xcarbonx: n/a


If you store the information in a file then display it later that will satisfy your teacher's request. With the array, you only have room for 30 grades. What will you do if someone want to enter 100 grades? There are several ways to handle that -- such as dynamically allocated arrays, linked lists, and files. But if you have not yet studies dynamic arrays or linked list then your only other option is to save the grades in a file so that they can be read back after all input is done and displayed like your teacher wants it.


I think what you have is basically fine for a start. You just have to have some way to exit your input loop -- maybe by entering a grade like -999 as the exit criteria. Keeping count of how many grades were entered makes the output loop easy.

Edited by WaltP: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.