0

im trying to get the data from a string variable and put it into its own place in a string vector but it saves each word as its own point in the vector.

i.e. "buy milk" will result in 0 being buy then 1 being milk not 0 being buy milk.

sorry for not knowing the proper terminology im really new to C++

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

using namespace std;

int main()
{
    string input;
    vector <string> Notes;
    int SaveNoteOnLine = 0;
    string output; //a var needed for specific situations, not used for all output

    cout << "welcome to cmdnotes alpha v1.0\n";
    while (true) {
        cin >> input;
        if (input == "-list") {
            for (int i = 0; i < SaveNoteOnLine; i++) {
                cout << Notes.at(i) << endl;
            }
        }
        else {
            Notes.push_back(input);
            cout << "saved on line " << SaveNoteOnLine << endl;
            SaveNoteOnLine++;
        }
    }
}
  • 4
    https://stackoverflow.com/questions/10464344/reading-from-stdin-in-c – armagedescu Feb 13 '21 at 15:37
  • 1
    `cin >> input;` will stop at any whitespace character. – drescherjm Feb 13 '21 at 15:39
  • 1
    Because that's how [`cin >> string`](https://en.cppreference.com/w/cpp/string/basic_string/operator_ltltgtgt) is supposed to work. If your really new, it might be a good idea to find a [good book](https://stackoverflow.com/q/388242/212858). – Useless Feb 13 '21 at 15:40
  • thanks for the help, i should probably find a good book about it yeah, is there any alternative to std::cin that doesnt stop at a whitespace character. – tactical waffle Feb 13 '21 at 15:42
  • 1
    you can use getline(cin,input) to get the string with whitespaces – Rahul Khanna Feb 13 '21 at 15:42
  • @tacticalwaffle, refer the first comment. – Kishore Feb 13 '21 at 15:43
  • 1
    If you want a good book you can look here: [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Blastfurnace Feb 13 '21 at 15:43
  • sorry im a bit dumb, thanks for the book recomendations and help with my code – tactical waffle Feb 13 '21 at 15:45
  • 1
    Does this answer your question? [std::cin input with spaces?](https://stackoverflow.com/questions/5838711/stdcin-input-with-spaces) – JaMiT Feb 13 '21 at 16:10

1 Answers1

1

Use getline(cin,input) inplace of cin>>input it will take whitespaces as input

Rahul Khanna
  • 118
  • 8