Which returns an integer, probably unsigned (or more exactly, std::size_t). It's not a reference so it can't be assigned...
soo...I must chage size() whit what?
Code:if (exams.at(i).total.size() < exams.at(i).total.size()+1) // ascending order simply changes to < { temp = exams.at(i).total.size(); // swap elements exams.at(i).total.size() = exams.at(i).total.size()+1; exams.at(i).total.size()+1 = temp; flag = 1; // indicates that a swap occurred. }
It depends on what you want to swap, but I am guessing that you just want to swap exams.at(i).total with exams.at(i+1).total
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i do like this but error...
C:\Documents and Settings\ashida\My Documents\MASTER C++\main\412.cpp(68) : error C2106: '=' : left operand must be l-value
Code:#include <iostream> // std::cout #include <fstream> #include <iomanip> #include <string> // std::string #include <vector> // std::vector<> #include <algorithm> //std::for each() using namespace std; // import "std" namespace into global namespace struct exam { int examid; vector <int> total; }; void SwapMembers (int items[], int index1, int index2) { int temp; temp=items[index1]; items[index1]=items[index2]; items[index2]=temp; } int main() { ifstream stream1("STA83SOLUTION.txt"); if ( !stream1.is_open()) { cout << "While opening a file an error is encountered" << endl; } else { cout << "Fail Di buka....." << endl; } vector <exam> exams; exam aExam; int tempExamID; int tempTotal; stream1 >> tempExamID >> tempTotal; aExam.examid = tempExamID; aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes while (stream1 >> tempExamID >> tempTotal) { if(tempExamID != aExam.examid) { exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector aExam.total.clear(); aExam.examid = tempExamID; } aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes } exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector stream1.close(); // We have read the entire file, so time to close it. { ofstream myfile; myfile.open("411.txt"); int temp, flag; if (myfile.is_open()) { for (size_t i = 0; i < exams.size(); i++) { for (size_t j = 0; j<exams.at(i).total.size(); j++) { if (exams.at(i).total.size() < exams.at(i).total.size()+1) // ascending order simply changes to < { temp = exams.at(i).total.size(); // swap elements exams.at(i).total.at(j) = exams.at(i).total.at(j)+1; exams.at(i).total.at(j)+1 = temp; flag = 1; // indicates that a swap occurred. } cout<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student cout<<" "<< exams.at (i).total.at(j)<<"\t"; } } } cin.get(); return 0; } }
Do you know what is the difference between exams.at(i).total.at(j)+1 and exams.at(i).total.at(j+1)?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
No i dont know the diffreence (j)+1 and (j+1)...
oo..i understand now..but i change (j_)+1 with (j+1) but its abnormal program.
exams.at(i).total.at(j) gives you the int at the jth position in the total vector member of the exam at the ith position in the exams vector (that was quite a mouthful).
exams.at(i).total.at(j)+1 is the sum of that int and 1.
exams.at(i).total.at(j+1) is the next int after that int.
Clearly, you cannot swap that int with the sum of itself and 1. But you can swap that int with the next int.
A std::out_of_range exception would be thrown for the last element, since at the last index, there is no next element.oo..i understand now..but i change (j_)+1 with (j+1) but its abnormal program.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i do like this but abnormal
Code:if (exams.at(i).total.at(j) < exams.at(i).total.at(j+1) ) // ascending order simply changes to < { temp = exams.at(i).total.at(j); // swap elements exams.at(i).total.at(j) = exams.at(i).total.at(j+1); exams.at(i).total.at(j+1) = temp; flag = 1; // indicates that a swap occurred. }
are your indexes in the range?
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
indexes in the range? i dont understand
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?
how to use hash and map in this programming?
Code:#include <iostream> // std::cout #include <fstream> #include <iomanip> #include <string> // std::string #include <vector> // std::vector<> #include <algorithm> //std::for each() using namespace std; // import "std" namespace into global namespace struct exam { int examid; vector <int> total; }; void SwapMembers (int items[], int index1, int index2) { int temp; temp=items[index1]; items[index1]=items[index2]; items[index2]=temp; } int main() { ifstream stream1("STA83SOLUTION.txt"); if ( !stream1.is_open()) { cout << "While opening a file an error is encountered" << endl; } else { cout << "Fail Di buka....." << endl; } vector <exam> exams; exam aExam; int tempExamID; int tempTotal; stream1 >> tempExamID >> tempTotal; aExam.examid = tempExamID; aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes while (stream1 >> tempExamID >> tempTotal) { if(tempExamID != aExam.examid) { exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector aExam.total.clear(); aExam.examid = tempExamID; } aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes } exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector stream1.close(); // We have read the entire file, so time to close it. { ofstream myfile; myfile.open("411.txt"); int temp, flag; if (myfile.is_open()) { for (size_t i = 0; i < exams.size(); i++) { for (size_t j = 0; j<exams.at(i).total.size(); j++) { if (exams.at(i).total.at(j) < exams.at(i).total.at(j+1) ) // ascending order simply changes to < { temp = exams.at(i).total.at(j); // swap elements exams.at(i).total.at(j) = exams.at(i).total.at(j)+1; exams.at(i).total.at(j)+1 = temp; flag = 1; // indicates that a swap occurred. } cout<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student cout<<" "<< exams.at (i).total.at(j)<<"\t"; } } } cin.get(); return 0; } }
if i or j+1 are out of range - at will through an exeption,
you can wrap your code in try/catch block and printout the exeption description to be sure, or just reread your code to see if you step out of the size of the vector
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
you continue your work even if file failed to open?
so what it will make of your j+1 index?Code:j<exams.at(i).total.size()
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
So I must change it to exams.at(i).total.size()+.....?