I'm working on a program for my C++ class. The assignment uses a file that includes student names and three test scores.

For example:
Name
Test1
Test2
Test3
Name
Test1
Test2
Test3

My teacher wrote the main part of the program for us and we are to write the functions. We are to write a program that will read the student names and totals, sort the names and totals in decending order by totals and print the sorted list.

This is what he wrote for main (we are not supposed to change anything here):

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <conio.h>
using namespace std;

int main()
{
    string names[50];
    int totals[50], count;
    count = ReadArrays(names,totals);
    SortArrays(names,totals,count);
    PrintArrays(names,totals,count);
    getch();
    return 0;
}

We are to write the ReadArrays, SortArrays and PrintArrays functions. This is where I'm having some trouble, as I'm really struggling with this class.

First, I'm trying to write the ReadArrays function, then move to the PrintArray function and lastly, write the SortArrays function (this is what he said to do.)

He gave us a loop to use in the ReadArrays function:

Here is the loop structure you will need to read the file STUDENT.TXT. Your program will be different, though. Instead of a single string called "name", you will use an array of strings.

ifstream StudentFile;
    string name; // This sample uses a single string.
                 // You will want an array of strings instead!


    int test1, test2, test3;
        .
        .
        .
    
    // process students until end of file
    while(getline(StudentFile,name))  // you'll need an array instead of "name"!
    {
        StudentFile >> test1;  // assume that if we got a name, 3 test scores DO follow
        StudentFile >> test2;
            .
            .
            .
        StudentFile.ignore(10,'\n');  // gets the carriage return after third number
    }

I've inserted this, but am not sure what to change to make it run....This is what I have for code so far:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <conio.h>
using namespace std;

int ReadArrays(string, names[], int totals[]);



int main()
{
    string names[50];
    int totals[50], count;
    count = ReadArrays(names,totals);
    //SortArrays(names,totals,count);
    //PrintArrays(names,totals,count);
    getch();
    return 0;
}

int ReadArrays(string, names[], int totals[])
{
	ifstream StudentFile;
    names[];

	int test1, test2, test3;
    
    while(getline(StudentFile,names[]))
    {
        StudentFile >> test1;  
        StudentFile >> test2;
		StudentFile >> test3;
            
        StudentFile.ignore(10,'\n');  
	}
}

I really would appreciate some help with writing these functions...I'm trying desperately to pass this class so I can finish my degree (I'm not majoring in Computer Science, but have to take this class as part of a science credit...)

Also, the function definition line for the first function is also what he told us to do.

This article has been dead for over six months. Start a new discussion instead.