Im doing exercices from c++ primer, and trying to do a program that recieves as input a word and a line. If when i ask for a word (with cin) I press enter, then the program just skips the next lines and doest ask for a line (with the getline)... and if i write an entire phrase in the cin (like "hello beautifull world") then the first word ("hello") is captured by the cin and the others two words ("beautifull world") by the getline.
i understand that in the cin, when i input a space, it cuts the input. what i dont uderstand are two things:
1.- why if i end the input (in the cin) with enter it skips all the rest of code? (is there a solution for that?)
2.- why if i write an entire phrase in the cin, it assign the other two word to the getline before to execute the cout << "enter a line" << endl; ?
Thx! sorry for my english C:
#include <iostream>
#include <string>
using namespace std;
int main() {
string word, line;
cout << "enter a word" << endl;
cin >> word;
cout << "enter a line" << endl;
getline(cin, line);
cout << "your word is " << word << endl;
cout << "your line is " << line << endl;
return 0;
}