So I write a simple program for c++, and I run into this problem. The program basically asks for the input from user, and it will skip whatever whitespace after the input. I was trying to use cin.ignore() after user input, but it won't work. How would I fix it ?
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cout << "Your name: ";
getline(cin, s);
cin.ignore();
cout << "You entered: " << s << endl;
return 0;
}
Why would you want to use std::cin.ignore()? Assuming you want to skip potential leading whitespace, you might want to use std::ws:
std::getline(std::cin >> std::ws, s);
Related
I'm new to C++ programming, and I've come upon a roadblock. Here's my code:
#include <iostream>
using namespace std;
int main(){
int sum = 0, a;
cout << "enter first set of numbers:";
while(cin >> a) sum += a;
cout << "first sum = " << sum;
cin.clear();
cin.ignore( numeric_limits<streamsize>::max(), '\n');
sum = 0;
cout << "enter second set of numbers:";
while(cin >> a) sum += a;
cout << "second sum = " << sum;
}
I'm trying to sum two sets of numbers that I input at the command line. The problem is that when I hit ctrl-d to terminate my first set of numbers, it skips the second set of cin's. Most of the pages I find elsewhere on the internet tell me to use cin.clear and cin.ignore. I've tried that and it still doesn't work. This page question concerning cin.clear() even seems to have the same problems. However, the fixes they suggest don't work for me.
Does this code above work for anyone else?
When you use Ctrl-D you terminate the input stream: there is no further character coming from std::cin. Even clearing the stream doesn't help: you have told the stream it is at its end. You can't reopen it. However, clear()ing the stream and using ignore() is the solution to your problem!
You need a different indicator that one set of values is done. The easiest is to use a non-digit character or string: trying to read something which isn't an int will cause the stream to go into failure mode [without closing the stream]. That is, you read until the conversion fails, clear() the stream, and ignore() everything up to the end of the line.
Your code should already do that: instead of using Ctrl-D just enter, e.g., x.
//Use stringstream ^^ for multiple user input
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main (void)
{
string mystr;
float price = 0;
int quantity = 0;
cout << "Enter price: ";
getline (cin, mystr);
stringstream(mystr) >> price;
cout << "Enter quantity:";
getline (cin, mystr);
stringstream(mystr) >> quantity;
cout << "Total price: " << price*quantity << endl;
return 0;
}
You can use Ctrl-D, you just need to have your loop in another thread. So Ctrl-D, kills the thread, then returns to your main function, then you can start another thread.
It'd not the best solution, but it can work, http://codebase.eu/tutorial/posix-threads-c/
Please, is there a way I can wait for a single ENTER key press in all of the below cases? It works fine except when the std::getline() is used, which consumes the \n, and the ENTER needs to be pressed twice.
The code below works fine:
#include <iostream>
#include <limits>
using std::cout;
using std::cin;
using std::string;
void waitForENTERKeyPress();
int main ()
{
char c;
string s;
cout << "Enter c:"; cin >> c;
waitForENTERKeyPress();
cout << "Enter c:"; c = cin.get();
waitForENTERKeyPress();
cout << "Enter s:"; cin >> s;
waitForENTERKeyPress();
cout << "Enter s:"; std::getline(cin, s);
waitForENTERKeyPress();
return 0;
}
void waitForENTERKeyPress()
{
// Does not work with std::getline(...)
cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Press an ENTER to continue...\n";
cin.get();
}
====[UPDATE]============================================
Let me rephrase what I am looking for: The way how to make the waitForENTERKeyPress() function working in both cases – with \n in the buffer, when cin operator >> or cin.get() is used and also in cases the std::getline() is used.
From what I understand it is not possible as there is not a way how to detect if the \n is still in the buffer or not.
I have tried to use cin.rdbuf()->in_avail(), but it always returns 0.
This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 3 years ago.
Is there a reason why if in my program I am asking the user for input, and I do:
int number;
string str;
int accountNumber;
cout << "Enter number:";
cin >> number;
cout << "Enter name:";
getline(cin, str);
cout << "Enter account number:";
cin >> accountNumber;
Why after inputting the first number, it outputs "Enter Name", followed immediately by "Enter Account Number" before I even get to input my "str" for the getline(cin, str) line? Thanks!
The getline(cin, str); reads the newline that comes after the number read previously, and immediately returns with this "line". To avoid this you can skip whitespace with std::ws before reading the name:
cout << "Enter number:";
cin >> number;
cout << "Enter name:";
ws(cin);
getline(cin, str);
...
Note that this also skips leading whitespace after the newline, so str will not start with spaces, even if the user did input them. But in this case that's probably a feature, not a bug...
Try
cout << "Enter name:";
cin.ignore();
getline(cin, str);
cin >> number
only grabs the numbers from the buffer, it leaves the "enter" in the buffer, which is then immediately grabbed up by the getline and interpreted as an empty string (or string with just the new line, i forget).
It looks like you want line based reading. For this you probably want to use getline consistently and then parse each line if you need to parse a number from then read line. It makes the input reading more consistent.
This way you don't have to manually scan for the end of each line to guarantee that the next read operation starts on a fresh line.
It also makes adding error handling for repeating input requests simpler.
e.g.
#include <string>
#include <iostream>
#include <istream>
#include <ostream>
#include <sstream>
int parse_integer(const std::string& input)
{
std::istringstream iss(input);
int result;
if (!(iss >> result))
{
// error - throw something?
}
return result;
}
int main()
{
int number;
std::string str;
int accountNumber;
std::string inputline;
std::cout << "Enter number: ";
if (!std::getline(std::cin, inputline))
{
// error - throw something?
}
number = parse_integer(inputline);
std::cout << "Enter name:";
if (!std::getline(std::cin, inputline))
{
// error - throw something?
}
str = inputline;
std::cout << "Enter account number:";
if (!std::getline(std::cin, inputline))
{
// error - throw something?
}
accountNumber = parse_integer(inputline);
return 0;
}
cin >> number // eat the numeric characters
getline(cin, str) // eat the remaining newline
I think the problem is that cin >> passes on the newline character (\n). The getline() assumes the newline character is whitespace and passes it on. Someone posted a solution you can use.
You can use a dummy getline(cin, dummy); or the real thing cin.ignore(100,'\n');
Don't use getline(): it's a bad thing for memory allocation. Use fgets(). See fgets reference.
Why does cin.getline start working for the second line on the body input but break on the first?
Example Program run:
Enter name: Will
Enter body: hello world
hello again <= It accepts this one
char* name = new char[100];
char* body = new char[500];
std::cout << "Enter name: ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.getline(name, 100);
std::cout << "Enter body: ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.getline(body, 500');
std::cin >> body;
As JoshD says, but additionally, you can save a lot of work & pain by using std::string and std::getline from the <string> header.
Like ...
#include <string>
#include <iostream>
int main()
{
using namespace std;
std::string name;
cout << "Enter name: "; getline( cin, name );
}
Cheers & hth.,
– Alf
Because you're ignoring the first line with the cin.ignore statement.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
That will ignore a whole line.
Remove that and you'll get what you want.
You may also want to flush the cout stream to assure your output prints to the screen right away. Add a cout.flush(); before your getline.
This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 3 years ago.
Is there a reason why if in my program I am asking the user for input, and I do:
int number;
string str;
int accountNumber;
cout << "Enter number:";
cin >> number;
cout << "Enter name:";
getline(cin, str);
cout << "Enter account number:";
cin >> accountNumber;
Why after inputting the first number, it outputs "Enter Name", followed immediately by "Enter Account Number" before I even get to input my "str" for the getline(cin, str) line? Thanks!
The getline(cin, str); reads the newline that comes after the number read previously, and immediately returns with this "line". To avoid this you can skip whitespace with std::ws before reading the name:
cout << "Enter number:";
cin >> number;
cout << "Enter name:";
ws(cin);
getline(cin, str);
...
Note that this also skips leading whitespace after the newline, so str will not start with spaces, even if the user did input them. But in this case that's probably a feature, not a bug...
Try
cout << "Enter name:";
cin.ignore();
getline(cin, str);
cin >> number
only grabs the numbers from the buffer, it leaves the "enter" in the buffer, which is then immediately grabbed up by the getline and interpreted as an empty string (or string with just the new line, i forget).
It looks like you want line based reading. For this you probably want to use getline consistently and then parse each line if you need to parse a number from then read line. It makes the input reading more consistent.
This way you don't have to manually scan for the end of each line to guarantee that the next read operation starts on a fresh line.
It also makes adding error handling for repeating input requests simpler.
e.g.
#include <string>
#include <iostream>
#include <istream>
#include <ostream>
#include <sstream>
int parse_integer(const std::string& input)
{
std::istringstream iss(input);
int result;
if (!(iss >> result))
{
// error - throw something?
}
return result;
}
int main()
{
int number;
std::string str;
int accountNumber;
std::string inputline;
std::cout << "Enter number: ";
if (!std::getline(std::cin, inputline))
{
// error - throw something?
}
number = parse_integer(inputline);
std::cout << "Enter name:";
if (!std::getline(std::cin, inputline))
{
// error - throw something?
}
str = inputline;
std::cout << "Enter account number:";
if (!std::getline(std::cin, inputline))
{
// error - throw something?
}
accountNumber = parse_integer(inputline);
return 0;
}
cin >> number // eat the numeric characters
getline(cin, str) // eat the remaining newline
I think the problem is that cin >> passes on the newline character (\n). The getline() assumes the newline character is whitespace and passes it on. Someone posted a solution you can use.
You can use a dummy getline(cin, dummy); or the real thing cin.ignore(100,'\n');
Don't use getline(): it's a bad thing for memory allocation. Use fgets(). See fgets reference.