Function allows word but not Phrase c++ [duplicate] - c++

This question already has answers here:
std::cin input with spaces?
(8 answers)
Closed 3 years ago.
I have a function that allows a user to input a word or phrase then display it on a menu but for some reason it will display a word but not a phrase, system crashes when I use more than one word
snippet of my function code:
string GetWord(void){
string localString = "";
cout<< "please enter a new word or phrase: ";
cin >> localString;
return localString;
}
does anyone know what I have done wrong ? the menu will display a single word but not double.

The input operator >> on std::cin stops reading at the first space and so just reads one word. This is the defined behavior.
I assume you would like to read until the user presses enter. You want to read a line. You can use std::getline() to achieve this:
std::string GetWordOrPhrase() {
std::string localString;
std::cout << "please enter a new word or phrase: ";
std::getline(std::cin, localString);
return localString;
}
BTW: Always use the std:: namespace explicitly in your code. Do not use using namespace std just to spare a few characters. (Code is not (only) about making it work with some compiler, but also a communication means between software developers.)

Related

Problem with std::getline() and std::cin.get() [duplicate]

This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 8 months ago.
can you help me
Why this code can't be swap
cout << "Enter a string: ";
getline(cin, str1);
cout << "Enter another string: ";
cin.get(str, 100, '\n');
Into
cout << "Enter string: ";
cin.get(str, 100, '\n');
cout << "Enter a string: ";
getline(cin, str1);
when i ran
First code
Output :
Enter a string: hai
Enter another string: hello
Second code
Output :
Enter another string: hello
Enter a string:
I can't input anymore, it just directly returned 0
Is it because of delimiters?
std::istream::get leaves the newline character in the stream so if you use std::getline afterwards, it'll directly read that newline character.
You can get rid of it like so:
std::cin.get(str, 100, '\n');
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(std::cin, str1);
Demo
But, it's easier if you don't mix std::getline and std::istream::get.
As you can see in the documentation for std::istream::get:
The delimiting character is not extracted from the input sequence if
found, and remains there as the next character to be extracted from
the stream (see getline for an alternative that does discard the
delimiting character).
I.e. the difference is that std::getline disacrds the newline delimiter character. If you use std::istream::get it stays in the stream buffer, and when you try to extract the 2nd string you will get it into your variable.
Regarding your specific example, it's a better to use either std::getline or std::istream::get consistently, rather than mixing them up.
If you have a good reason to mix them, see how to in #TedLyngmo's answer.

Reading a string from user [duplicate]

This question already has answers here:
Using getline(cin, s) after cin [duplicate]
(13 answers)
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 6 years ago.
#include<iostream>
#include<cstring>
using namespace std;
int main() {
int t;
cin >> t;
string s;
getline(cin, s);
cout << s;
return 0;
}
As soon as I press the enter key after giving input for t, the program terminates as string takes the newline character as input. What can I do? I cannot output something between these two inputs and an enter after t is mandatory.
The Enter key you press to end the input for t is added to the input buffer. So the first input in the buffer seen by getline is the Enter key and it thinks you have given an empty line.
Use the ignore function to skip past the newline:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
You can also use two std::getline calls, and use e.g. std::stoi to convert the input an int. Or put in an std::istringstream and extract using the normal >> operator.
You may use getline(cin, s) twice. Fist will ignore rest of first line and the second will actually read what you need or better use cin.ignore() with appropriate arguments

Why does cin.getline() not work (C++)? [duplicate]

This question already has answers here:
Using getline(cin, s) after cin [duplicate]
(13 answers)
Closed 7 years ago.
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
char text[200];
int input;
cin>>input;
if (input == 1)
{
cin.getline(text, 200);
cout<<text<<"\n";
}
else if(input == 0)
{
cout <<"You entered a 0";
}
return 0;
}
I am trying to make a small program where the user gives an input either a 1 or 0. if the user enters a 1 then he can enter a whole sentence and stores it in the char array of text. My problem is that when I put the cin.getline() inside an if statement it no longer works. why is that?
Thanks
It is not that cin.getline() doesn't work. It does exactly what has been asked of it: Read the line of text up to the next newline. It just so happens that cin >> input; has read some digits and then left the first non-digit input in the input buffer - which typically is a newline unless you typed something that wasn't a number.
You can work around this by calling cin.ignore(), which will "read everything up to the next newline and throw it away".
Ideally, you should decide whether you want to use cin >> or cin.getline(), and use one or the other, but that means then reading a string of text and in your code converting to a digit, and if you are a novice, that's probably a bit more complex than you actually want to make it.

C++ skipping on cin.getline [duplicate]

This question already has answers here:
cin and getline skipping input [duplicate]
(4 answers)
Closed 8 years ago.
i have a problem with input char to my program
#include <iostream>
using namespace std;
int main()
{
int choise;
char word[81];
cin >> choise;
cout << "enter the word:" << endl;
cin.getline(word, 81);
return 0;
}
the visual studio open the input to "choise"
but skip on cin.getline (it the same if i replace it with gets_s).
i tried to write
cin.get(); before the "getline"...
but then the program not get's the first char
(if i put 'aa' it get 'a')
what can i do?
thanks
Its because you entered a newline for the program to accept the integer you entered for choice, the that newline is not extracted from the buffer, leaving it to be read in your next input operation. The getline call reads that left-over newline, and is happy with that.
There are a couple of ways to solve your problem. The first and most obvious is to use std::string for the word variable, and then use the normal input operator >> as that will skip leading whitespace (which includes newline).
Another solution is to tell the input stream to ignore until and including a newline. The linked reference has an example on how to do exactly that.
After entering the integer you press enter, and this enter input is left in the buffer area and used as the next input for getline and the computer assumes you're done.
add this line before getline statement and re- compile it.
cin.ignore().

String Truncated for Some Reason in C++ [duplicate]

This question already has answers here:
Issue with cin when spaces are inputted, using string class
(3 answers)
Closed 8 years ago.
So, I was just starting C++, and I have a problem. I declared a string variable and all that, but when I have input give the variable a value, then try to display it, it only shows the first word of the sentence.
#include <iostream>
#include <string>
using namespace std;
using std::string;
int main()
{
string answer;
cout << "Give me a sentence and I will repeat it!";
cin >> answer;
cout << answer;
return 0;
}
For example, I entered "Yay it worked!", and it outputted "Yay"
The delimiter for std::cin is whitespace, so it only took the first word of your sentence. Like #πάνταῥεῖ said, use std::getline(cin,answer) instead.
As the comment explains, cin will only read until the first bit of whitespace is met (in your case this seems to be a space). Instead, you can use std::getline, which will read until a specified character, or a return by default:
std::string answer;
std::cout << "Give me a sentence and I will repeat it!";
std::getline(std::cin, answer):
std::cout << answer;
To make it read until a specified character would look like:
char end_char = 'a';
std::getline(std::cin, answer, end_char);