| | |
vector to float
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
How to change this data sturucture from vector <int> total; to float total;? because if I change it to float teher is many error....
C++ Syntax (Toggle Plain Text)
#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.size() = exams.at(i).total.size()+1; exams.at(i).total.size()+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; } }
you mean you want the structure to look like this:
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.
C++ Syntax (Toggle Plain Text)
struct exam { int examid; float total; };
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.
initialize it to 0 when you create the array of structures. Something like this
C++ Syntax (Toggle Plain Text)
struct exam array[20]; for(int i = 0; i < 20; i++) 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.
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.
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.
C++ Syntax (Toggle Plain Text)
#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; float total; }; 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++) { size.total = 0; cout<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student } } } cin.get(); return 0; } }
•
•
Join Date: Mar 2008
Posts: 42
Reputation:
Solved Threads: 6
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:
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() '.
C++ Syntax (Toggle Plain Text)
struct exam { int examid; vector <int> examcodelist; float total; };
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() '.
![]() |
Similar Threads
- Random rotation of a random vector (C++)
- reading data from file to vector (C++)
- strstream problem (C++)
- vector, put out sorted list (Java)
- Parallel Resistance (C++)
- Help with a missing ';' (C++)
- Help with vectors and structs (C++)
Other Threads in the C++ Forum
- Previous Thread: Please help with Pointers, Classes, Virtual Functions, and Abstract Classes
- Next Thread: oh man i really need help!
Views: 1342 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






