-2

I'm trying to make a c++ program that reads a line, UNTIL \n, and keeps every word in a vector<string> something;

I tried a lot of things like

vector<string> something;
char buffer[100];
 while(scanf("%s",buffer) != '\n')
 {
        if(strcmp(buffer, ' ')
        something.push_back(buffer);
 }

but nothing works. Some help please?

desoares
  • 861
  • 7
  • 15
Andrew T
  • 79
  • 1
  • 1
  • 9
  • 1
    while(getline(cin, buffer) { something.push_back(buffer); } You're using C++, use C++. You also need to include iostream and use name space std. – thethiny May 03 '18 at 20:05
  • @Andrew T Please accept an answer if your problem was solved. Click the checkbox next to the answer that helped you the most. –  May 08 '18 at 09:16

2 Answers2

3

You can use std::getline() to get a whole line:

#include <iostream>
#include <vector>
#include <string>

int main() {
    const std::string exitString = "exit";
    std::vector<std::string> lines;

    std::string inp;
    while (inp != exitString) {
        std::getline(std::cin, inp);
        if(inp != exitString)
            lines.push_back(inp);
    }

    //print received input and exit
    std::cout << "\n\nLines recorded (" << lines.size() << "):\n";
    for (auto str : lines)
        std::cout << str << "\n";

    std::cin.get();
    return 0;
}

With a few rounds of arbitrary input the program outputs the lines stored in the vector:

Lines recorded (6):
Hello World!
I  a m  v e r y  s c a t t e r e d
123 321 456 654 7 8 9  9 8 7
A B C
A  B  C
A   B   C

And since you mention "keeping words in a vector" - Here's one way (add this to the code above):

//separate to words
std::vector<std::string> words;
for (auto str : lines) {
    std::string word;
    for (auto c : str) {
        if(c != ' ')
            word += c;
        else {
            words.push_back(word);
            word = "";
        }
    }
    if (word != "") 
        words.push_back(word);
}
  • OP asks to read until newline character, not the entire file. (At least that's what I've understood and written as the answer.) In the end of the day he got an answer for both of the situations. If @AndrewT wants to read the file from the file he can also use `while(fileoperator >> stringname)` to read word by word and just push each back. Which is an easier way to solve this. – Tuğberk Kaan Duman May 03 '18 at 20:32
  • @TuğberkKaanDuman Why are you assuming that he intends to read from file? The `scanf()` he uses is directed to keyboard/console input by default, and I don't see a direct or indirect reference to files in his question. Regarding the question of "lines", I'm not sure. But this is what I think he meant (considering the unintuitive behaviour of e.g. `cin` when reading whitespaces...). –  May 03 '18 at 20:46
  • That's why we need this: https://stackoverflow.com/help/mcve question was unclear. – Tuğberk Kaan Duman May 03 '18 at 20:54
1

What I did here is, read the file char by char. Upon seeing a newline char \n broke the reading process and written the final word. Until seeing a space kept adding characters to a string named str. Upon seeing the space, pushed the str into the vector and cleared the str to refill it on next loop.

This just keeps repeating until it sees a new line character. At the end I printed vector contents on screen. I've provided example file binStr.txt that I've used and the output below.

I hope this helps you.

#include <string>
#include <vector>
#include <fstream>
#include <iostream>

int main()
{
    std::vector <std::string> words;
    std::string str;

    std::ifstream stackoverflow("binStr.txt");
    char c;
    while (stackoverflow.get(c))
    {
        str += c;
        if(c == '\n')
        {
            words.push_back(str);
            str.clear();
            break;
        }
        if(c == ' ')
        {
            words.push_back(str);
            str.clear();
        }
    }
    stackoverflow.close();

    for (unsigned int i = 0; i < words.size(); ++i)
        std::cout << "Word: " << words[i] << "\n";

    return 0;
}

File content:

test some more words until new line
hello yes
maybe stackoverflow potato

Result:

Word: test
Word: some
Word: more
Word: words
Word: until
Word: new
Word: line