scanning string in c++ [duplicate] - c++

This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 2 years ago.
#include<bits/stdc++.h>
using namespace std;
int main() {
int test;
cin>>test;
for(int i=0;i<test;i++){
string name;
getline(cin,name);
cout<<name<<"\n";
}
}
input
2
Pratik Patil
Niranjan shirdhone
output:
Pratik Patil
why this code not scanning string for second test case?

When using the std::cin >> test syntax, a newline is always appended at the end as soon as Enter key is pressed. The leading newline inhibits the expected functionality of your program, it follows that it must be skipped or ignored somehow.
That's why std::getline() ignores the input. You need to discard that newline character. A simple solution to that is to call std::cin.ignore() after the the first extraction:
cin >> test;
cin.ignore();
...

Related

Why does getline() overwrite the content of my string? [duplicate]

This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 1 year ago.
I'm trying to figure out why when I read my string using getline() function my string gets empty
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
getline(cin,s);
cout << s;
}
The getline reads characters from an input stream and places them into a string.
If you use "cin >> s" and then use getline(cin, s), you should first call cin.ignore() before it.
Because "std::getline()" performs the following:
Calls s.erase()
Extracts characters from input and appends them to s
If no characters were extracted for whatever reason, getline sets failbit and returns.
You should modify your code like below:
string s;
cin >> s;
cin.ignore();
std::getline(std::cin, s);

isalpha() function not working for spaces in string [duplicate]

This question already has answers here:
C++: Why does space always terminate a string when read?
(4 answers)
Closed 2 years ago.
I wrote a code so that it removes everything(like spaces and other things) other than the alphabats using isalpha() function and converts it to lower case using tolower() function. It is working fine if i don't put a space in the string but if there is any space in the string then it go beyond the space. I dont understand why this is happening. This is the code i wrote.
#include<bits/stdc++.h>
#include<cstring>
#include<cctype>
using namespace std;
int main()
{
int i;
string A,b="";
cin>>A;
for(i=0;i<A.size();i++)
{
if(isalpha(A[i]))
b+= tolower(A[i]);
else
continue;
}
cout<<b;
}
Please help me.
Thankyou
The cin >> A; considers the space to terminate the input.
To get the whole line, use getline(cin, A);
cin reads the string till the first space it encounters, if your input string is "Hello World", then cin will only read "Hello".
You can use getline function to read a complete line.

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

How to use cin and getline properly in a loop? [duplicate]

This question already has answers here:
std::getline on std::cin
(3 answers)
Closed 7 years ago.
I want to write a program which reads in a line and a character and then print them together iterally. This is my code:
#include <string>
#include <iostream>
using namespace std;
int main() {
string s;
string a;
while (getline(cin,s)) {
cin>>a;
cout<<s<<a<<endl;
}
}
The first time I input:"abc d" as a line and then "a" as a character and
the output is "abc da".
But then I input "abc d" again, it immediately output "abc" without waiting for me to input "a" and then output "abc da". Where is my code wrong?
After the cin>>a; there is still a newline in the buffer which is then read by the getline(). To avoid this you need to flush the newline out of the buffer. This can be achieved by calling cin.ignore() and in some cases cin.sync(), although I am not sure of the exact functionality of the cin.sync() function.

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().