By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

C++ Parallel Arrays - Storing and Sorting

P: 5
Hi,

I'm working on a program that takes the following input from the user:

Student Name, Student Number, and Student GPA

The information is then supposed to be stored in three separate arrays.

Prior to sending the output to the screen, a bubble sort has to be performed
so that it's sorted by Student Name.

Here is the code I have so far, but I'm having the following problems:

1 - I'm unsure about the first while loop for input (need to know what I
should be comparing it to, so that it breaks properly.

2 - I need to know more about the sorting of parallel arrays

Expand|Select|Wrap|Line Numbers
  1.     int studentNumber[50];
  2.     string studentName[50],temp;
  3.     double studentGPA[50],temper;
  4.     int i=0,tmp;
  5.     int x = 0;
  6.     int y = 0;
  7.     int ia = 0;
  8.     int length = 0;
  9.  
  10.  /* Prompt user for input of studentNumber, studentName, and studentGPA */
  11.     while (studentNumber[i] != 0)
  12.     {
  13.  
  14.  
  15.         cout << "Enter Student Number: ";
  16.         cin >> studentNumber[i];
  17.         cout << endl;
  18.         cout << "Enter Student Name: ";
  19.         cin >> studentName[i];
  20.         cout << endl;
  21.         cout << "Enter Student GPA: :";
  22.         cin >> studentGPA[i];
  23.         cout << endl;
  24.         i = i + 1;
  25.     }
Any help is greatly appreciated. I know that it's probably something simple, but my eyes having been looking at the code all afternoon and I'm missing something.

Earl
Mar 12 '07 #1
Share this Question
Share on Google+
8 Replies


Ganon11
Expert 2.5K+
P: 3,652
For the while loop, why not add a char variable before the loop initialized to 'Y'. Inside the loop, ask the user, "Do you want to enter more data?" and input into the character. Then your loop condition will be "Continue while the character is 'Y' or 'y'".

For sorting the parallel arrays, it looks like you have the right idea.
Mar 12 '07 #2

P: 5
For the while loop, why not add a char variable before the loop initialized to 'Y'. Inside the loop, ask the user, "Do you want to enter more data?" and input into the character. Then your loop condition will be "Continue while the character is 'Y' or 'y'".

For sorting the parallel arrays, it looks like you have the right idea.
Many thanks for your help...

I have the while loop working great :)

However, my sort doesn't appear to be working....

Any ideas?

Earl
Mar 12 '07 #3

P: 5
Many thanks for your help...

I have the while loop working great :)

However, my sort doesn't appear to be working....

Any ideas?

Earl
Well, I did manage to fix the sorting code, but the output to the screen has
something funky going on. If anyone can figure it out, I would be greatful.

Data:

123456 Bakter 3.0
342130 Carson 3.8
111230 King 4.0
234512 Zimmerman 3.1

Output Display:

Mar 12 '07 #4

Ganon11
Expert 2.5K+
P: 3,652
Very strange...Where are you outputting the data? Can we see just that portion of the code again?

It looks like it is trying to output garbage data, like memory addresses or something.
Mar 12 '07 #5

DeMan
100+
P: 1,806
I think there may be a slight problem with

Expand|Select|Wrap|Line Numbers
  1. while(studentNumber[i] != 0)
  2. {
  3.   cin >> studentNumber[i];
  4.  
  5.   // other things
  6.   i = i + 1;
  7. }
  8.  
This means you are comparing to an unitialised array value......(I think this may be why Ganon11 suggested adding a "do you want to do more" type statement on the while loop)
Mar 12 '07 #6

P: 5
I want to thank everyone for their great help :)

I did solve the mystery data output problem that was in my screenshot
earlier.

It was a result of not subtracting '1' from my inner loop counter when
the 'done' condition was met.

The mystery data that you seen on the screen was the contents
of the next array, which hadn't been filled yet :)



Earl
Mar 13 '07 #7

Ganon11
Expert 2.5K+
P: 3,652
It's always something simple :)

Good to see you figured it out!
Mar 13 '07 #8

P: 5
I just had to take it section, by section and walk through the logic with some
debugging prints to the screen.

It sure feels good to solve those problems and see the software do exactly what you want it too :)

Earl
Mar 13 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.