Removing letter code gone wrong (C++) - c++

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;
}

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, '.');

Related

I have three getlines, but once I compile, there are too many inputs

#include <iostream>
#include <string>
#include <sstream>
#include <ctype.h>
using namespace std;
int main()
{
string team1, team2, temp;
int Days;
cout << "Days: ";
cin >> Days;
int teamScore1[Days];
int teamScore2[Days];
cin.clear();
cin.ignore();
cout << "!";
getline(cin, team1);
cin.clear();
cin.ignore();
cout << "#";
getline(cin, team2);
cin.ignore();
int i = 0;
while(team1.length() > 0)
{
temp = team1.substr(0, team1.find(" "));
stringstream(temp) >> teamScore1[i];
i++;
}
I'm just testing this code out, I have one cin, and 2 getlines but when I compile and run, there are more input prompts than I'm expecting. Thanks for the help in advance and sorry for the vague question, I don't really understand it enough to explain it.
the problem is the cin.ignore() after the getline. If you remove both cin.ignore() the problem will be solved.
More Detailed
If you check the c++ site for isstream::ignore it bascially says that cin.ignore() will read a character from the input stream.
cin.ignore(): Extracts characters from the input sequence and discards them, until either n characters have been extracted, or one compares equal to delim.
C++ cin.ignore() Reference
But the getline() will read the newline at the end of your input and discard it. So, when you execute cin.ignore() afterwards, it will expect something and wait therefore for another input.
getline(): If the delimiter is found, it is extracted and discarded C++ getline() Reference
For example if you run the following code:
#include <iostream>
using namespace std;
int main() {
cin.ignore();
cout << "HELLO";
}
It won't print the output until you type in something.
Hope that helps!

String not working with #include <string> and using namespace std

about the code below, string doesn't light up anymore and when I entered "John Smith", only "John" appears, string was working fine for me weeks ago until i tried calling strings function today which didn't work so i tested for a simpler one.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string name;
// Get the user's name
cout << "Please enter your first name: ";
cin >> name;
// Print the greeting
cout << "Hello, " << name << "." << endl;
return 0;
}
string doesn't light up like int
I might be asking at the wrong place but I cant' tell what's the problem, please help :(
To get all the line, use getline(cin, name);
instead of cin >> name;
See http://www.cplusplus.com/reference/string/string/getline/
With std::string's, using std::cin >> someString will only read the first word off the buffer (it will stop at the first whitespace encountered).
Use getline(std::cin, someString) instead to read the entire line.
std::cin gets only characters to first 'white' character, like space, tab or enter.
If you want to read whole line use e.g. getline()
string line;
cin.clear(); //to make sure we have no pending characters in input buffer
getline(cin, line);

Adding cin in while loop to read multiple words from a single string input

q.Write a program that uses an array of char and a loop to read one word at a time
until the word done is entered.The program should then report the number of
words entered (not counting done).
I want the count to increment until done is found in the string. I found a solution for this question online
#include <iostream>
#include <string>
int main()
{
using namespace std;
string word;
string matchword = "done";
int numwords=0;
cout << "Enter words (to stop, type the word done):\n";
cin >> word;
while(word != matchword)
{
cin >> word;//how does it read the next word ????
numwords++;
};
cout << "\nYou entered a total of " << numwords << " words.";
cin.get();
cin.get();
return 0;
}
how does the cin read the next word of the string.My question basically is for someone to explain working of cin and string to me.
You will need a delimiter, in order for you to be able to split the string and get the words within word.
The simplest thing for you to do is to create a word string array and collect the words entered(loop until word(N+1) is "" or null. then you can iterate through the word array and list everyone word and you can get the count of how many words as well.
CIN explanation...(sort of)...
In your case cin is a pen and the string word is a white board.
You use cin to write things on word.
Everytime you use cin, you erase the white board and then write the new word.
http://www.cplusplus.com/doc/tutorial/basic_io/
From the link reference "cin" is used to take the value of input to "word" variable and it replace the value of "word" everytime so if the first word is "dog" word =input="dog"
and if next word is "cat" the word=input="cat" not "dogcat" .Like it replace itselves everytime you press enter. Is this what you want from explanation?
Hope you understand
Poom
Every time in side the while loop you ask from the user to enter a word and cin replays the content of the word variable.

Full String is not showing in C++ program

I have following Simple program to print string in C++, But this program only reads characters before space, not reading full string.
#include<iostream>
using namespace std;
int main()
{
char str[90];
cout << "Enter a string:";
cin >> str;
cout << str;
system("pause");
}
This is by design: cin "breaks" lines on whitespace characters, such as spaces and tabs.
Moreover, you are limiting the input to 90 characters, which is not good either: typing more than 90 characters with no spaces in between would overflow the buffer.
Here is a way to fix it:
std::string str;
std::cout << "Enter a string: ";
std::getline(std::cin, str);
Unlike character arrays, std::string objects can grow dynamically, so they would accommodate any number of characters the user chooses to enter.
You need to add two headers in order for this to compile:
#include <string>
#include <iostream>
>> reads a single word. You want getline to read a whole line:
cin.getline(str, sizeof str);
Now the problem is that the line will be truncated if it's too long. To fix that, use a string rather than a fixed-size buffer:
string str;
getline(cin, str);

whitespace identification in c++

My code has to identify whitespace characters using cin, so when I use space as an input it should identify the space. How do I do this?
You can use std::noskipws to disable the whitespace skipping that std::cin does by default:
#include <iostream>
#include <iomanip>
int main() {
char c;
std::cin >> std::noskipws;
while (std::cin >> c) {
if (c == ' ')
std::cout << "A space!" << std::endl;
}
return 0;
}
string str;
getline(cin, str); // get the whole line
If you want to deal with c-strings you could use the mentioned cin.getline(....) which is different from strings getline.
Cin breaks on whitespace, of any kind. If you need to read an entire line, you need to use the get line function:
getline(cin, line);
Where line is a std::string. This will still cut off any new lines or carriage returns.
To test the string for spaces examine every character in the string and compare it to the space character " ". That is left as an exercise for the reader ;)
Use cin.getline to read the line with the space.
http://www.cplusplus.com/reference/iostream/istream/getline/