vector to float

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
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

vector to float

 
0
  #1
Apr 21st, 2008
How to change this data sturucture from vector <int> total; to float total;? because if I change it to float teher is many error....

  1. #include <iostream> // std::cout
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string> // std::string
  5. #include <vector> // std::vector<>
  6. #include <algorithm> //std::for each()
  7. using namespace std; // import "std" namespace into global namespace
  8.  
  9. struct exam
  10. {
  11. int examid;
  12. vector <int> total;
  13. };
  14.  
  15. void SwapMembers (int items[], int index1, int index2)
  16. {
  17. int temp;
  18. temp=items[index1];
  19. items[index1]=items[index2];
  20. items[index2]=temp;
  21. }
  22.  
  23. int main()
  24. {
  25. ifstream stream1("STA83SOLUTION.txt");
  26. if ( !stream1.is_open())
  27. {
  28. cout << "While opening a file an error is encountered" << endl;
  29. }
  30. else
  31. {
  32. cout << "Fail Di buka....." << endl;
  33. }
  34. vector <exam> exams;
  35. exam aExam;
  36. int tempExamID;
  37. int tempTotal;
  38. stream1 >> tempExamID >> tempTotal;
  39. aExam.examid = tempExamID;
  40. aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
  41. while (stream1 >> tempExamID >> tempTotal)
  42. {
  43. if(tempExamID != aExam.examid)
  44. {
  45. exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector
  46. aExam.total.clear();
  47. aExam.examid = tempExamID;
  48. }
  49. aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
  50. }
  51. exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector
  52. stream1.close(); // We have read the entire file, so time to close it.
  53. {
  54. ofstream myfile;
  55. myfile.open("411.txt");
  56.  
  57. int temp, flag;
  58. if (myfile.is_open())
  59. {
  60. for (size_t i = 0; i < exams.size(); i++)
  61. {
  62. for (size_t j = 0; j<exams.at(i).total.size(); j++)
  63. {
  64. if (exams.at(i).total.size() < exams.at(i).total.size()+1) // ascending order simply changes to <
  65. {
  66. temp = exams.at(i).total.size(); // swap elements
  67. exams.at(i).total.size() = exams.at(i).total.size()+1;
  68. exams.at(i).total.size()+1 = temp;
  69. flag = 1; // indicates that a swap occurred.
  70. }
  71.  
  72. cout<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
  73. cout<<" "<< exams.at (i).total.at(j)<<"\t";
  74. }
  75. }
  76. }
  77.  
  78. cin.get();
  79. return 0;
  80. }
  81. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: vector to float

 
0
  #2
Apr 22nd, 2008
you mean you want the structure to look like this:
  1. struct exam
  2. {
  3. int examid;
  4. float total;
  5. };

You will have to make a lot of code changes to do that because total is no long an array of ints. I suppose instead of push_back(123) just add 123 to the current value of total.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
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

Re: vector to float

 
0
  #3
Apr 22nd, 2008
yes...I want to do like that but

" instead of push_back(123) just add 123 to the current value of total. "

123 ?I dont understand...Ancient Dragon.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: vector to float

 
0
  #4
Apr 22nd, 2008
>>123 ?I dont understand...Ancient Dragon.

That was just a generic thing to mean any number -- whatever number you used in the push_back() function call.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
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

Re: vector to float

 
0
  #5
Apr 22nd, 2008
"
just add 123 to the current value of total. "
Ancient,..
where is teh curernt value of total?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: vector to float

 
0
  #6
Apr 22nd, 2008
initialize it to 0 when you create the array of structures. Something like this
  1. struct exam array[20];
  2. for(int i = 0; i < 20; i++)
  3. array.total = 0;
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
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

Re: vector to float

 
0
  #7
Apr 23rd, 2008
I have done like this.. but still many error..
i have change ti float..then for array.total i change to size.total=0.

rror C2228: left of '.push_back' must have class/struct/union type
F:\460.cpp(37) : error C2228: left of '.clear' must have class/struct/union type
F:\460.cpp(40) : error C2228: left of '.push_back' must have class/struct/union type
F:\460.cpp(53) : error C2228: left of '.size' must have class/struct/union type
F:\460.cpp(56) : error C2228: left of '.total' must have class/struct/union type
F:\460.cpp(57) : error C2228: left of '.at' must have class/struct/union type
Error executing cl.exe.

  1. #include <iostream> // std::cout
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string> // std::string
  5. #include <vector> // std::vector<>
  6. #include <algorithm> //std::for each()
  7. using namespace std; // import "std" namespace into global namespace
  8.  
  9. struct exam
  10. {
  11. int examid;
  12. float total;
  13. };
  14. int main()
  15. {
  16. ifstream stream1("STA83SOLUTION.txt");
  17. if ( !stream1.is_open())
  18. {
  19. cout << "While opening a file an error is encountered" << endl;
  20. }
  21. else
  22. {
  23. cout << "Fail Di buka....." << endl;
  24. }
  25. vector <exam> exams;
  26. exam aExam;
  27. int tempExamID;
  28. int tempTotal;
  29. stream1 >> tempExamID >> tempTotal;
  30. aExam.examid = tempExamID;
  31. aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
  32. while (stream1 >> tempExamID >> tempTotal)
  33. {
  34. if(tempExamID != aExam.examid)
  35. {
  36. exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector
  37. aExam.total.clear();
  38. aExam.examid = tempExamID;
  39. }
  40. aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
  41. }
  42. exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector
  43. stream1.close(); // We have read the entire file, so time to close it.
  44. {
  45. ofstream myfile;
  46. myfile.open("411.txt");
  47.  
  48. int temp, flag;
  49. if (myfile.is_open())
  50. {
  51. for (size_t i = 0; i < exams.size(); i++)
  52. {
  53. for (size_t j = 0; j<exams.at(i).total.size(); j++)
  54. {
  55.  
  56. size.total = 0;
  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  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: vector to float

 
0
  #8
Apr 23rd, 2008
>>I have done like this.. but still many error..
So, fix the errors. After all this time you should know how to recognize the errors and correct them.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 42
Reputation: littlestone is an unknown quantity at this point 
Solved Threads: 6
littlestone littlestone is offline Offline
Light Poster

Re: vector to float

 
0
  #9
Apr 24th, 2008
Why you want to change vector<int> to float? Do you want to use the float total to store the sum of the exam codes? maybe you can add a member to your struct. like this:
  1. struct exam
  2. {
  3. int examid;
  4. vector <int> examcodelist;
  5. float total;
  6. };

In addition, in you source code, you use at() to access vector, but you don't catch the range error exception. so, it make no sense. for performance consider, you'd better use '[]' instead of 'at() '.
Reply With Quote Quick reply to this message  
Reply

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




Views: 1342 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC