473,378 Members | 1,419 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,378 software developers and data experts.

how can i check my data type?

GraxGunz
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.    int num;
  7.    int mul;
  8.  
  9.    cout << "Enter a number: ";
  10.    cout >> num;
  11.  
  12.    //if()
  13.  
  14.    mul= num*10;
  15.    cout << "This is 10 times the numbers << mul << endl;
  16.  
  17.    /*   
  18.    else
  19.    {
  20.       cout << "Input error, Please try again."
  21.       return main();
  22.     }*/
  23.  
  24.    return 0;
  25.  
  26.  
this is the simple program i wrote, an d i want to know is there anyway to check my data type of num so that the program will only run when user enter an integer value. it will prompt "Input error, Please try again." when user enter a float, string or char values, can someone help me, i have no ideal how to do that....
Dec 9 '11 #1

✓ answered by weaknessforcats

cin.clear() only resets the good/fail flag. The bad data is still there.

You can't really cear the stdin buffer since you don't know what is there.

Your could write a function that takes an int by reference and returns true if an int was entered.

The function does a cin >> an int.
If this works you have your int and the function returns true.
Otherwise, the function calls cin.clear() to reset the fail bit. and returns false. You don't have your int and the bad data is still in stdin.


Expand|Select|Wrap|Line Numbers
  1. bool GetInt(int& thedata)
  2. {
  3.     cin >> thedata;
  4.     if (cin.fail() == true)
  5.     {
  6.         //no int
  7.         cin.clear();  //reset fail flag
  8.         return false;
  9.     }
  10.     return true;  //got the int
  11. }
  12.  
The function doesn't try to recover the stdin since it's job was to fetch an int. The function that calls this one can decide what to do. In tis case, a single byte is fetched and discarded and the function is tried again:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     int myInt;
  4.     while (GetInt(myInt) == false)
  5.     {
  6.         char trash;
  7.         cin >> trash;
  8.     }
  9.     cout << myInt << endl;
  10. }
  11.  
This should give you the idea.

5 3849
weaknessforcats
9,208 Expert Mod 8TB
You are using C++ so cin >> to an int and then call cin.fail(). If cin.fail() returns true and int was not entered. Before you retry, you will need to empty the stdin buffer to remove the non-int data.
Dec 9 '11 #2
[Before you retry, you will need to empty the stdin buffer to remove the non-int data.]

i got your ideal, but how can i remove the non int data at stdin? does c++ have any reserve word? sorry, i just start leaned few months ago... still not understand very well...
Dec 10 '11 #3
i had try cin.clear(); but it seems didn't works...
note: i'm using dev c++
Dec 10 '11 #4
weaknessforcats
9,208 Expert Mod 8TB
cin.clear() only resets the good/fail flag. The bad data is still there.

You can't really cear the stdin buffer since you don't know what is there.

Your could write a function that takes an int by reference and returns true if an int was entered.

The function does a cin >> an int.
If this works you have your int and the function returns true.
Otherwise, the function calls cin.clear() to reset the fail bit. and returns false. You don't have your int and the bad data is still in stdin.


Expand|Select|Wrap|Line Numbers
  1. bool GetInt(int& thedata)
  2. {
  3.     cin >> thedata;
  4.     if (cin.fail() == true)
  5.     {
  6.         //no int
  7.         cin.clear();  //reset fail flag
  8.         return false;
  9.     }
  10.     return true;  //got the int
  11. }
  12.  
The function doesn't try to recover the stdin since it's job was to fetch an int. The function that calls this one can decide what to do. In tis case, a single byte is fetched and discarded and the function is tried again:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     int myInt;
  4.     while (GetInt(myInt) == false)
  5.     {
  6.         char trash;
  7.         cin >> trash;
  8.     }
  9.     cout << myInt << endl;
  10. }
  11.  
This should give you the idea.
Dec 10 '11 #5
i got that! thank you very much ^.^
Dec 10 '11 #6

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

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
13
by: dawatson833 | last post by:
I have several stored procedures with parameters that are defined with user defined data types. The time it takes to run the procedures can take 10 - 50 seconds depending on the procedure. If I...
5
by: BigAl | last post by:
I am using a SQL server database and am using a Check Box on my .aspx screen. What data type should I use when I design my table? I tried bit but When I ran the program and Checked one of the...
5
by: Don | last post by:
I have an array of System.type and I need to go through the array and perform different logic depending on the type stored in the array. I want to do: if (typeof typeCollection(i) is String)...
3
by: Bob Day | last post by:
VS 2003, sql How do you determine the data type of a column if its value is DBNull? 1)Table: Column1 STRING non-nullable 2) Fill to a DataSet via DataAdapter 3) dim Data_Type_Is...
1
by: Neo | last post by:
I have a field named "Male" & this is of SQL BIT data type (SQL Server). How can i bind this field to a check box? I noticed that this field can be NULL, 0 or 1. But i'm having trouble binding...
2
by: Neo | last post by:
I put a similar post for CheckBoxes, but i want to use a Radio button & not a CheckBox I have a field named "Male" & this is of SQL BIT data type (SQL Server). How can i bind this field to a...
13
by: Fei Liu | last post by:
Hi Group, I've got a problem I couldn't find a good solution. I am working with scientific data files in netCDF format. One of the properties of netCDF data is that the actual type of data is only...
7
by: Arpan | last post by:
The .NET Framework 2.0 documentation states that An Object variable always holds a pointer to the data, never the data itself. Now w.r.t. the following ASP.NET code snippet, can someone please...
2
by: fniles | last post by:
I am using VB.NET 2005 and SQLDataReader. How can I check the data type of a field in the DataReader ? Thank you. Dim cmdSQL As SqlClient.SqlCommand Dim drSQL As SqlClient.SqlDataReader sSQL...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...

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.