getchar function skipped the first character when used after integer - c++

Hello everyone this problem made me crazy
i have to take large line as input T number of times. I use following code
#include <iostream>
#include <string.h>
using namespace std;
int main() {
int T;
char buffer,b1;
cin >> T>> b1;
//fflush(stdin);
while(T--)
{
char ch,c;
int A[14];
while((ch=getchar())!='\n')
{
cout << ch;
}
}
return 0;
}
I know if we taken input character after integer then it store null as a character thats why i take an extra input as b1(as a buffer) so that null will store in it.
when i pass input as
2
758
8758
It gives following output
58
8758

As BLUPIXY suggested in comments replace
cin >> T>> b1;
with
cin >> T; cin.ignore();
It works

Related

How to get string input n times? [duplicate]

This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 3 years ago.
I have a C++ program. I want to get a number from the user (t) and force the user to enter line t times but the program's execution terminates after 1 iteration. This is the code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
int t;
cin >> t;
for (int i=0; i< t; i++) {
getline(cin, str);
cout << str;
}
return 0;
}
Can anyone explain me why this happening and how to solve it?
Thank you my friends.
The newline character is still in the buffer when you do cin >> t so the next line you read will be blank. When you mix formatted input (>>) and unformatted (std::getline) you often get in situations like this and you need to take measures when switching to unformatted input. Example remedy:
#include <iostream>
#include <limits>
#include <string>
using namespace std;
int main() {
string str;
int t;
cin >> t;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // skip the rest of the line
for(int i = 0; i < t; i++) {
if(getline(cin, str)) // check that the getline actually succeeded
cout << str << '\n';
else
break;
}
return 0;
}
When you enter your first character (the times to repeat), a character is left in the cin buffer - newlines are not consumed by cin >>. As a result, getline(cin, str) reads this character and takes it as the first input, which then empties the buffer out and lets you enter the others.
You can clear the buffer with std::cin.ignore(1); to remove that trailing character - this lets your code run as anticipated. Why not just use cin >> str, though? That solves the problem and avoids a call to getline.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
int t;
cin >> t;
//clear one character out of buffer
cin.ignore(1);
//note that 1 is used for demonstration purposes
//in development code, INT_MAX, numeric_limits<streamsize>::max(),
//or some other large number would be best, followed
//by std::cin.clear()
for (int i=0; i< t; i++) {
cout << "input: ";
//you could use cin >> str; instead of getline(cin, str);
getline(cin, str);
cout << "got: " << str << std::endl;
}
return 0;
}
Demo

How to simulate user input to std::cin for a mcve? [duplicate]

If we have this code snippet:
int a;
cout << "please enter a value: ";
cin >> a;
And in the terminal, the input request would look like this
please enter a value: _
How can I programatically simulate a user's typing in it.
Here's a sample how to manipulate cin's input buffer using the rdbuf() function, to retrieve fake input from a std::istringstream
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
istringstream iss("1 a 1 b 4 a 4 b 9");
cin.rdbuf(iss.rdbuf()); // This line actually sets cin's input buffer
// to the same one as used in iss (namely the
// string data that was used to initialize it)
int num = 0;
char c;
while(cin >> num >> c || !cin.eof()) {
if(cin.fail()) {
cin.clear();
string dummy;
cin >> dummy;
continue;
}
cout << num << ", " << c << endl;
}
return 0;
}
See it working
Another option (closer to what Joachim Pileborg said in his comment IMHO), is to put your reading code into a separate function e.g.
int readIntFromStream(std::istream& input) {
int result = 0;
input >> result;
return result;
}
This enables you to have different calls for testing and production, like
// Testing code
std::istringstream iss("42");
int value = readIntFromStream(iss);
// Production code
int value = readIntFromStream(std::cin);
Hey why don't you write your input in a plain text file and redirect it to cin ???
It's the simplest method.
Open Command Prompt.
Suppose your text file which will used as input is in.txt and your program is prog.exe.
Keep the text file and the program in same folder. cd to your folder. Then type:
prog.exe < in.txt
Remember, your text file will be treated exactly as it is. Shoudld't be a problem if you know cin only catches upto next whitespace character, while string input functions (e.g. cin.getline) only catch upto next newline character.
//Sample prog.cpp
#include <iostream>
using namespace std;
int main()
{
int num;
do
{
cin >> num;
cout << (num + 1) << endl;
}
while (num != 0);
return 0;
}
//Sample in.txt
2
51
77
0
//Sample output
3
52
78
1
Sorry if you are on other platform, I don't know about them.

How to simulate user input in c++? [duplicate]

If we have this code snippet:
int a;
cout << "please enter a value: ";
cin >> a;
And in the terminal, the input request would look like this
please enter a value: _
How can I programatically simulate a user's typing in it.
Here's a sample how to manipulate cin's input buffer using the rdbuf() function, to retrieve fake input from a std::istringstream
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
istringstream iss("1 a 1 b 4 a 4 b 9");
cin.rdbuf(iss.rdbuf()); // This line actually sets cin's input buffer
// to the same one as used in iss (namely the
// string data that was used to initialize it)
int num = 0;
char c;
while(cin >> num >> c || !cin.eof()) {
if(cin.fail()) {
cin.clear();
string dummy;
cin >> dummy;
continue;
}
cout << num << ", " << c << endl;
}
return 0;
}
See it working
Another option (closer to what Joachim Pileborg said in his comment IMHO), is to put your reading code into a separate function e.g.
int readIntFromStream(std::istream& input) {
int result = 0;
input >> result;
return result;
}
This enables you to have different calls for testing and production, like
// Testing code
std::istringstream iss("42");
int value = readIntFromStream(iss);
// Production code
int value = readIntFromStream(std::cin);
Hey why don't you write your input in a plain text file and redirect it to cin ???
It's the simplest method.
Open Command Prompt.
Suppose your text file which will used as input is in.txt and your program is prog.exe.
Keep the text file and the program in same folder. cd to your folder. Then type:
prog.exe < in.txt
Remember, your text file will be treated exactly as it is. Shoudld't be a problem if you know cin only catches upto next whitespace character, while string input functions (e.g. cin.getline) only catch upto next newline character.
//Sample prog.cpp
#include <iostream>
using namespace std;
int main()
{
int num;
do
{
cin >> num;
cout << (num + 1) << endl;
}
while (num != 0);
return 0;
}
//Sample in.txt
2
51
77
0
//Sample output
3
52
78
1
Sorry if you are on other platform, I don't know about them.

terminate while loop using character

I need help with the following snippet:
Using string in the following loop terminates:
int main() {
string in;
while(1){
cin >> in;
if (in == "|")
break;
}
But using int in the following loop does not terminates:
int main() {
int in;
while(1){
cin >> in;
if (in == '|')
break;
else
cout<< in << "\n";
}
I want to terminate the last shown snippet. Is it possible to do using int in.
I've seen the post C++ Terminate loop using a char input to int but no solution.
While characters are represented in the computer as small integers, an int is not the same as a char. Not in C++.
When you read into an int variable the >> operator tries to parse the input as an integer, as a number and not as a character.
If you want to read a character then read a character:
char in;
std::cin >> in;
If you try to read an integer, and the input is not a number, then the input operator will fail. See e.g. this very simple example.
This is the approach I used
I needed to input integers to a vector as input using while loop indefinitely and terminate it using a character.
This approach takes the input as character and then convert this char into integer.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
char in;
std::vector<int> vec;
while(1){
cin>>in;
if(in == '|'){
break;
}
vec.push_back(in - '0');
}
for(int j=0; j<vec.size(); j++){
cout<<vec[j];}
return 0;
}

C++ : initialize input programmatically

If we have this code snippet:
int a;
cout << "please enter a value: ";
cin >> a;
And in the terminal, the input request would look like this
please enter a value: _
How can I programatically simulate a user's typing in it.
Here's a sample how to manipulate cin's input buffer using the rdbuf() function, to retrieve fake input from a std::istringstream
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
istringstream iss("1 a 1 b 4 a 4 b 9");
cin.rdbuf(iss.rdbuf()); // This line actually sets cin's input buffer
// to the same one as used in iss (namely the
// string data that was used to initialize it)
int num = 0;
char c;
while(cin >> num >> c || !cin.eof()) {
if(cin.fail()) {
cin.clear();
string dummy;
cin >> dummy;
continue;
}
cout << num << ", " << c << endl;
}
return 0;
}
See it working
Another option (closer to what Joachim Pileborg said in his comment IMHO), is to put your reading code into a separate function e.g.
int readIntFromStream(std::istream& input) {
int result = 0;
input >> result;
return result;
}
This enables you to have different calls for testing and production, like
// Testing code
std::istringstream iss("42");
int value = readIntFromStream(iss);
// Production code
int value = readIntFromStream(std::cin);
Hey why don't you write your input in a plain text file and redirect it to cin ???
It's the simplest method.
Open Command Prompt.
Suppose your text file which will used as input is in.txt and your program is prog.exe.
Keep the text file and the program in same folder. cd to your folder. Then type:
prog.exe < in.txt
Remember, your text file will be treated exactly as it is. Shoudld't be a problem if you know cin only catches upto next whitespace character, while string input functions (e.g. cin.getline) only catch upto next newline character.
//Sample prog.cpp
#include <iostream>
using namespace std;
int main()
{
int num;
do
{
cin >> num;
cout << (num + 1) << endl;
}
while (num != 0);
return 0;
}
//Sample in.txt
2
51
77
0
//Sample output
3
52
78
1
Sorry if you are on other platform, I don't know about them.