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

Grade book program using arrays and Bubble sort (many problems)

P: 11
I am writing a gradbook type program. It first allows the user to enter the number of students they want to enter. then allows them to enter the first name, last name, and grade of each student. The program then should sort the names alphabetically by last name and display a list of all the students names and grades in alphabetical order. I have completed all my functions and classes, but i am getting a god aweful amount of errors and i am not sure what is wrong. It is my first attempt at using classes and objects and bubble sort so i am not sure. here is my code let me know any problems you see, i would greatly appreciate some guidance. First is the header file
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>  // allows the program to output data to the screen
  2.  
  3. // students class definition
  4. class students
  5. {
  6. public:
  7.     int arraySize; // size of array entered by user for number of students
  8.  
  9.     int getNumstudents(int arraySize); // function to get the number of students user wants to enter
  10.     void getData(int arraySize); // function to get first and last name and the students grade
  11.     void displayMessage(); // displays welcome message
  12.     void sortData (); // function to alphabetize by last name using buble sort
  13.     void printData (); // function to print data
  14. private:
  15.     char*firstName[arraySize]; // array of students first names
  16.     char*lastName[arraySize]; // array of students last names
  17.     int grades[arraySize]; // array of students grades
  18. };
  19.  
and here is the cpp file
Expand|Select|Wrap|Line Numbers
  1.  #include <iostream>  // allows the program to output data to the screen
  2. #include <conio.h>
  3. #include  <iomanip>
  4. #include <cstdlib>
  5. #include <string.h>
  6. #include "students.h" // gradebook class defintion
  7.  
  8.  
  9. using std::cout; // program uses cout
  10. using std::cin; // program uses cin
  11. using std::endl; // program uses endl
  12. using std::setprecision; // set numeric output precision
  13. using std::fixed; // ensures that decimal point is displayed
  14. using std::setw;
  15.  
  16.  
  17. void  students::displayMessage()
  18. {
  19.  
  20.     cout << "Welcome to the Student Scores Application." << endl;
  21. }
  22.  
  23. // sorts data alphabetically by last name using bubble sort    
  24. void students::sortData()
  25. {
  26.  
  27.     for(int i = 0; i < arraySize ; i++){
  28.             for(int j = i+1; i < arraySize; j++){
  29.                   if(strcmp(lastName[i]){
  30.                     char *temp = lastName[i];    // swap pointers
  31.                     lastName[i] = lastName[j];
  32.                     lastName[j] = temp;
  33.                 }
  34.               }
  35.         }
  36.  }
  37.  
  38. // Prints all the students data
  39. void students::printData()
  40. {
  41.  
  42.     for (int i = 0; i < arraySize; i++)
  43.     {
  44.  
  45.     cout << "Student " << num << " last name: " << lastName[i] <<", " << firstName[i] << ": " << grades[i] << endl;
  46.  
  47.     }
  48. }
  49.  
  50. // gets the number of students the user wnats to enter and sets that as the arraysize
  51. int students::getNumstudents(int arraySize)
  52. {
  53.     cout << "Enter number of students to enter: ";
  54.     cin >> arraySize;
  55. }
  56.  
  57. // gets students info from user
  58. void students::getData(int arraysize)
  59. {
  60.     int num = 1;
  61.     char*userinput1;
  62.     char*userinput2;
  63.     int userinput3;
  64.  
  65.     while ( num <= arraySize){
  66.  
  67.     cout << "Student " << num << " last name: ";
  68.  
  69.     lastName[arraysize] = userinput1;
  70.  
  71.     cout << "Student " << num << " first name: ";
  72.  
  73.     firstName[arraysize] = userinput2;
  74.  
  75.     cout << "Student " << num << " score: ";
  76.  
  77.     grades[arraysize] = userinput3;
  78.  
  79.     num++
  80.     }
  81. }
  82.  
  83. // function main begins program exectuion
  84. int main() 
  85. {
  86.  
  87.  
  88.     students.displayMessage();
  89.  
  90.     students.getNumstudents();
  91.  
  92.     students.getData();    
  93.  
  94.     students.sortData();
  95.  
  96.     students.printData();
  97. }
  98.  
Apr 30 '08 #1
Share this Question
Share on Google+
14 Replies


Ganon11
Expert 2.5K+
P: 3,652
Two things I immediately notice:

1) This is C++ (as I see from you using strings, cin, cout, etc.), so why are you using char* to hold names and such? That's what std::strings are for.

2) In your main(), you are trying to call methods on an object, but you haven't made a student object. You need to say:

Expand|Select|Wrap|Line Numbers
  1. student myStudent;
  2.  
  3. myStudent.displayMessage();
or something of the sort.

In addition, I'm not sure your design is entirely straightforward. A student shouldn't have to know how many classmates he/she has (i.e. the array size), nor should he/she need to have information about inputting/outputting information (getData(), displayMessage()).

An easier to design (not to mention easier to work with) design would be like this: the Student class has a first name and last name as well as an array/vector of grades, and a constructor which allows you to set these attributes. Then you either have another class, maybe Class, which will hold a collection of students and can sort them, etc.

Finally, if you are getting errors, it's usually easier to go ahead and post these errors as well, rather than letting repliers blindly try to analyze your code.
Apr 30 '08 #2

P: 11
thanks for the help man, i got rid of all the errors and the program "runs' now. When it runs it just prints out getData() cout stuff 100 times, and then prints out Student Last name: folloed by a bunch of jibberish. The program requirments call for the program to promt the user for the number of students they would like to enter, that is the only reason i did it like that. I have commened out the bubble sort, becaussei can't figure out how to make it sort the last names. Any help with with getting the program working better would be apprecriated. here is what i have now,
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>  // allows the program to output data to the screen
  3. #include <string.h>
  4.  
  5. // students class definition
  6. class students
  7. {
  8. public:
  9.     int getNumstudents(); // function to get the number of students user wants to enter
  10.     void getData(); // function to get first and last name and the students grade
  11.     void displayMessage(); // displays welcome message
  12.     //void sortData (); // function to alphabetize by last name using buble sort
  13.     void printData (); // function to print data
  14.     const static int numStudents = 100;
  15. private:
  16.     char firstName[100]; // array of students first names
  17.     char lastName[100]; // array of students last names
  18.     int grades[100]; // array of students grades
  19. };
  20.  
Expand|Select|Wrap|Line Numbers
  1.  /* 
  2. Josh Mauney
  3. CSC215
  4. 4-28-08
  5. student grade program
  6. project 2
  7. */
  8.  
  9. #include <iostream>  // allows the program to output data to the screen
  10. #include <conio.h>
  11. #include  <iomanip>
  12. #include <cstdlib>
  13. #include <string.h>
  14. #include "students.h" // gradebook class defintion
  15.  
  16.  
  17. using std::cout; // program uses cout
  18. using std::cin; // program uses cin
  19. using std::endl; // program uses endl
  20. using std::setprecision; // set numeric output precision
  21. using std::fixed; // ensures that decimal point is displayed
  22. using std::setw;
  23. using std::string;
  24.  
  25.  
  26. void  students::displayMessage()
  27. {
  28.  
  29.     cout << "Welcome to the Student Scores Application." << endl;
  30. }
  31.  
  32. // sorts data alphabetically by last name using bubble sort    
  33. /*void students::sortData(int numStudents)
  34. {
  35.     char temp[100];
  36.  
  37.     for(int i = 0; i < numStudents ; i++){
  38.             for(int j = i+1; i < numStudents; j++){
  39.                   if(strcmp(lastName[i], lastName[j] > 0)){
  40.                     temp = lastName[i];    
  41.                     lastName[i] = lastName[j];
  42.                     lastName[j] = temp;
  43.                 }
  44.               }
  45.         }
  46.  }
  47. */
  48. // Prints all the students data
  49. void students::printData()
  50. {
  51.  
  52.  
  53.     for (int i = 0; i < numStudents; i++)
  54.     {
  55.  
  56.     cout << "Student last name: " << lastName[i] <<", " << firstName[i] << ": " << grades[i] << endl;
  57.  
  58.     }
  59. }
  60.  
  61. // gets the number of students the user wnats to enter and sets that as the arraysize
  62. int students::getNumstudents()
  63. {
  64.     int arraySize;
  65.     cout << "Enter number of students to enter: ";
  66.     return arraySize;
  67. }
  68.  
  69. // gets students info from user
  70. void students::getData()
  71. {
  72.     int num = 1;
  73.     char userinput1;
  74.     char userinput2;
  75.     int userinput3;
  76.  
  77.     while ( num <= numStudents){
  78.  
  79.     cout << "Student " << num << " last name: ";
  80.  
  81.     lastName[numStudents] = userinput1;
  82.  
  83.     cout << "Student " << num << " first name: ";
  84.  
  85.     firstName[numStudents] = userinput2;
  86.  
  87.     cout << "Student " << num << " score: ";
  88.  
  89.     grades[numStudents] = userinput3;
  90.  
  91.     num++;
  92.     }
  93. }
  94.  
  95. // function main begins program exectuion
  96. int main() 
  97. {
  98.     students mystudent;
  99.  
  100.     mystudent.displayMessage();
  101.  
  102.     mystudent.getNumstudents();
  103.  
  104.     mystudent.getData();    
  105.  
  106.     //mystudent.sortData();
  107.  
  108.     mystudent.printData();
  109.  
  110.     char pause = getchar();  // program pauses before ending
  111.  
  112.     getchar();
Apr 30 '08 #3

Expert 100+
P: 671
Quick note: it's <string>, not <string.h> . You do know how standard headers work, do you not?

So, what have you done to figure out where the problem areas are? It's not nice to take your non working program, dump it on us, and say "figure out what's wrong please".
May 1 '08 #4

P: 11
I'm sorry that is not what i have meant to do...I just wanted suggestions and i got some that helped alot.. like the idea of using strings, so now i am using string vectors and completly redid my program. I do appreicate your guys help and sorry if came off stupid...i was just really lost.. it being my first large program and all. Here is what i have now as sort of prototype using the vectors. I can get it to read the names i eneter, but for some reason as soon as i am done enetering them the program quits instead of displaying them.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>  // allows the program to output data to the screen
  2. #include <string>
  3.  
  4. // students class definition
  5. class students
  6. {
  7. public:
  8.     void getData(); // function to get first and last name and the students grade
  9.     void displayMessage(); // displays welcome message
  10.  
  11. private:
  12.  
  13. }; 
Expand|Select|Wrap|Line Numbers
  1.  #include <iostream>  // allows the program to output data to the screen
  2. #include <conio.h>
  3. #include  <iomanip>
  4. #include <cstdlib>
  5. #include <string>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <numeric>
  9. #include "students.h" // gradebook class defintion
  10.  
  11.  
  12. using std::cout; // program uses cout
  13. using std::cin; // program uses cin
  14. using std::endl; // program uses endl
  15. using std::setprecision; // set numeric output precision
  16. using std::fixed; // ensures that decimal point is displayed
  17. using std::setw;
  18. using std::string;
  19. using std::vector;
  20.  
  21.  
  22. void  students::displayMessage()
  23. {
  24.  
  25.     cout << "Welcome to the Student Scores Application." << endl << endl;
  26. }
  27.  
  28.  
  29. void students::getData()
  30. {
  31.     int numStudents;
  32.     vector<string> student_names(numStudents);
  33.  
  34.     cout <<"Enter number of students to enter: ";
  35.     cin >> numStudents;
  36.  
  37.     for (int i = 0; i <= numStudents; i++)
  38.     { 
  39.             cout << "Enter student name (Enter to finish): "; 
  40.             getline (cin, student_names[i]);
  41.     }
  42.  
  43.             // sort them alphabetically
  44.         sort (student_names.begin(), student_names.end());
  45.  
  46.     for (int i =0; i < numStudents; i++)
  47.     {
  48.             cout << student_names[i];
  49.     }
  50.  
  51.  
  52.  
  53. }
  54.  
  55. // function main begins program exectuion
  56. int main() 
  57. {
  58.     students mystudent;
  59.  
  60.     mystudent.displayMessage();
  61.  
  62.     mystudent.getData();    
  63.  
  64. }
May 1 '08 #5

Expert 100+
P: 671
I have two questions for you. Hopefully this gives you a hint.

int i = 0; i <= numStudents
You sure about that?

Next. Look at the following code:

Expand|Select|Wrap|Line Numbers
  1. void createNumbers()
  2. {
  3.     std::vector<int> numbers;
  4.     numbers.push_back(1);
  5.     numbers.push_back(3);
  6.     numbers.push_back(10);
  7. }
  8.  
  9. void printNumbers()
  10. {
  11.     createNumbers();
  12.     std::cout << numbers[0] << std::endl;
  13. }
  14.  
What's wrong with the above code?
May 1 '08 #6

P: 11
I have two questions for you. Hopefully this gives you a hint.

You sure about that?

Next. Look at the following code:

Expand|Select|Wrap|Line Numbers
  1. void createNumbers()
  2. {
  3.     std::vector<int> numbers;
  4.     numbers.push_back(1);
  5.     numbers.push_back(3);
  6.     numbers.push_back(10);
  7. }
  8.  
  9. void printNumbers()
  10. {
  11.     createNumbers();
  12.     std::cout << numbers[0] << std::endl;
  13. }
  14.  
What's wrong with the above code?
the only problem i can see is that your just print the 0 space in the numbers array. So i am guessing that is what i am doing in my code..calling it to output nothing. and as far as being sure about i=0; i < numStudents; i++...i am pretty sure because that would call each array element 0 thorugh the size of array. right? i appreciate your method of help...i don't like being told the answer. thanks
May 1 '08 #7

Expert 100+
P: 671
You've missed both hints, so I'll throw it again in a slightly more indepth fashion. Let's start with the <= numStudents issue. I'll throw out sample code again.

Expand|Select|Wrap|Line Numbers
  1. int numItems = 1;
  2. std::vector<int> numbers(numItems);
  3. numbers[0] = 50;
  4.  
How many elements are there in numbers? Clearly 1. Let's loop over this vector.
Expand|Select|Wrap|Line Numbers
  1. for (int i=0; i<=numItems; ++i) //remember that numItems=1
  2. {
  3.     std::cout << numbers[i] << std::endl;
  4. }
  5.  
Write this out by hand. Write down i. Write down the evaluation of i<= numItems. If true, write down numbers[i]. Then repeat the loop. You'll see the problem (a very, very frequent one).

As to my second example. You also missed the issue.

the only problem i can see is that your just print the 0 space in the numbers array
Terminology first. It's a vector, not an array. There is no such thing as a 0 space. An index of 0 gets me the first element. Also, it's impossible to output, but have nothing as your output. You either output or you don't. If you do output, it must be something in memory. Now, you know that the computer works in 1s and 0s. That is, you either have a 1 or 0. "Nothing" is not a part of the binary system. But this all gets away from the real important point.

First, answer this question.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.     std::cout << i;
  6. }
  7.  
What's wrong here?

Then:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. void someFunc()
  4. {
  5.     int i=50;
  6. }
  7.  
  8. int main()
  9. {
  10.     std::cout << i;
  11. }
  12.  
What's wrong here?

Then:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. void someFunc()
  4. {
  5.     int i=50;
  6. }
  7.  
  8. int main()
  9. {
  10.     someFunc();
  11.     std::cout << i;
  12. }
  13.  
So what's wrong here? Don't hesitate to use your compiler to get a hint.
May 1 '08 #8

P: 11
You've missed both hints, so I'll throw it again in a slightly more indepth fashion. Let's start with the <= numStudents issue. I'll throw out sample code again.

Expand|Select|Wrap|Line Numbers
  1. int numItems = 1;
  2. std::vector<int> numbers(numItems);
  3. numbers[0] = 50;
  4.  
How many elements are there in numbers? Clearly 1. Let's loop over this vector.
Expand|Select|Wrap|Line Numbers
  1. for (int i=0; i<=numItems; ++i) //remember that numItems=1
  2. {
  3.     std::cout << numbers[i] << std::endl;
  4. }
  5.  
Write this out by hand. Write down i. Write down the evaluation of i<= numItems. If true, write down numbers[i]. Then repeat the loop. You'll see the problem (a very, very frequent one).

As to my second example. You also missed the issue.

Terminology first. It's a vector, not an array. There is no such thing as a 0 space. An index of 0 gets me the first element. Also, it's impossible to output, but have nothing as your output. You either output or you don't. If you do output, it must be something in memory. Now, you know that the computer works in 1s and 0s. That is, you either have a 1 or 0. "Nothing" is not a part of the binary system. But this all gets away from the real important point.

First, answer this question.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.     std::cout << i;
  6. }
  7.  
What's wrong here?

Then:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. void someFunc()
  4. {
  5.     int i=50;
  6. }
  7.  
  8. int main()
  9. {
  10.     std::cout << i;
  11. }
  12.  
What's wrong here?

Then:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. void someFunc()
  4. {
  5.     int i=50;
  6. }
  7.  
  8. int main()
  9. {
  10.     someFunc();
  11.     std::cout << i;
  12. }
  13.  
So what's wrong here? Don't hesitate to use your compiler to get a hint.

Alright on the first one the for loop is only ran through once when i=0, after that i = 1 and and so is numItems. So i see why that is wrong, but in my code i have declared int numStudents and then let the user define the number of students. So in my for loop i shouldn't have that problem...at least that is what i think.

And for the second questions on the codes you wrote, on all three of them you haven't declared i. But in my code i have declared i in the for loop. Maybe i am missing the big picture here. i will coninue to try to figure it out. I appreciate the help...i am sure it will click soon enough,
May 1 '08 #9

P: 11
Alright i think i just had a revalation. i realized what you were saying about i < numStudents. I need to set it up so i is less than or equal to the size of the vector. so this is how i changed my code... and it worked it printed the names in alphbetical order.
Expand|Select|Wrap|Line Numbers
  1.  void students::getData()
  2. {
  3.     int numStudents;
  4.     vector<string> student_names(numStudents);
  5.  
  6.     cout <<"Enter number of students to enter: ";
  7.     cin >> numStudents;
  8.  
  9.     for (int i = 0; i <= numStudents; i++)
  10.     { 
  11.             cout << "Enter student name (Enter to finish): "; 
  12.             getline (cin, student_names[i]);
  13.     }
  14.  
  15.             // sort them alphabetically
  16.         sort (student_names.begin(), student_names.end());
  17.  
  18.     for (int i =0; i <= student_names.size(); i++)
  19.     {
  20.             cout << student_names[i];
  21.     }
  22.  
  23.  
  24.  
thanks for the help...hopefully i will be ok from here on out....I know it is printing out right, but did i do it correctly, i jst want to make sure i am learning properly. thanks very much
May 1 '08 #10

Expert 100+
P: 671
Ok, I guess I made things a bit more confusing...

Expand|Select|Wrap|Line Numbers
  1. int  numItems = 1;
  2. std::vector<int> numbers(numItems);
  3. numbers[0] = 50;
  4. for (int i =0; i<= numItems; ++i)
  5. {
  6.     std:: cout << numbers[i] << '\n';
  7. }
  8.  
Let's run through this.

Expand|Select|Wrap|Line Numbers
  1. Output:
  2. i    i<=numItems    numbers[i]
  3. 0    0<=1 true      numbers[0]==50
  4. 1    1<=1 true      numbers[1]==?
  5.  
Do you see the problem?


And don't worry about the whole declaring i thing. I guess I pulled out the wrong idea with it. It's a design issue you have in your class, and you'll realize it soon enough.
May 1 '08 #11

P: 11
Ok, I guess I made things a bit more confusing...

Expand|Select|Wrap|Line Numbers
  1. int  numItems = 1;
  2. std::vector<int> numbers(numItems);
  3. numbers[0] = 50;
  4. for (int i =0; i<= numItems; ++i)
  5. {
  6.     std:: cout << numbers[i] << '\n';
  7. }
  8.  
Let's run through this.

Expand|Select|Wrap|Line Numbers
  1. Output:
  2. i    i<=numItems    numbers[i]
  3. 0    0<=1 true      numbers[0]==50
  4. 1    1<=1 true      numbers[1]==?
  5.  
Do you see the problem?


And don't worry about the whole declaring i thing. I guess I pulled out the wrong idea with it. It's a design issue you have in your class, and you'll realize it soon enough.

I understand the problem in the example you gave. Becasue numbers[0] = 50 that when the for loop tries to call numbers[1] there is nothing there, even though the condition is true....wait are you saying that when i am setting myy for loop to i <= numStudents, that when it gets to that last element there is nothing there because the vector starts at 0 and numstudents starts at 1. so i would set the for loop to i <= numStudents -1. that makes since...but then again maybe i am completly wrong again. I apologize if i am complety wrong again, i dont mean to waste you time, but i really appreciate it. I have been up for the past 24 hrs working on this program , i think i just need to sleep and it will make more sense in the morning. ill give it a try though beforei hit the sack.. thanks again.
May 1 '08 #12

Ganon11
Expert 2.5K+
P: 3,652
I understand the problem in the example you gave. Becasue numbers[0] = 50 that when the for loop tries to call numbers[1] there is nothing there, even though the condition is true....wait are you saying that when i am setting myy for loop to i <= numStudents, that when it gets to that last element there is nothing there because the vector starts at 0 and numstudents starts at 1. so i would set the for loop to i <= numStudents -1. that makes since...but then again maybe i am completly wrong again. I apologize if i am complety wrong again, i dont mean to waste you time, but i really appreciate it. I have been up for the past 24 hrs working on this program , i think i just need to sleep and it will make more sense in the morning. ill give it a try though beforei hit the sack.. thanks again.
i <= numStudents -1

Will work and accomplish your goal, or you could write

i < numStudents

which will do the same thing.

Basically, because arrays and vectors are 0 indexed (that is, the first element is at index 0), the last index is always size - 1. So, in a vector of size 5, the elements are at index 0, 1, 2, 3, and 4. There is no guarantee that anything useful can be found in index 5.
May 1 '08 #13

P: 11
hey man thanks that worked wonders. I have edited the code and it does run smoother and doesnt crash. The problem i run into now is that when i run it last name and first name are displayed at the same time for the user to enter. Like this last name: first name:. How get them to be seperate so the user can enter them individually. Also it not sorting them but i think it has to do with the program not getting the first and last name seperatly.
Expand|Select|Wrap|Line Numbers
  1.  #include <iostream>  // allows the program to output data to the screen
  2. #include <conio.h>
  3. #include  <iomanip>
  4. #include <cstdlib>
  5. #include <string>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <numeric>
  9. #include "students.h" // gradebook class defintion
  10.  
  11.  
  12. using std::cout; // program uses cout
  13. using std::cin; // program uses cin
  14. using std::endl; // program uses endl
  15. using std::setprecision; // set numeric output precision
  16. using std::fixed; // ensures that decimal point is displayed
  17. using std::setw;
  18. using std::string;
  19. using std::vector;
  20.  
  21.  
  22. void  students::displayMessage()
  23. {
  24.  
  25.     cout << endl << "Welcome to the Student Scores Application." << endl << endl;
  26. }
  27.  
  28.  
  29. void students::getData()
  30. {
  31.     int numStudents = 0;
  32.     string name;    
  33.     int score = 0;
  34.     int i = 0;
  35.  
  36.     cout <<"Enter number of students to enter: ";
  37.     cin >> numStudents;
  38.  
  39.     vector<string> student_lastnames(numStudents);
  40.     vector<string> student_firstnames(numStudents);
  41.     vector <int> student_score(numStudents);
  42.  
  43.     do
  44.  
  45.     { 
  46.             cout << "Student " << i + 1 <<" last name: "; 
  47.             getline (cin, name);
  48.               student_lastnames.push_back(name);
  49.  
  50.             cout << "Student " << i + 1 <<" first name: "; 
  51.             getline (cin, name);
  52.              student_firstnames.push_back(name);
  53.  
  54.             cout << "Student " << i + 1 <<" score: "; 
  55.             cin >> score;
  56.         student_score.push_back(score);
  57.  
  58.      i++;
  59.      }
  60.  
  61.      while ( i < numStudents);
  62.  
  63.  
  64.             // sort them alphabetically
  65.         sort (student_lastnames.begin(), student_lastnames.end());
  66.  
  67.     for (int l =0; l < student_lastnames.size(); l++)
  68.     {
  69.             cout << student_lastnames[l] << " " << student_firstnames[l] << " " << student_score[l];
  70.     }
  71.  
  72.  
  73.  
  74. }
  75.  
  76. // function main begins program exectuion
  77. int main() 
  78. {
  79.     students mystudent;
  80.  
  81.     mystudent.displayMessage();
  82.  
  83.     mystudent.getData();    
  84.  
May 1 '08 #14

P: 11
now the user can enter all the info indivudally. And it the sort works... the problem i run into now is that for some reason when it prints it always priints two zeros before the text. and while it sorts the last names it prints the last name that is sorted withe first name and score of the first entry. ex. user inputs
studnt 1 last name: oaks
student 1 first name: john
student 1 grade: 100

studnt 2 last name: hill
student 2 first name: mike
student 2 grade: 75

program prints out:

0 0 hill john 100 oaks mike 75

i realize that since i am just sorting the lastname vector, that it is storing the sorted last name with the user input of the first name. not sure how to fix it and get rid of the zeros. thanks for the help so far.


Expand|Select|Wrap|Line Numbers
  1.  #include <iostream>  // allows the program to output data to the screen
  2. #include <conio.h>
  3. #include  <iomanip>
  4. #include <cstdlib>
  5. #include <string>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <numeric>
  9. #include <fstream>
  10. #include <ios>
  11. #include <istream>
  12. #include <limits>
  13. #include "students.h" // gradebook class defintion
  14.  
  15.  
  16. using std::cout; // program uses cout
  17. using std::cin; // program uses cin
  18. using std::endl; // program uses endl
  19. using std::setprecision; // set numeric output precision
  20. using std::fixed; // ensures that decimal point is displayed
  21. using std::setw;
  22. using std::string;
  23. using std::vector;
  24. using std::max;
  25.  
  26.  
  27. void  students::displayMessage()
  28. {
  29.  
  30.     cout << endl << "Welcome to the Student Scores Application." << endl << endl;
  31. }
  32.  
  33.  
  34. void students::getData()
  35. {
  36.     int numStudents = 0;
  37.     string name;    
  38.     int score = 0;
  39.     int i = 0;
  40.  
  41.     cout <<"Enter number of students to enter: ";
  42.     cin >> numStudents;
  43.     cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
  44.  
  45.     vector<string> student_lastnames(numStudents);
  46.     vector<string> student_firstnames(numStudents);
  47.     vector <int> student_score(numStudents);
  48.  
  49.     do
  50.  
  51.     { 
  52.             cout << "Student " << i + 1 <<" last name: "; 
  53.             cin >> name;
  54.               cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );    
  55.         student_lastnames.push_back(name);
  56.  
  57.  
  58.  
  59.  
  60.             cout << "Student " << i + 1 <<" first name: "; 
  61.             cin >> name;
  62.              cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
  63.         student_firstnames.push_back(name);
  64.  
  65.             cout << "Student " << i + 1 <<" score: "; 
  66.             cin >> score;
  67.         cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );    
  68.         student_score.push_back(score);
  69.  
  70.      i++;
  71.      }
  72.  
  73.      while ( i < numStudents);
  74.  
  75.  
  76.             // sort them alphabetically
  77.         sort (student_lastnames.begin(), student_lastnames.end());
  78.  
  79.     for (int l =0; l < student_lastnames.size(); l++)
  80.     {
  81.             cout << student_lastnames[l] << " " << student_firstnames[l] << " " << student_score[l];
  82.     }
  83.  
  84.  
  85.  
  86. }
  87.  
  88. // function main begins program exectuion
  89. int main() 
  90. {
  91.     students mystudent;
  92.  
  93.     mystudent.displayMessage();
  94.  
  95.     mystudent.getData();    
  96.  
May 1 '08 #15

Post your reply

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