Concatenating the elements of a vector in a single string

Hi Everybody,

I have started learning c++ with the help of "thinking in c++" I am stuck at the 6th exercise of it, which asks to concatenate all the elements of a vector into a single string. based on my learning till now I have written the following code but it doesn't seem to work, could someone please explain? It got compiled properly but doesn't show anything on screen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
#include<string>
#include<fstream>
#include<vector>

using namespace std;

int main()
{
    int i; 
    vector <string> v;
    string line;
    string s;
    ifstream in("readme.cpp");
    while(getline(in,line))
    {
      v.push_back(line);
    }
    for (i=0;i<v.size();i++)
    {
        s += v[i];
    }
    cout << s << endl;
    system("pause");
    return 0;
}
Last edited on
Try checking a couple of things:
a) did the file open successfully at line 14
b) what is the value of v.size() just after line 18
Check if file is opened properly.
It looks like there was an error with file, so no data were read.
Also Output vector size before handling string to see if there is actual data in it.
Great minds...
Thanks a lot to both of you, it's working now , previously the file was not opened so it didn't show any output. I copied it to some other directory and created a new project(I could not find the old one) and it started working as usual :)
Topic archived. No new replies allowed.