473,785 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

11 New Member
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
14 8064
oler1s
671 Recognized Expert Contributor
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
xtheendx
11 New Member
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
3,652 Recognized Expert Specialist
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
xtheendx
11 New Member
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
xtheendx
11 New Member
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

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

Similar topics

6
4964
by: hoover_richard | last post by:
I am a newbie to C++ and I need help with a simple program I am trying to write. My program is designed to print all of the odd integers contained in an array and output the sum of the odd integers. My code is listed below, but the problem is that my output of sum is wrong. For example, I am using 1347830 for my integers and the program outputs 373 and after adding 373 you should get 13 for the total, but that doesn't happen. Any help...
2
4339
by: Tonytt | last post by:
Hi, I'm pretty new to c-programming and am taking a course in it. I'm trying to write a menu program using functions and a two-dimensional array. The program consists of entering 4 test grades for 10 students. The array is initialized with -1 in the beginning - this is because if a student did not take a test yet I want to be able to sort and find this out. There is a menu option to enter grades which calls a function in order to enter...
0
4441
by: Slowjam | last post by:
How do I correct the program below to search all the anagrams in a given file. First, for each word, build another word (its key) with all its letters sorted. For instance, build "dorw" for "word", or "eelttr" for "letter". Build an array of all the keys, and sort it using a bubble sort. I have to write a modified version of bubble sort that maintains also the array of the primary words. For instance, if I had , our initial array of keys...
5
3930
kuchma2000
by: kuchma2000 | last post by:
Hi. I have a C program (see code below) which I need to urgrade. All I need to do is: a. Extract the temps for the City #1 and store in a one dimensional array, display the array. b. Sort the one dimensional array of City #1 temps using a selection sort and display the sorted array c. Extract the temps recorded from all Cities as 3rd entry and store in a one dimensional array, display the array d. Sort the one dimensional array of temps...
1
5661
by: guest | last post by:
I am doing a program for Intro to Computer Programming where we take an array of strings and we must sort them alphabetically. we are supposed to use a bubble sort, but i know the code if meant for integers. How do i get a bubble sort to put names in alphabetical order?
0
1446
by: hockeyjock | last post by:
I need to develop a grade book program. This is my first programming class. I have learned variables, data types, math operations and precedence, I HAVE not learned methods, I have read about 5 book and need help. I need to ask a different question that is similar to this, make up something different because this is my homework question and that is not allowed. Question: The First program is required to ask for 5 students names and their...
0
1752
by: anelie | last post by:
the problem is: using 2 listboxes and 2 command buttons.. when you click the 1st cmd button an inputbox will appear asking the number of items to be sorted..then when you inputed a number, another inputboxes will appear asking for the items to be sorted... then the items you inputed will be printed in the 1st listbox.. then when you click the 2nd cmd button the items in the first listbox will appear in the second listbox but they...
7
7153
by: mahdiahmadirad | last post by:
Hi dears! I wrote a simple bubble sort algorithm. it works properly when we compare full arrays but i want to sort a 2d array according to a specific part of array. it has some problem to swapping this array. please help me. my scenario: assume that we have a big 2d char array for example students for 20 persons an 30 character for each person. first 15 chars contains first name and the rest is last name. no i want to sort this array...
0
9484
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10097
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9957
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7505
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.