data structure >> sort

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

Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

data structure >> sort

 
0
  #1
Apr 14th, 2008
I have this programming but I want to sort the total in descending order...
How to make an examid together with total after has sort the output?
  1.  
  2.  
  3. #include <iostream> // std::cout
  4. #include <fstream>
  5. #include <iomanip>
  6. #include <string> // std::string
  7. #include <vector> // std::vector<>
  8. #include <algorithm> //std::for each()
  9. using namespace std; // import "std" namespace into global namespace
  10.  
  11. struct exam
  12. {
  13. int examid;
  14. vector <int> total;
  15. };
  16.  
  17. int main()
  18. {
  19. ifstream stream1("STA83SOLUTION.txt");
  20. if ( !stream1.is_open())
  21. {
  22. cout << "While opening a file an error is encountered" << endl;
  23. }
  24. else
  25. {
  26. cout << "Fail Di buka....." << endl;
  27. }
  28. vector <exam> exams;
  29. exam aExam;
  30. int tempExamID;
  31. int tempTotal;
  32. stream1 >> tempExamID >> tempTotal;
  33. aExam.examid = tempExamID;
  34. aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
  35. while (stream1 >> tempExamID >> tempTotal)
  36. {
  37. if(tempExamID != aExam.examid)
  38. {
  39. exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector
  40. aExam.total.clear();
  41. aExam.examid = tempExamID;
  42. }
  43. aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
  44. }
  45. exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector
  46. stream1.close(); // We have read the entire file, so time to close it.
  47. {
  48. ofstream myfile;
  49. myfile.open("411.txt");
  50. int temp;
  51. if (myfile.is_open())
  52. {
  53. for (size_t i = 0; i < exams.size(); i++)
  54. {
  55. for (size_t j = 0; j<exams.at(i).total.size(); j++)
  56.  
  57. cout<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
  58.  
  59. }
  60. }
  61.  
  62. cin.get();
  63. return 0;
  64. }
  65. }
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