Hey everyone,

I need to write a program that reads names and GPA’s from a text file. The file looks like:

James 3.9
Margaret 3.5
Charles 1.2
Jennifer 4.0

The program sorts the students ascending by gpa, and prints the sorted list including names. To keep the names with the numbers use parallel arrays, one for the names, the other for the numbers. Sort the numbers. Whenever you swap 2 numbers also swap their matching names in the names array.

The only thing that I've done so far is to open the text file and created a function called:
gpa_sorter(istream& in,string student[200],double studentGPA[200])

I don't know how to even write the pseudo code after while(!in.eof()) because im confused about sorting the data from in the file into 2 separate arrays, one that is double and one that is string.

Also, I'm not allowed to use vectors yet, and if you could please describe it to me as simple as possible in pseudo code, that would be great because i'm only in my first year.

Thanks again.

well if you have the read the data in from the file. store the names into an array of a string and the gpa into an array of doubles. I'm not exactly how you are supposed to be sorting the information. the trick is that when you are sorting one of the arrays you need to do the swap in the other array. lets say you have

string names[20];
double gpa[20];

when you swap gpa[2] and gpa[5] you also need to swap names[2] and names[5].

my first problem I need to resolve is to actually put the correct data into both arrays. How do i store the names in one, and the numbers in the other array if their both in the same file?

If the file is guaranteed to be in the form that you have above, you can do something simple like:

std::string names[n];
double grade[n];
std::ifstream infile("filename.txt", std::ios::in);
unsigned i = 0;
while(infile.fail() == false && i < n){
    infile >> names[i] >> grade[i];
    ++i;
}

where n is the maximum number of students that you expect. Of course, there are better, more advanced ways of doing this that are more robust, but this will also work.

Hey ravenous,

I have a question about:
infile >> names >> grade ;

Will this go through the first line of the file and put the name in names[] and the gpa in grade[], then repeat with i++?

After this part, I need to order the array so that it is in ascending order, meaning that the lowest gpa goes first with the correct name, followed by the 2nd lowest gpa with the correct name, ect.

I have to swap gpa[0] and gpa[2], which means doing the same thing to names[0] and names [2]. How do I make a swap?

Thanks for all the help guys.

I have a question about:
infile >> names >> grade ;

Will this go through the first line of the file and put the name in names[] and the gpa in grade[], then repeat with i++?

The >> operator will extract from a stream (stdin, in this case) until it hits some white space (a space, tab or newline). So each time you go through the loop you want to get the stuff up until the first white space (the name) and then the stuff up to the second white space (the grade, which stops at a newline character) and then start the loop again. So you end up with the names and grades in the right arrays.

To swap, you have to use a temporary variable:

/* Set up some variables */
int a = 1, b = 2;
int temp;
std::cout << "a = " << a << " b = " << b << std::endl;

/* Now do the swap */
temp = a;
a = b;
b = temp;

/* Check the result */
std::cout << "a = " << a << " b = " << b << std::endl;

There are a lot of ways to sort things, the simplest to understand and code are selection sort, insertion sort and bubble sort. Check out sorting-algorithms.com.

Have fun!

I have a question about:
infile >> names >> grade ;

Will this go through the first line of the file and put the name in names[] and the gpa in grade[], then repeat with i++?

What happened when you tried it? That's easier and faster than posting on a forum.

After this part, I need to order the array so that it is in ascending order, meaning that the lowest gpa goes first with the correct name, followed by the 2nd lowest gpa with the correct name, ect.

That's called a sort. You won't confuse us by using the proper terms.

I have to swap gpa[0] and gpa[2], which means doing the same thing to names[0] and names [2]. How do I make a swap?

By swapping both gpa and names when the gpa is in the wrong order.

Hey Walt,

I respect your response and all, but I was just trying to explain as detailed as possible what I needed to do.

Your answer to my last question leaves me without a clue of what to code, and what do you mean by "thats easier and faster than posting on a forum"?

When I try and do:

in >> student >> studentGPA;

I am getting an error "no operator ">>" matches these operands.

I respect your response and all, but I was just trying to explain as detailed as possible what I needed to do.

Cool

Your answer to my last question leaves me without a clue of what to code,

Do you know how to switch the gpa values if they are in the wrong order? The way you asked it seemed yes.

and what do you mean by "thats easier and faster than posting on a forum"?

You asked if the code will do something specific 10 hours ago. If you actually ran the code then, you would have had the answer 10 hours ago rather than waiting for us.

When I try and do:

in >> student >> studentGPA;

I am getting an error "no operator ">>" matches these operands.

How did you define in ?

When I try and do:
in >> student >> studentGPA;
I am getting an error "no operator ">>" matches these operands.

you need to do

in >> student[i] >> studentGPA[i];

This assigns the values to particular elements of the arrays. What you had is trying to assign values to the name of the arrays, which will fail to compile as you found.

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.