473,394 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

cin ALMOST does what I want!

Hello,
I am writing a script that needs to get a numeric value from the keyboard. I have already solved the problem of when a user inputs a string, so that is not the problem! Now I need to solve the problem of when a user inputs a mixed type. Here is an example of what I mean, the script:
------------------------------------------------------------------------------------------------------
#include <iostream>
#include "CinHelp.h"

int main()
{
int p;
double r;
CinHelp myin;

cout << "Enter an int: ";
p = myin.getInt();
cout << p << endl;

cout << "Enter a double: ";
r = myin.getDouble();
cout << r << endl;
return (0);
}
------------------------------------------------------------------------------------------------------

This script uses a class I wrote to deal with the case where a user enters a string such as 'pizza' instead of a number. The class:
------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;

class CinHelp
{
public:

int getInt()
{
int inpt;

cin >> inpt;

while (cin.fail())
{
cout << endl
<<"Looking for an integer (1, 2, 3, etc.). Try again. ";
cin.clear();
cin.ignore(INT_MAX, '\n');
cin >> inpt;
}

return(inpt);
}


double getDouble()
{
double inpt;
cin >> inpt;

while (cin.fail())
{
cout << endl
<<"Looking for a number (1, 2.3, etc.). Try again. ";
cin.clear();
cin.ignore(INT_MAX, '\n');
cin >> inpt;
}

return(inpt);
}

};
------------------------------------------------------------------------------------------------------
This solves the problem of a user doing something like:
Enter an int: pizza
but it does not solve the problem of a user doing something like:
Enter an int: 44tre
If you copy these and run them, you will see that the characters are stuck in the stream somehow and end up messing with the next cin call. Can anyone please help me modify my CinHelp class to deal with this situation? I have no programming experience at all as of 3 weeks ago, I am learning completely on my own, so sorry if this is a really stupid question!
Thanks.
Nov 16 '06 #1
2 1652
horace1
1,510 Expert 1GB
when converting a decimal numeric value from characters (e.g. reading from cin) the conversion stops at the first non-digit and so long as some digits have been converted the stream is error free. So if 44tre is entered when reading an int, 44 is returned and the next conversion can fail.
What you can do is to check if the next character is white space and if not call cin.ignore() to skip the rest of the line. e.g.
Expand|Select|Wrap|Line Numbers
  1. int getInt()
  2. {
  3. int inpt;
  4. // read int - if fail report error and read again
  5. while (!(cin >> inpt))
  6. {
  7. cout << endl
  8. <<"Looking for an integer (1, 2, 3, etc.). Try again. ";
  9. cin.clear();
  10. cin.ignore(INT_MAX, '\n');
  11. }
  12. // if next character is not whitespace skip characters to end of line
  13. if(!isspace(cin.peek())) cin.ignore(INT_MAX, '\n');
  14. return(inpt);
  15. }
  16.  
of course this will not help if the user enters
44 tt

if you know all numeric input will be one value per line you can call cin.ignore() after every read to clear any surplus characters off the line
Nov 17 '06 #2
Thank you horace1.
That works beatifully, and it is simple!

I think I will do this for cases when a user must only enter one number per line. Thanks again.
Nov 17 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Bob Kochem | last post by:
By default, right-clicking the TextBox control launches a standard "edit" pop-up menu. Microsoft provides a work-around for the case where you want to launch a different pop-up menu; ...
15
by: Steve Richfield | last post by:
To All, First, some of your replies to me have been posted through DevelopersDex, and these are NOT posted on USENET for the world to see. DevelopersDex appears to be trying to hijack USENET,...
3
by: Jérôme de Lagausie | last post by:
Hello, In one hand, I've got a set of more than 130 different structures, mostly rather simple (a set of simple data members such as int, long, char , float ...) sample : #ifndef _H2_H_...
10
by: Guadala Harry | last post by:
I have a modal dialog that currently does all of the following except item 4. 1. lets users select a graphic from a list of thumbnails (and when selected, displays the full-size image in a...
1
by: Rob | last post by:
The code below is almost there (it does change the value for EVERY ORDER).... I simply want to be able to change the value of the Delivered Tag to Yes. BUT ONLY if the Order is equal to 123456 ? ...
1
by: swynne | last post by:
Hi friendly people...I am a newb to programming and need a little assistance. I have a small personal ad database created in Access, however all is well until I want the personal ad link to take me...
1
by: Ron | last post by:
This code is almost doing what I want it to do, here is the code: private void btnConvert_Click(object sender, EventArgs e) { // Get a directory string path = @"c:\WAMP\"; foreach (string...
11
by: Peter Webb | last post by:
I previously asked about two problems I had with some graphics - the first was that when I drew animation to a picturebox it wouldn't display when the Form loaded. It was suggested to me by...
9
by: mrcheeky | last post by:
Hi, I'm stuck, but it's almost working! From a html page, my javascript calls a server-side php script. The php reads a value from a server-side .txt file and passes it back as a javascript...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.