Issue
Why this doesn’t print the first word of sentence?
#include <iostream>
#include <string>
int main()
{
std::string sentence;
std::cout<<"Enter sentence: ";
std::cin>>sentence;
std::getline(std::cin,sentence);
std::cout<<sentence;
return 0;
}
If I enter
"This is text"
output would be
" is text"
Solution
You dont need the first cin (std::cin>>sentence;), this will solve your problem
#include <iostream>
#include <string>
int main()
{
std::string sentence;
std::cout<<"Enter sentence: ";
std::getline(std::cin,sentence);
std::cout<<sentence;
return 0;
}
Answered By – LoopFree
Answer Checked By – Senaida (BugsFixing Volunteer)