•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,369 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,107 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1678 | Replies: 17
![]() |
•
•
Join Date: Dec 2004
Location: Allentown, PA
Posts: 60
Reputation:
Rep Power: 4
Solved Threads: 1
In using "cin" for getting input I have about a 99% success rate. In the remaining
1% of cases the program does not pause for input. What causes this and how does one fix it?
In ancient times when we programmed in C rather than C++ the same problem occurred
occasionally if "getc" or "getchar" was used for input. You could solve the problem by
putting the line "flushall()" before the input statement but that doesn't seem to
work with cin.
I ought to give an example but the only ones I have are in longish programs that I
wouldn't ask anyone to read.
1% of cases the program does not pause for input. What causes this and how does one fix it?
In ancient times when we programmed in C rather than C++ the same problem occurred
occasionally if "getc" or "getchar" was used for input. You could solve the problem by
putting the line "flushall()" before the input statement but that doesn't seem to
work with cin.
I ought to give an example but the only ones I have are in longish programs that I
wouldn't ask anyone to read.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,697
Reputation:
Rep Power: 36
Solved Threads: 878
same problem as in old days. when you enter a number and press <Enter> cin does not remove the "\n" from the keyboard -- you have to do that
int x; cin >> x; cin.ignore(); // remove '\n' key
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
•
•
In using "cin" for getting input I have about a 99% success rate. In the remaining 1% of cases the program does not pause for input. What causes this and how does one fix it?
•
•
•
•
In ancient times when we programmed in C rather than C++ the same problem occurred occasionally if "getc" or "getchar" was used for input. You could solve the problem by putting the line "flushall()" before the input statement but that doesn't seem to work with cin.
#include <iostream>
#include <limits>
int main()
{
using namespace std;
// Create the problem by leaving a '\n' in the stream
char letter = 0;
cout << "Please type one letter: ";
cin.get( letter );
cout << "The next character is: " << (int)cin.get() << '\n';
// Create the same problem and fix it with cin.ignore()
cout << "Please type one letter: ";
cin.get( letter );
// Ignore the maximum value of the streamsize type or until '\n' is ignored
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
// Now the cin.get() call will block because the stream is empty
cout << "The next character is: " << (int)cin.get() << '\n';
return 0;
}#include <iostream>
#include <limits>
#include <sstream>
#include <stdexcept>
#include <string>
namespace Raye {
using namespace std;
template <class T, class U>
T CType( U src )
{
stringstream ss;
T dst;
if ( !( ss << src ) )
throw runtime_error( "CType: stringize error" );
if ( !( ss >> dst ) )
throw runtime_error( "CType: invalid conversion" );
return dst;
}
template <class T>
T GetType( istream& is )
{
string s;
if ( !getline( is, s ) )
throw runtime_error( "GetInt: input failure" );
return CType<T>( s );
}
}
int main()
{
using namespace std;
int age;
string name;
// Create the problem as an example
cout << "Please type your age: ";
cin >> age;
cout << "Please type your name: ";
getline( cin, name );
cout << name << " is " << age << " years old\n";
// Clean up the stream for an accurate test
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
// Eliminate the problem with GetType
cout << "Please type your age: ";
age = Raye::GetType<int>( cin );
cout << "Please type your name: ";
getline( cin, name );
cout << name << " is " << age << " years old\n";
return 0;
}
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
•
•
•
•
flushall() is not a standard function so you cannot expect it to work. You use cin.ignore() in C++.
#include <iostream> #include <limits> int main() { using namespace std; char letter = 0; cout << "Please type one letter: "; cin.get( letter ); cout << "The next character is: " << (int)cin.get() << '\n'; cout << "Please type one letter: "; cin.get( letter ); cin.ignore( numeric_limits<streamsize>::max(), '\n' ); cout << "The next character is: " << (int)cin.get() << '\n'; return 0; }
Though it would be really better if cin.clear( ) was placed before the call to cin.ignore( ) to deal with corrupted input stream along with the stray characters or junk in it. (eg. user enters a float in place of character).
Last edited by ~s.o.s~ : Jan 2nd, 2007 at 1:47 pm.
"I don't accept change. I don't deserve to live."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Yes true, but if already used, why not make it a bit more er... robust...
"I don't accept change. I don't deserve to live."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,697
Reputation:
Rep Power: 36
Solved Threads: 878
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
•
•
Though it would be really better if cin.clear( ) was placed before the call to cin.ignore( ) to deal with corrupted input stream along with the stray characters or junk in it. (eg. user enters a float in place of character).
Well, in real code I would check the error state of cin and deal with legitimate problems long before trying to do cin.ignore(). But real code is harder to read and has lots of extras that hide the point I'm trying to make.

•
•
•
•
Keep in mind, cin is the C++ version of C's scanf() and must be used appropriately. IMAO that means -- don't!
Not to be nitpicky, but cin is the C++ version of C's stdin. The overloaded >> operator is the C++ version of scanf.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
•
•
•
•
Not to be nitpicky, but cin is the C++ version of C's stdin. The overloaded >> operator is the C++ version of scanf.
Yes, you guys are right. So I change my statement:
Keep in mind,
cin << is the C++ version of C's scanf() and must be used appropriately. IMAO that means -- don't!
Age is unimportant -- except in cheese
But then I couldn't be nitpicky.
Isn't nitpicking supposed to be one of life's true joys or something?
Isn't nitpicking supposed to be one of life's true joys or something?•
•
•
•
Keep in mind,cin <<is the C++ version of C's scanf() and must be used appropriately. IMAO that means -- don't!
cin >>, you mean.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the C++ Forum
- Previous Thread: How to make a C++ game??
- Next Thread: Carets in turbo c++ 3.0 graphics?



Linear Mode