This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 4 years ago.
In this C++ code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
string S;
getline(cin,S);
cout<<S;
return 0;
}
It prints the string with spaces, eg:
Input:
abc def
Output:
abc def
However, in this code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
string S;
getline(cin,S);
cout<<S;
return 0;
}
It does not print any output, eg:
Input:
1
abc def
Output:
How is that even possible? Please help. Where am I going wrong?
You read a number and then a line. Think carefully about what is going on, a newline is not part of a number. So if the input is 123 newline, you read the number 123 and then getline reads the newline. Whatever is on the next line hasn't been read at all.
Related
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();
...
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.
This question already has answers here:
How to read from std::cin until the end of the stream?
(2 answers)
How to break loop by Enter (c++)?
(3 answers)
End of File(EOF) of Standard input stream (stdin)
(6 answers)
Closed 5 years ago.
I was trying to take input variable number of strings and numbers.Found this solution link:
I tried this for numbers:
#include<iostream>
using namespace std;
int main(){
int np;
while (cin>>np){
cout << np<<endl;
}
return 0;
}
for strings:
#include<iostream>
#include<string>
using namespace std;
int main(){
string line;
while (getline(cin, line)){
cout << line <<endl;
}
return 0;
}
But,when I run the code,even if I just press enter it doesn't come out of the loop.The loop should terminate if just an enter key is pressed,but that does not happen.
Please provide suggestions how to achieve this functionality.
You can write
while (std::getline(std::cin, line) && !line.empty()) {
// ...
}
Only keep looping when the fetched string is nonempty.
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.
This question already has answers here:
How to cin Space in c++?
(8 answers)
Closed 7 years ago.
i have a cpp code
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string s,s1;
ofstream f("new.txt");
cin>>s;
f<<s;
f.close();
ifstream f1("new.txt");
while(!f1.eof())
{
f1>>s1;
cout<<s1;
}
return 0;
}
if i give the input string as "MAGICAL STRING WITH SPACES" . The ofstream object only writes "MAGICAL" into the textfile, what do i do to write and read whitespaces from string variables into an output file stream ?
This is because you use cin.
Try getline() instead!
string newArgument = "";
getline(cin, newArgument);