User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 374,145 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,481 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:

C++ Bubble sort not working with array of records, does work with simple array.

Join Date: May 2008
Posts: 9
Reputation: compumasta is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
compumasta compumasta is offline Offline
Newbie Poster

C++ Bubble sort not working with array of records, does work with simple array.

  #1  
May 9th, 2008
  1.  
  2. struct if1
  3. {
  4. string idnum;//id number for the student
  5. string lastname;//last name of student
  6. string firstname;//first name of student
  7. int examscore[maxexam];//array of all the exam scores of the student
  8. int hwscore[maxhw];//array of all the home work scores of the student
  9. };
  10.  
  11.  
  12. void alphasort(if1 student[], int n)
  13. //the alphasort function will sort the records into alphabetical order by last name. It expects the list as well as the total number of
  14. //names and will return the sorted array. The basic format for this function came from the class handout on bubblesorting.
  15. {
  16. if1 temp;//used as a swapping mechanism
  17. int i; int j;// used for implementing for loop checks
  18. int f=1;//used for checking letters after the first
  19. for (i=0; i < n-1; i++)
  20. for (j=0; j < n-(i+1); j++)
  21. if(student[j].lastname[0] > student[j+1].lastname[0])
  22. {
  23. temp = student[j];
  24. student[j] = student[j+1];
  25. student[j+1] = temp;
  26. }
  27. else if(student[j].lastname[0] == student[j+1].lastname[0])
  28. {
  29. while(student[j].lastname[f] == student[j+1].lastname[f])
  30. {
  31. f++;
  32. }
  33. if (student[j].lastname[f] > student[j+1].lastname[f])
  34. {
  35. temp = student[j];
  36. student[j] = student[j+1];
  37. student[j+1] = temp;
  38. }
  39. }
  40. }

im new here, so i hope i didnt post in the wrong area or something...


using an array of structs this function does not work, while the same function using only a simple array of strings does work properly.

i cant figure out why there would be any difference.

the array of structs will not sort alphabetically using all letters in the name if necessary while the simple array will sort 100% correct.

any ideas?
AddThis Social Bookmark Button
Reply With Quote  
All times are GMT -4. The time now is 3:17 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC