Thread: My program wont put lines into a vector

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    5

    Post My program wont put lines into a vector

    I'm trying to make a program that puts the lines in a file into a vector. For some reason its only putting the first line into the vector:
    Code:
    #include<iostream>
    #include<string>
    #include<vector>
    #include<algorithm>
    #include<cmath>
    
    
    #include<sstream>
    #include <fstream>
    
    
    using namespace std;
    
    
    
    int main()
    {
        string temp;
        istringstream line;
        vector<string> vline;
        ifstream iFile{"file.txt"};
    
    
        while(getline(iFile,temp,'\n')){
            line.str(temp);
            for(string s;line>>s;){vline.push_back(s);}
    
    
        }
        for(auto &e:vline)
            cout<<e;
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,404
    Instead of:
    Code:
    line.str(temp);
    Try:
    Code:
    istringstream line(temp);
    Note that this doesn't put the lines in the vector; it puts "words" extracted from the lines into the vector. If that's your aim then you might not need to read line by line in the first place.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2019
    Posts
    5
    Thx it works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help, my program wont run!
    By Avocado4Life in forum C Programming
    Replies: 6
    Last Post: 02-09-2011, 05:55 PM
  2. count how many lines, allocate vector, and read again
    By patiobarbecue in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2009, 07:18 PM
  3. sob.. until now my program wont run...
    By lesrhac03 in forum C Programming
    Replies: 4
    Last Post: 03-31-2008, 11:38 AM
  4. MS Visual C++ 6 wont include vector.h in a header file
    By bardsley99 in forum C++ Programming
    Replies: 9
    Last Post: 11-06-2003, 12:05 PM
  5. my program wont read vector array
    By MKashlev in forum C++ Programming
    Replies: 5
    Last Post: 08-10-2002, 03:51 PM

Tags for this Thread