Brain cramp

Something like this should be dead simple. I can't believe it's not working. Here is my code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<string> v;
    string s;

    while( cin >> s );
        v.push_back(s);

    for( string str : v )
        cout << str << " ";

    return 0;
}


As you can see, all I want is to input a few strings and print them back out. So I input my strings, separated by <enter> and terminate input with a ctrl-z. But only the last string I enter is being printed.
You have a semicolon at the end of line 12, so line 13 will never be reached until there is no more input.

Always use curly braces.
Last edited on
Like LB said. ALWAYS use curly brackets, even if it's only one statement. It makes the code way clearer to read and you will make less mistakes.

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

using namespace std;

int main()
{
    vector<string> v;
    string s;

    while( cin >> s )
    {
        v.push_back(s);
    }
    
    for( string str : v )
    {
        cout << str << " ";
    }

    return 0;
}
Last edited on
lol thanks
Topic archived. No new replies allowed.