Help me please.......!


I tried several times to solve this aproblem but I can not.


The problem is:
------------------------------------------------------
Write a C++ program that reads total marks of at most 50 students from an input file called scores.txt. The total marks are whole numbers out of 100 and are sorted in the input file by increasing order of ID. The following is a sample input file.


11111 91
22222 85
33333 66
44444 75
55555 97
66666 112


Your program should:

1. Count the percentage of students in each grade category and display these as whole percentages (rounded to two digits) as shown in the sample output below. You also need to count the percentage of invalid score entries.


2. Find the grade corresponding to each student according to the following table and write the grades in reverse order of ID to an output file called grades.txt

Between 90 and 100 (inclusive) A
Between 81 and 89 (inclusive) B
Between 71 and 80 (inclusive) C
Between 60 and 70 (inclusive) D
Between 0 and 59 (inclusive) F
Less than 0 or more than 100 ERROR


The following is the output of the program corresponding to the sample input:

Grade Percentage
A 33%
B 17%
C 17%
D 17%
F 0%
Invalid 17%

Contents of output file:

ID Grade
66666 ERROR
55555 A
44444 C
33333 D
22222 B
11111 A
Have you got any code from your attempts to do this?

Edit: why on Earth was this reported?
Last edited on


this one
but it is mixed up because i was tring to add something and delete others.
until i gave up.

---------------------------------------------------------------------



#include <iostream>
#include <fstream>
#include <iomanip>


using namespace std;

int n, m;
double ID_Score[50][2];
int ID;
float Score;


int main()
{

ifstream Scores_File;
ofstream Grades_File;
Scores_File.open("scores.txt");
Grades_File.open("grades.txt");

if (!Scores_File)
{
cout<<"Sorry, the file called scores.txt does not exist"<<endl;
cout<<"Please, check it and try again\n";
}

else
{
while(Scores_File)
{
for(n=0; n<50; n++)
{
for(m=0; m<2; m++)
{
Scores_File>>ID_Score[n][m];
}
}

}

cout<<setw(8)<<"ID"<<setw(8)<<"score"<<endl;

for(n=0; n<50; n++)
{
for(m=0; m; m++)
{
cout<<setw(8)<<ID_Score[n][m];
}
cout<<endl;
}

}

Scores_File.close();
Grades_File.close();

return 0;
}
is there a problem?


sorry sir, i reported your reply by mistake.


beginner mistakes
Last edited on



it seems no answer.
When you enter code for a something you want answered please put it in the form [code]...enter code [/code] this way it highlights certain areas such as your function, classes, etc makes it easier to read. If you want to do it the lazier way just press the <> key after highlighing the code. From what I can gather from your code that you do have in the forum though is very disorganized remember for mat when writing IE

1
2
3
4
5
6
7
8
{
    cout << "Hello from main.\n";
    for ( int count = 0; count < 5; count++ )
        displayMessage(); //Call displayMessage
    cout << "Back in function main again.\n"'
    system ("PAUSED");
    return EXIT_SUCCESS;
} 


noticed that it is tabbed indicating the opening and closing of a part of your program.

also not sure why your while has for loop inside of it you might get an infinite loop unless you have some means of stopping that loop which means
1
2
3
4
5
6
7
int count = 0
while (Scores_File < 5) 
{
           for (n=0; n < 50; n++)
           count++;
}
//count keeps incementing till it reaches 4 remember 0 is counted by the compiler. 



You can do a loop in a loop but you need to make sure you have a condition that needs to be met or else you will create an infinite loop and your program will keep going creating a logic error
Last edited on

Ok,

if the number of students unknown and i need from the compiler to read all students IDs and marks. then store them in an array. how is the array declared?
Last edited on
In your post on your first one it said a max of 100 to read from the data file. What I am showing you is not going to read from the data file that he gives you I assumbe but I will show you how to set up yours the only difference is besides reading the data file is you have to change the number 5 to 100 this will give you up to 100 students
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
          const int NUM_STUDENT = 5; // change to 100 students
          int studentIDS[NUM_STUDENT];
          const int PERC_STUDENT = 5; //change to 100 students
          double percentNum[PERC_STUDENT];

// input student ID number
         cout << "Enter your student ID for \n";
         for (int student = 0; student < NUM_STUDENT; student++)
        {
                 cout << "Student #" << (student+1) << ": ";
                 cin >> studentIDS[student];
         }
//Input percentage for grade.
         cout << "\nEnter the percentage recieved for\n";
         for (int grade = 0; grade < PERC_STUDENT; grade++)
         {
                cout << "Percent #" << (grade+1) << ": ";
                cin >> percentNum[grade];
         }
         cout << endl;
         for (int count = 0; count < PERC_STUDENT; count++)
         {
                cout << setw(9) << left << " " << studentIDS[count] << " " << percentNum[count] << endl;
         }
         system("pause");
         return EXIT_SUCCESS;
}
Are you reading data from a file or writing data to a text file?

If reading you will need to read data from the file and store it to an array. The processes is straightforward, and in most cases is best done with a loop like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{          
          const int NUM_STUDENT = 5; // change to 100 students
          int studentIDS[NUM_STUDENT];
          const int PERC_STUDENT = 5; //change to 100 students
          double percentNum[PERC_STUDENT];

         // Open Data File
        datafile.open(grades.txt);
        if (!datafile)
                  cout << "Error opening data file\n"
        else
       { // Input student grades
                  for(count = 0; count < NUM_STUDENT; count++)
                          datafile >> studentIDS[count];
                  datafile.close();

//rest can be finished with what I alread gave you.

{ 

Each iteration of the loop reads an item from the file and stores it in an array element.
Topic archived. No new replies allowed.