Need help with calculating student grades using arrays.
If I have the names and subsequent grades listed on notepad input file.
eg.
Name1 23 43 54 23 43 54
Name2 54 34 65 76 23 54
Name3 97 68 45 68 86 58

How do I setup two dimensional array using for loops and while loops so it would get the total of all the grades.
In addition, say the first 3 are programs and the last 3 are the test grades, how do I get it to add the first 3 to give me the program avg and last 3 to give me test avg?

I have done the following so far.. if someone can please help me on where to begin.

#include<iostream>
#include<iomanip>
#include<fstream>
#include<conio.h>

using namespace std;

ifstream fin;

int main(){
    system("color f0");
    
    fin.open("input.txt");

    if (!fin){
              cout<<"Input failure"<<endl;
              system("pause");
              return 1;
              }

string names [3][2];
int scores [3][6];
float progavg[3], testavg[3], courseavg[3], total[3];

Recommended Answers

All 12 Replies

use fin >> and since you know the order of the line (1 string, 6 numbers, 1 string, 6 numbers, etc.) make a nested for loop (rows and then columns) to put them in their proper variables.

P.S. It doesn't matter too much, but if you are going to do that color thing at least put it back to the way it was or do away with it altogether.

Build on what you know. For example here's a progression you might be able to use.

Write code to read the whole file one line at a time and display the line to the screen.

Modify the above code to read in one word at a time, whether the word is a name or a grade. Display input as you go.

If all the lines are exactly the same---name followed by x number of grades then modify input loop to read first "token" into a string and the other "tokens" in the line into x number of ints. Display input as you go.

Modify the above to place all numerical inputs into an one dimensional array, overwriting the same array with each input. Display input as you go.

Modify above program by reading grades into a 2 dimensional array of ints instead of a single array that is overwritten each time through the loop. Display the array of grades after the loop has been completed.

Do calculations on each student, each test, whatever you want to do with the information once the information is in the 2 dimensional array.

As a freebie, don't use eof() at all in the input loop.

What am I missing on the following code? I was simply trying to display all tha names and scores that's on the notepad file. But instead, I am getting garbage on the screen. (memory addresses)

cout<<left<<fixed<<showpoint<<setprecision(2);

for (int row =0; row<3; row++){
    for(int col=0; col<3; col++)
    fin>>names[row][col];
    for(int col=0; col<6; col++)
    fin>>scores[row][col];
    cout<<names[row]<<" "<<scores[row]<<endl;
    }// end of outer for 


system("pause");
return 0;
}

I meant to ask before but didn't, why is names a 2D array? I'd just make it 1D and dump that second for loop for(int col=0; col<3; col++) .

I meant to ask before but didn't, why is names a 2D array? I'd just make it 1D and dump that second for loop for(int col=0; col<3; col++) .

because of the first name and last name, it was required to be two dimensional.

string names[3][2]

It's a bit too much hard coding for me but how about

int strcnt = 0;
	for (int i=0;i<3;i++)
	{
		fin>>names[strcnt][0];
		fin>>names[strcnt][1];
		strcnt++;
                
                //inner loop etc.
      }

It's a bit too much hard coding for me but how about

int strcnt = 0;
	for (int i=0;i<3;i++)
	{
		fin>>names[strcnt][0];
		fin>>names[strcnt][1];
		strcnt++;
                
                //inner loop etc.
      }

I like the concept. Do you know from there, how I could get the names to display on the screen?
cout<<names; or cout<<names[strcnt]; seem to bring garbage.

for (int i = 0;i<3;i++)
	{	
                cout <<names[i][0]<<" "<<names[i][1]<<" ";
		for(int j = 0;j<6;j++)
		{
			cout <<scores[i][j]<<" ";
		}
		cout <<endl;
	}

You can't use << with the array, unfortunately.

Thanks. I'll give that a try!

#include<iostream>
#include<iomanip>
#include<fstream>
#include<conio.h>

using namespace std;

ifstream fin;

int main(){
    system("color f0");
    
    fin.open("input.txt");

    if (!fin){
              cout<<"Input failure"<<endl;
              system("pause");
              return 1;
              }

string names [3][2];
int scores [3][6];
float progavg[3], testavg[3], courseavg[3], total[3];

for (int row =0; row<4; row++){
    for(int col=0; col<2; col++)
    fin>>names[row][col];
    for(int col=0; col<6; col++)
    fin>>scores[row][col];
    }// end of outer for 

/* 
How do I fix the array so it will display the total and avgs for each student line by line?
Rightnow, I have variable total that displays those for only the first student.
Is there a way to display them line by line for all the students without having to create a new variable total2 ??
*/

   float total = scores[0][0]+scores[0][1]+scores[0][2]+scores[0][3]+scores[0][4]+scores[0][5];
    float pavg = (scores[0][0]+scores[0][1]+scores[0][2])/3.0;
    float tavg = (scores[0][3]+scores[0][4]+scores[0][5])/3.0;
    float cavg = pavg+cavg;
    cout<<total<<" "<<pavg<<" "<<tavg<<" "<<cavg<<endl;

   
//output the names from input file
    cout<<left<<fixed<<showpoint<<setprecision(2);
    for(int row=0; row<7; row++){
    cout<<setw(20)<<names[row][0]+" "+names[row][1];

//output the scores from input file   
    for(int col=0; col<6; col++){
    cout<<setw(5)<<scores[row][col];}
    
    cout<<endl;
       
}
   
    system("pause");
    return 0;
} //end of main

Instead of

float total = scores[0][0]+scores[0][1]+scores[0][2]+scores[0][3]+scores[0][4]+scores[0][5];
    float pavg = (scores[0][0]+scores[0][1]+scores[0][2])/3.0;
    float tavg = (scores[0][3]+scores[0][4]+scores[0][5])/3.0;
    float cavg = pavg+cavg;

try

float total[6];  or go wild and double total[6];
double pavg[6];
double tavg[6];           //I forgot if there were 3 or 6 students
double cavg[6];
for (int i = 0;i<STUDENTS;i++)   or however many students you have now)
    {
      for (int j = 0;j<5;j++)
          total[i] += scores[i][j];
    
      pavg[i] = same thing (for loop over ones you want)
     tavg [i]= same thing (for loop over ones you want)
     cavg[i] = pavg[i]+cavg[i];
}

Then for display
for (int i = 0;i<STUDENTS;i++)
      { 
       cout<<name[i][0]<<" "<<name[i][1];
       for (over scores)
               cout <<scores[i][j]<<" ";
      cout<<total<<average1[i]<<average2[i]<<average3[i]<<endl;
}

I took some liberties but you get the idea.

Thank you for all your help Jonsca. Sorry it took me so long to get back. I got really tied up with school works. I think you fixed my issue. Thanks again for taking your time. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.