Please I need help formatting an output file and sorting the names!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 23
Reputation: angel22 is an unknown quantity at this point 
Solved Threads: 0
angel22 angel22 is offline Offline
Newbie Poster

Please I need help formatting an output file and sorting the names!

 
0
  #1
Aug 2nd, 2005
Hello:

I need help with a project.

I have to read an input file that contains data for 17 students. This is how they are: first ID, then first name, then last name, then 10 test scores.

For this data, I have to read it and process it to send it to a new file that only contains:

the last name, a comma, the first name, the ten scores, and the average for them followed by a letter grade.

However, the names have to be in alphabetical order, and it has to be formatted.

This is the code I have now:

It outputs the last name, a comma, first name, scores, average and letter grade.

However, it doesn't put the names in order neither makes them look formatted.

Any ideas?

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <iomanip>
  6. using namespace std;
  7.  
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11. ifstream infile;
  12. int ID, score[10];
  13. float ave, sum=0;
  14. string firstn, lastn, fullname;
  15. infile.open ("c:\\students.txt");
  16. while (infile>>ID>>firstn>>lastn) // read file
  17. {
  18. fullname= lastn + "," +firstn; // combine last
  19. cout.setf(ios::fixed); // and 1st name
  20. cout.setf(ios::showpoint);
  21.  
  22. cout<<fullname<<" "; // Outputs fullname
  23.  
  24.  
  25. for (int n=0; n<10; n++) // processes scores
  26. { infile>>score[n];
  27.  
  28. cout<<score[n]<<" "; // outputs scores
  29. sum+=score[n]; // sums scores
  30. }
  31. ave=sum/10; // Processes average
  32. cout.precision(3);
  33. cout<<ave<<" "; // Outputs average
  34. if (ave>=90)
  35. {cout<<"A"; // Processes letter grades and
  36. } // Outputs it
  37. else
  38. { if (ave>=80)
  39. {cout<<"B";
  40. }
  41. else
  42. { if(ave>=70)
  43. {cout<<"C";
  44. }
  45. else
  46. { if(ave>=60)
  47. {cout<<"D";
  48. }
  49. else
  50. {cout<<"F";
  51. }
  52. }
  53. }
  54. }
  55. cout<<endl;
  56. sum=0; // resets sum to 0 for new student
  57. }
  58.  
  59.  
  60.  
  61.  
  62. system("PAUSE");
  63. return EXIT_SUCCESS;
  64. }
<< moderator edit: added [code][/code] tags >>
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Please I need help formatting an output file and sorting the names!

 
0
  #2
Aug 2nd, 2005
what is the format that it has to be in?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 23
Reputation: angel22 is an unknown quantity at this point 
Solved Threads: 0
angel22 angel22 is offline Offline
Newbie Poster

Re: Please I need help formatting an output file and sorting the names!

 
0
  #3
Aug 2nd, 2005
For example:

The output for let's say two students would be:


Cannon, Scott 70 75 78 88 83 85 79 91 88 90 82.7 B
Espinola, Angelica 97 98 89 88 90 92 95 100 92 88 99.1 A


In alphabetical order and name to the left, and scores, average and letter grade to the right!

I've got the names to the left, but I need to sort them and move scores and average to the right all aligned!


this thing doesn't let me put them justified but at least it is similar!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 23
Reputation: angel22 is an unknown quantity at this point 
Solved Threads: 0
angel22 angel22 is offline Offline
Newbie Poster

Re: Please I need help formatting an output file and sorting the names!

 
0
  #4
Aug 2nd, 2005
I've working with this, and I have already solved the formatting problem, my only problem left is how to put the names in order!

This is how I changed it a little, I just included some width and precision functions, as well as left and right justifiers.


  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <iomanip>
  6. using namespace std;
  7.  
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11. ifstream infile;
  12. int ID, score[10];
  13. float ave, sum=0;
  14. string firstn, lastn, fullname;
  15. infile.open ("c:\\students.txt");
  16. while (infile>>ID>>firstn>>lastn)
  17. {
  18. fullname= lastn + "," +firstn;
  19. cout.setf(ios::fixed);
  20. cout.setf(ios::showpoint);
  21.  
  22. cout.precision(3);
  23. cout << showpoint;
  24. cout.width(20);
  25. cout<<left<<fullname<<" ";
  26.  
  27.  
  28. for (int n=0; n<10; n++)
  29. { infile>>score[n];
  30. cout.precision(3);
  31. cout << showpoint;
  32. cout.width(3);
  33. cout <<right<<score[n]<<" ";
  34. sum+=score[n];
  35. }
  36. ave=sum/10;
  37. cout.precision(1);
  38. cout<<ave<<" ";
  39. if (ave>=90)
  40. {cout.precision(1);
  41. cout << showpoint;
  42. cout.width(2);
  43. cout<<right<<"A";
  44. }
  45. else
  46. { if (ave>=80)
  47. {cout.precision(1);
  48. cout << showpoint;
  49. cout.width(2);
  50. cout<<right<<"B";
  51. }
  52. else
  53. { if(ave>=70)
  54. {cout.precision(1);
  55. cout << showpoint;
  56. cout.width(2);
  57. cout<<"C";
  58. }
  59. else
  60. { if(ave>=60)
  61. {cout.precision(1);
  62. cout << showpoint;
  63. cout.width(2);
  64. cout<<"D";
  65. }
  66. else
  67. {cout.precision(1);
  68. cout << showpoint;
  69. cout.width(2);
  70. cout<<"F";
  71. }
  72. }
  73. }
  74. }
  75. cout<<endl;
  76. sum=0;
  77. }
  78.  
  79.  
  80.  
  81.  
  82. system("PAUSE");
  83. return EXIT_SUCCESS;
  84. }
<< moderator edit: added [code][/code] tags >>
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 62
Reputation: freemind is an unknown quantity at this point 
Solved Threads: 1
freemind's Avatar
freemind freemind is offline Offline
Junior Poster in Training

Re: Please I need help formatting an output file and sorting the names!

 
0
  #5
Aug 2nd, 2005
The sorting you can do in many ways. One way could be putting all the data in let's say a class and then sort the vector of objects with ssort(), qsort() or whatever function you want. Other way could be putting the unsorted lines in a temp file, taking the first letter of every line and comparing the first letter from the next line with it (Tip: after the first letter, which is at position 0 in the file every next letter will be after a '\n'). If the corresponding condition is true you can swap the lines and go on. There are other ways probably better ones, this is what comes to me now.

The formatting you can do by creating an imaginary "name field" for the names. When you get them from the file and create the fullname you can check it's length and save it as a temporary. Consequtive checks can give you the size of the longest name. When writing the temp output file you can check the name of the student before streaming it to the file and add to it (if necessary) the number of white spaces it needs to reach the name with the max size, i.e. int bla = longest_name - fullname.size(); and add 'bla' blanks to fullname. Then you can continue with copying the scores. Since I don't know the inbuilt C++ libraries too good I can't offer you a more subtle way to do the formatting, but this should work.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Please I need help formatting an output file and sorting the names!

 
0
  #6
Aug 2nd, 2005
You are going to need to store the information as you read it in, do the sort and then write it back out. Can't write each line as you read it in AND sort at same time. my c/c++ is a little rusty but you want something like the following. Was in a little bit of a hurry and was doing this in notepad, so there might be some mismatched brackets, but that should give you an idea
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <iomanip>
  6. using namespace std;
  7.  
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11. ifstream infile;
  12. int IDarray[17], scorearray[17][10];
  13. int count = 0, ID;
  14. float ave[17], sum=0;
  15. string firstn, lastn, fullname[17];
  16. string tempname;
  17. int tempint;
  18.  
  19. // get all the data
  20. infile.open ("c:\\students.txt");
  21. while (infile>>ID>>firstn>>lastn) // read file
  22. {
  23. fullname[count]= lastn + "," +firstn; // combine last
  24. IDarray[count] = id;
  25. for (int n=0; n<10; n++) // processes scores
  26. { infile>>score[count][n];
  27. }
  28. count++;
  29. }
  30.  
  31. // sort it
  32.  
  33. for (int n=0; n<17; n++){
  34. for (int m=n+1; m<17; m++)
  35. {
  36.  
  37. if(fullname[m] comes before [fullname[n])// sorry, can't give you this exactly will have to do pseudocode
  38. {
  39. tempname = fullname[m];
  40. fullname[m] = fullname[n];
  41. fullname[n] = tempname;
  42. for(int i = 0; i < 10; i++)
  43. {
  44. tempint = score[n][i];
  45. score[n][i] = score[m][i];
  46. score[m][i] = tempint;
  47. }
  48. }
  49. }
  50. }
  51.  
  52. // now that it is sorted by name, print it out
  53. cout.setf(ios::fixed); // and 1st name
  54. cout.setf(ios::showpoint);
  55. for (int i = 0; i < 17; i++){
  56. sum = 0;
  57. for (int j = 0; j < 10; j++)
  58. {
  59. sum += score[i][j];
  60. }
  61. ave=sum/10; // Processes average
  62. cout<<fullname<<" "; // Outputs fullname
  63. cout<<score[n]<<" "; // outputs scores
  64. cout.precision(3);
  65. cout<<ave<<" "; // Outputs average
  66. if (ave>=90)
  67. {
  68. cout<<"A"; // Processes letter grades and
  69. } // Outputs it
  70. else
  71. { if (ave>=80)
  72. {cout<<"B";
  73. }
  74. else
  75. { if(ave>=70)
  76. {cout<<"C";
  77. }
  78. else
  79. { if(ave>=60)
  80. {cout<<"D";
  81. }
  82. else
  83. {cout<<"F";
  84. }
  85. }
  86. }
  87. }
  88. cout<<endl;
  89.  
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. system("PAUSE");
  100. return EXIT_SUCCESS;
  101. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 23
Reputation: angel22 is an unknown quantity at this point 
Solved Threads: 0
angel22 angel22 is offline Offline
Newbie Poster

Re: Please I need help formatting an output file and sorting the names!

 
0
  #7
Aug 3rd, 2005
I am still working on this problem, but I cannot find a way to finish it without mixing up all the scores and the names.

Is there a way to sort the names without mixing the scores with anyone else's scores.

I can't use pointers because I do not know them yet.

Nor, classes!


Please, any help is really apprecciated, I am really confused.

The code sent to me is kind of confusing because it makes me loose track of what it is doing!

Can anyone explain it to me, I find it difficult to understand two dimensional arrays!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Please I need help formatting an output file and sorting the names!

 
0
  #8
Aug 3rd, 2005
post the code your are using to sort
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Please I need help formatting an output file and sorting the names!

 
0
  #9
Aug 3rd, 2005
Doing this without using struct/classes will be tedious. You could have an array of firstnames only, an array of last names only, an array of middle names only, an array for each persons first exam grades, an array for each persons second exam grades, ..., an array for each persons grade average, and an array for each letter grade. A given index in a given array has the information for the same person. Therefore, if you have to rearrange the order of the names after loading all the file data, to keep everything correlated by index you could rearrange the order for each of the other arrays in the same manner. So, for example, if you are using bubble sort to sort the array of last names and you end up swapping LastName[i] with LastName[i + 1]
Then you will need to swap:
FirstName[i] with FirstName[i + 1];
Exam1[i] with Exam1[i + 1];
Exam2[i] with Exam2[i + 1];
.
.
.
Exam10[i] with Exam10[i + 1];
AveNumResult[i] with AveNumResult[i + 1];
and, finally
LetterGrade[i] with LetterGrade[i + 1];
etc.

After setting all that up just once, I can assure you, you will appreciate what struct/classes can do for you when you learn about them.

Since you don't know pointers then you won't be able to use lists, so if you try to sort with insertion rather than after completely reading the file you will need to be able to shift all contents coming after desired insertion location in all arrays to make room for the new entry. Having said that however, it might be easier than sorting post insertion. Your call.

In the end, however, I suspect the exercise is either to get you used to working with arrays
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 23
Reputation: angel22 is an unknown quantity at this point 
Solved Threads: 0
angel22 angel22 is offline Offline
Newbie Poster

Re: Please I need help formatting an output file and sorting the names!

 
0
  #10
Aug 3rd, 2005
Do you mean that I can open the first file and once opened I can insert the ID, first name, last name, 10 scores, for each of the 17 students in different arrays, and after that sort them alphabetically?

So, after sorting calculate average and letter grade?

Or, first calculate average and letter grades and then sort the names!

That is what confuses me because I do not know if first I have to read it and process them and then sort them or viceversa!

Now, i have them all formatted and average processed, and letter grade too.

But, i did not put names into arrays, so I do not know if i have to redo the whole project, or if I can work from what I have!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC