Reputation: 13
I was writing this code below that is suppose to take in information from the user ( a sentence ) and remove the desired letter. However, it only works if that sentence is one word. If the information contains a space, it will terminate at the space.Any advice as how I can get the program to read the entire sentence?
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string sentence;
char letterRemoved;
//Entering in Information
cout <<"Enter a sentence here!";
cin >> sentence;
cout <<"Enter letter to remove";
cin>>letterRemoved;
//Removing Occurence of letter
sentence.erase(remove(sentence.begin(), sentence.end(),letterRemoved), sentence.end());
//Print out
cout<<sentence << "\n";
return 0;
}
Upvotes: 0
Views: 63
Reputation: 3764
Reading input using cin >> sentence
only reads until whitespace is encountered, and then stops. If you want to read an entire line (until the user presses enter), you want to use std::getline
:
getline(cin, sentence);
Alternately, if you want to read up until a full stop character or a newline is found, you can use the delimeter
argument to getline
:
getline(cin, sentence, '.');
Upvotes: 4