inputing string & int from file to array.
-
-
Have a name followed by 10 int's and it repeats for 15 names. Have a program that will input just the int into a 2d array if no string is involved. Need to somehow input the names from the file (every 11 lines) into a seperate array. Any suggestions?
Ok, so what part is troubling you? Be specific now. No teasing!
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Ok, so what part is troubling you? Be specific now. No teasing!
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
File i have the code for is like: 34 34 45 32 .... File i need to be able to get grades from is: Name 35 54 34 44 34 54 34 54 34 54 Name 34 34 ... Need help on how to somehow get the name strings into an array somehow. As i have it now it is just inputFile >> data[students][tests] which is a declared 2d array with 15 students and 10 tests.
-
File i have the code for is like: 34 34 45 32 .... File i need to be able to get grades from is: Name 35 54 34 44 34 54 34 54 34 54 Name 34 34 ... Need help on how to somehow get the name strings into an array somehow. As i have it now it is just inputFile >> data[students][tests] which is a declared 2d array with 15 students and 10 tests.
This is a starting place (it isn't complete, but it should get you started):
#include <string> #include <vector> #include <algorithm> #include <interator> #include <iostream> #include <fstream> class Student { public: Student() : _Name("") {} ~Student() {} void setName(const std::string& name) { _Name = name; } std::string& getName() const { return _Name; } void setGrades(const std::vector<int>& grades) { _Grades.assign(grades.begin(), grades.end()); } std::vector<int>& getGrades() const { return _Grades; } private: std::string _Name; std::vector<int> _Grades; }; std::ostream& operator<<(std::ostream& os, const Student& s) { os << s.getName() << " "; // NOTE: older versions of STL will require a character buffer instead const std::vector<int> grades = s.getGrades(); std::copy(grades.begin(), grades.end(), std::ostream_iterator<int>(os, " ")); os << std::endl; return os; } std::istream& operator>>(std::istream& is, Student& s) { string name = ""; // NOTE: older versions of STL will require a character buffer instead is >> name; std::vector<int> grades; std::copy(std::istream_iterator<int>(is), std::istream_iterator<int>(), std::back_inserter(grades)); s.setName(name); s.setGrades(grades); return is; } int main() { std::ifstream fin; std::vector<Student> students; fin.open("MyData.txt"); std::copy(std::istream_iterator<Student>(fin), std::istream_iterator<Student>(), std::back_inserter(students)); fin.close(); // do whatever you want with students vector }
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
This is a starting place (it isn't complete, but it should get you started):
#include <string> #include <vector> #include <algorithm> #include <interator> #include <iostream> #include <fstream> class Student { public: Student() : _Name("") {} ~Student() {} void setName(const std::string& name) { _Name = name; } std::string& getName() const { return _Name; } void setGrades(const std::vector<int>& grades) { _Grades.assign(grades.begin(), grades.end()); } std::vector<int>& getGrades() const { return _Grades; } private: std::string _Name; std::vector<int> _Grades; }; std::ostream& operator<<(std::ostream& os, const Student& s) { os << s.getName() << " "; // NOTE: older versions of STL will require a character buffer instead const std::vector<int> grades = s.getGrades(); std::copy(grades.begin(), grades.end(), std::ostream_iterator<int>(os, " ")); os << std::endl; return os; } std::istream& operator>>(std::istream& is, Student& s) { string name = ""; // NOTE: older versions of STL will require a character buffer instead is >> name; std::vector<int> grades; std::copy(std::istream_iterator<int>(is), std::istream_iterator<int>(), std::back_inserter(grades)); s.setName(name); s.setGrades(grades); return is; } int main() { std::ifstream fin; std::vector<Student> students; fin.open("MyData.txt"); std::copy(std::istream_iterator<Student>(fin), std::istream_iterator<Student>(), std::back_inserter(students)); fin.close(); // do whatever you want with students vector }
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
So in the
operator>>
method, theStudent
object that gets passed in is modified (i.e., itssetName()
andsetGrades()
methods are called), but thestudents
vector inmain()
never gets anyStudent
objects added to it. Isn't this what the call tocopy()
is supposed to be doing?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
So in the
operator>>
method, theStudent
object that gets passed in is modified (i.e., itssetName()
andsetGrades()
methods are called), but thestudents
vector inmain()
never gets anyStudent
objects added to it. Isn't this what the call tocopy()
is supposed to be doing?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
It should be getting 1 student only (due to the previously mentioned bug in that example). Fixing the bug will allow it to copy all the students in the file to the vector. The easiest way to do this without having to modify the data file format is to modify the input stream extractor to get the id and then calling getline to store the rest of the line as a string. Then create a stringstream object and extract the grades (everything in the streamstream) into the grades vector using a copy operation. Basically, you add 2 lines of code and change the iterators for the grades-copy call.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
It should be getting 1 student only (due to the previously mentioned bug in that example). Fixing the bug will allow it to copy all the students in the file to the vector. The easiest way to do this without having to modify the data file format is to modify the input stream extractor to get the id and then calling getline to store the rest of the line as a string. Then create a stringstream object and extract the grades (everything in the streamstream) into the grades vector using a copy operation. Basically, you add 2 lines of code and change the iterators for the grades-copy call.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
I'm still learning this C++ stream stuff, so I don't think we are on the same page here, Zac. For an input file having a format of: bob 1 2 3 4 5 mary 2 3 4 5 6 7 nathan 9 8 7 6 The first line of the file is read correctly by the
operator>>
method. The name is stored in thename
variable, and the grades are stored in thegrades
vector. Those then get copied successfully to theStudent
object. The problem is that thestudents
vector inmain()
does not get anyStudent
objects added to it.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
I'm still learning this C++ stream stuff, so I don't think we are on the same page here, Zac. For an input file having a format of: bob 1 2 3 4 5 mary 2 3 4 5 6 7 nathan 9 8 7 6 The first line of the file is read correctly by the
operator>>
method. The name is stored in thename
variable, and the grades are stored in thegrades
vector. Those then get copied successfully to theStudent
object. The problem is that thestudents
vector inmain()
does not get anyStudent
objects added to it.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Here is a working example with the data format you gave:
#include <string> #include <vector> #include <algorithm> #include <iterator> #include <iostream> #include <fstream> #include <sstream> using namespace std; class Student { public: Student() : _Name("") {} ~Student() {} void setName(const string& name) { _Name = name; } string getName() const { return _Name; } void setGrades(const vector& grades) { _Grades.assign(grades.begin(), grades.end()); } vector getGrades() const { return _Grades; } private: string _Name; vector _Grades; }; std::ostream& operator<<(std::ostream& os, const Student& s) { os << s.getName() << " "; // NOTE: older versions of STL will require a character buffer instead const vector grades = s.getGrades(); copy(grades.begin(), grades.end(), ostream_iterator(os, " ")); os << std::endl; return os; } std::istream& operator>>(std::istream& is, Student& s) { string name = ""; // NOTE: older versions of STL will require a character buffer instead is >> name; string sGrades = ""; getline(is, sGrades); vector grades; stringstream ss(sGrades); copy(istream_iterator(ss), istream_iterator(), back_inserter(grades)); s.setName(name); s.setGrades(grades); return is; } int main() { ifstream fin; vector students; fin.open("data.txt"); copy(istream_iterator(fin), istream_iterator(), back_inserter(students)); fin.close(); // do whatever you want with students vector std::copy(students.begin(), students.end(), ostream_iterator(cout, "\n\n")); }
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
Here is a working example with the data format you gave:
#include <string> #include <vector> #include <algorithm> #include <iterator> #include <iostream> #include <fstream> #include <sstream> using namespace std; class Student { public: Student() : _Name("") {} ~Student() {} void setName(const string& name) { _Name = name; } string getName() const { return _Name; } void setGrades(const vector& grades) { _Grades.assign(grades.begin(), grades.end()); } vector getGrades() const { return _Grades; } private: string _Name; vector _Grades; }; std::ostream& operator<<(std::ostream& os, const Student& s) { os << s.getName() << " "; // NOTE: older versions of STL will require a character buffer instead const vector grades = s.getGrades(); copy(grades.begin(), grades.end(), ostream_iterator(os, " ")); os << std::endl; return os; } std::istream& operator>>(std::istream& is, Student& s) { string name = ""; // NOTE: older versions of STL will require a character buffer instead is >> name; string sGrades = ""; getline(is, sGrades); vector grades; stringstream ss(sGrades); copy(istream_iterator(ss), istream_iterator(), back_inserter(grades)); s.setName(name); s.setGrades(grades); return is; } int main() { ifstream fin; vector students; fin.open("data.txt"); copy(istream_iterator(fin), istream_iterator(), back_inserter(students)); fin.close(); // do whatever you want with students vector std::copy(students.begin(), students.end(), ostream_iterator(cout, "\n\n")); }
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
Thanks, Zac. So by having
copy()
read from astringstream
rather than aistream
, how did that make a difference? Thegrades
vector had the correct number of items added to it either way.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Thanks, Zac. So by having
copy()
read from astringstream
rather than aistream
, how did that make a difference? Thegrades
vector had the correct number of items added to it either way.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Putting the grades in a stringstream prevents the istream from trying to read the rest of the file. When the istream gets to the end of the file (that is, when the >> returns an eof()) it doesn't add anything to the vector (which is what you were seeing).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
Putting the grades in a stringstream prevents the istream from trying to read the rest of the file. When the istream gets to the end of the file (that is, when the >> returns an eof()) it doesn't add anything to the vector (which is what you were seeing).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
That helps. Thanks.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne