Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. inputing string & int from file to array.

inputing string & int from file to array.

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ramper
    wrote on last edited by
    #1

    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?

    D 1 Reply Last reply
    0
    • R Ramper

      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?

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      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

      R 1 Reply Last reply
      0
      • D David Crow

        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

        R Offline
        R Offline
        Ramper
        wrote on last edited by
        #3

        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.

        Z 1 Reply Last reply
        0
        • R Ramper

          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.

          Z Offline
          Z Offline
          Zac Howland
          wrote on last edited by
          #4

          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

          D 1 Reply Last reply
          0
          • Z Zac Howland

            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

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            So in the operator>> method, the Student object that gets passed in is modified (i.e., its setName() and setGrades() methods are called), but the students vector in main() never gets any Student objects added to it. Isn't this what the call to copy() 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

            Z 1 Reply Last reply
            0
            • D David Crow

              So in the operator>> method, the Student object that gets passed in is modified (i.e., its setName() and setGrades() methods are called), but the students vector in main() never gets any Student objects added to it. Isn't this what the call to copy() 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

              Z Offline
              Z Offline
              Zac Howland
              wrote on last edited by
              #6

              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

              D 1 Reply Last reply
              0
              • Z Zac Howland

                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

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                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 the name variable, and the grades are stored in the grades vector. Those then get copied successfully to the Student object. The problem is that the students vector in main() does not get any Student 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

                Z 1 Reply Last reply
                0
                • D David Crow

                  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 the name variable, and the grades are stored in the grades vector. Those then get copied successfully to the Student object. The problem is that the students vector in main() does not get any Student 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

                  Z Offline
                  Z Offline
                  Zac Howland
                  wrote on last edited by
                  #8

                  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

                  D 1 Reply Last reply
                  0
                  • Z Zac Howland

                    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

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Thanks, Zac. So by having copy() read from a stringstream rather than a istream, how did that make a difference? The grades 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

                    Z 1 Reply Last reply
                    0
                    • D David Crow

                      Thanks, Zac. So by having copy() read from a stringstream rather than a istream, how did that make a difference? The grades 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

                      Z Offline
                      Z Offline
                      Zac Howland
                      wrote on last edited by
                      #10

                      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

                      D 1 Reply Last reply
                      0
                      • Z Zac Howland

                        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

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        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

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups