| | |
User Input: Strings and Numbers [C++]
Please support our C++ advertiser: Intel Parallel Studio Home
1
•
•
•
•
>Not sure if you guys are not considering "fflush" on purpose
Actually, yes... http://www.gidnetwork.com/b-57.html
Actually, yes... http://www.gidnetwork.com/b-57.html
0
•
•
•
•
My question is regarding he use of numeric_limits<streamsize>::max().
I found that this compiled in the command line using the C++ command in Bloodshed Dev-C++. However, when I tried to compile this code in a console project in Visual C++ 6.0, I was successful only after including headers for <stream> and <limits>. Why this difference?
Raman
I found that this compiled in the command line using the C++ command in Bloodshed Dev-C++. However, when I tried to compile this code in a console project in Visual C++ 6.0, I was successful only after including headers for <stream> and <limits>. Why this difference?
Raman
1
•
•
•
•
>Why this difference?
Well, first of all, Visual C++ 6.0 is a fairly old compiler and doesn't really conform to proper C++ standards. You should really get a newer compiler (Try the Express edition of Visual Studio 2008).
Second, some compilers' headers include other headers, some don't. In this case, because I was using the STL objects string, stringstream, cout, numeric_limits<T>, it means I should have also included the following headers in the code:
Those headers are included by default from iostream in gcc. But you are correct, I should update the tutorial.
Well, first of all, Visual C++ 6.0 is a fairly old compiler and doesn't really conform to proper C++ standards. You should really get a newer compiler (Try the Express edition of Visual Studio 2008).
Second, some compilers' headers include other headers, some don't. In this case, because I was using the STL objects string, stringstream, cout, numeric_limits<T>, it means I should have also included the following headers in the code:
C++ Syntax (Toggle Plain Text)
#include <string> #include <limits>
0
•
•
•
•
•
•
•
•
>Why this difference?
Well, first of all, Visual C++ 6.0 is a fairly old compiler and doesn't really conform to proper C++ standards. You should really get a newer compiler (Try the Express edition of Visual Studio 2008).
Second, some compilers' headers include other headers, some don't. In this case, because I was using the STL objects string, stringstream, cout, numeric_limits<T>, it means I should have also included the following headers in the code:
Those headers are included by default from iostream in gcc. But you are correct, I should update the tutorial.C++ Syntax (Toggle Plain Text)
#include <string> #include <limits>
Raman
0
•
•
•
•
are the codes used in the c++ programming same with the dev-c++ programming, coz im using the latter, and will you give me the list of header files and their use and algorithms, i am a fully first time programmer, thanks
0
•
•
•
•
•
•
•
•
User input/output is a key component in the programs you write, yet most online tutorials just provide a quick-and-dirty method of grabbing data. In other words, it works right up until the point the user does something unexpected.
In this tutorial you will learn how to avoid the common mistakes by doing it properly.
Take this typical method that tutorials use to teach user input:
C++ Syntax (Toggle Plain Text)
int number; cin >> number; cout << "You typed the number " << number << ".\n";
Now let's say you want the user to enter a string that contains a space in it. Since usingcin >> myStringwon't work (it will only grab the first word and then exit), you decide you need to find a function that will grab a whole line of input.
After some Googling, you decide that getline() is probably the best way to do it. You add it to your existing code, and this is how your entire program looks:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int main() { int number; string name; cout << "Please enter a number." << endl; cin >> number; cout << "Enter your entire name (first and last)." << endl; getline(cin, name); cout << "Your full name is " << name << ", and the number you entered is " << number << endl; return 0; }
Now you run it and the program totally skips the getline() statement!
That's rather odd. Let's try using only getline for our input (which means the number will be put into a string instead):Please enter a number. 5 Enter your entire name (first and last). Your full name is , and the number you entered is 5
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int main() { string number; string name; cout << "Please enter a number." << endl; getline(cin, number); cout << "Enter your entire name (first and last)." << endl; getline(cin, name); cout << "Your full name is " << name << ", and the number you entered is " << number << endl; return 0; }
The program's output being:
The problem went away.Please enter a number. 5 Enter your entire name (first and last). Joe Programmer Your full name is Joe Programmer, and the number you entered is 5
You will probably suspect something is up withcinand are right. What's actually happening in the first example is:
- You enter something in.
- You hit return.
- All of this is put into the input buffer (including the newline that results from hitting return).
cingrabs whatever it needs (in this case the number), but leaves the newline behind!- Since
getline()only grabs 1 line, all it gets is the newline left behind bycin.
To fix this the best approach, although not that easy for newbies, is to avoid the use ofcinuntil you know what you're doing, and letgetline()handle your user input. It not only solves the newline problem, but it also solves a number of other problems induced by usingcin.
This is relatively easy for people to do until they get to numbers (which usually can't be read into strings because they need to be manipulated). For this, you can use astringstreamto convert the string back into a number:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <sstream> // need this for stringstreams! using namespace std; int main() { int number; string line; stringstream stream; cout << "Please enter a number." << endl; getline(cin, line); stream << line; stream >> number; cout << "The number entered was " << number << ".\n"; return 0; }
This outputs:
Please enter a number. 4 The number entered was 4.
You can now see how the operation is nearly identical, except that you are taking an extra step by first putting the input a string, and then putting it into a separate stream before popping it back out in the number.
Finally, you actually can mixcinwithgetline(), if you know what you are doing. The easiest method is to usecin.ignore(<some large number>, '\n')to clear the input buffer. You could simply hard code a large number, although it's usually better to use a built-in constant. The Standard Template Library provides such a constant, which is found in the <limits> header:numeric_limits<streamsize>::max()
If you were to implement this in your first example, it would work like this:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <limits> using namespace std; int main() { int number; string name; cout << "Please enter a number." << endl; cin >> number; cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << "Enter your entire name (first and last)." << endl; getline(cin, name); cout << "Your full name is " << name << ", and the number you entered is " << number << endl; return 0; }
And the output would be:
Please enter a number. 3 Enter your entire name (first and last). Joe Programmer Your full name is Joe Programmer, and the number you entered is 3
However, this should only be used when absolutely necessary, as this is just a band-aid for the function. It doesn't really fix some of the other problems that come with using it. So perhaps it is best to stick withgetline(). Don't do the quick-and-dirty method that online tutorials teach you, because it will eventually come back and bite you in the back of the neck. Flushing the input buffer like mentioned previously is only a last resort for usingcin; it's not a good alternative to usinggetline()to handle your input. A little bit of trouble now usinggetline()will save you a lot of pain later!
Update: Narue has written a nice tutorial showing how to clear the input buffer and all the problems associated with doing so: How do I flush the input stream?
Similar Threads
- user input into a string (C++)
- Tutorial: User Input: Strings and Numbers [C] (C)
- error checking of user input (C++)
- Need Help With Error Checking User Input (C)
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets


