Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 07-09-2002, 02:06 AM   #1 (permalink)
brtklt
Registered User
 
Join Date: Jul 2002
Location: Dallas, TX
Posts: 2
brtklt is on a distinguished road
Send a message via Yahoo to brtklt
checking datatypes in c++

How do I check that the correct input was given for a certain datatype? For example, I declare an int var "i" and want to make sure the input for this var is actually a number before the rest of the program continues?

Last edited by brtklt; 07-09-2002 at 03:16 AM.
brtklt is offline   Reply With Quote
Old 07-09-2002, 11:30 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,696
redhead is on a distinguished road
Code:
int main()
{
   int i;
   cout << Please enter an integer: ;
   cout.flush();
   cin >> i;
   if(i > 0)
     cout << "Thanks for giving me an integer" <<endl;
   else
     cout << "What, are you dumb or just stupid? I said an integer... Dumba**" <<endl;
   return i;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 07-09-2002, 11:37 PM   #3 (permalink)
brtklt
Registered User
 
Join Date: Jul 2002
Location: Dallas, TX
Posts: 2
brtklt is on a distinguished road
Send a message via Yahoo to brtklt
Simple enough...
brtklt is offline   Reply With Quote
Old 07-09-2002, 11:46 PM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,696
redhead is on a distinguished road
If I remember correct, the cin class will set a given int type to 0 if the input isn't recognised as the same type.

Naturaly inputting to a string, will just convert the input of 123 to the string "123"
But this will be done for int and float/double inputs.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 07-10-2002, 12:54 AM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,696
redhead is on a distinguished road
This is missleading... But I guess C++ is ugly that way..

A small test revealed, if the input isn't an int, the int value will be set to less than INT_MIN/2 So the disturbing part is, what if the user chose to input INT_MIN+2 then we can't be sure wether its a valid input or not... Not even errno is set to anything..

Oddly enough, theres alot of setf() flags, that can turn input into truely boolean or decimal but none defined for a simple int...

Guess the last thing todo, is use a fscanf() on stdin, to make sure its a valid int you're getting..
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 07-10-2002, 03:57 AM   #6 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,696
redhead is on a distinguished road
There is a fix, buts thats just a small hack I came up with...

Code:
#include <iostream>
#include <limits.h>
using namespace std;

int main()
{
  int i=INT_MIN;
  cout << "Enter your digit: ";
  cout.flush();
  cin >> i;
  if(i > INT_MIN)
    cout << "Thanks for the integer." << endl;
  else
    cout << "WTF. No integer?" << endl;
  return i;
}
But, then again, if the user inputs a value repressenting INT_MIN (which on 32bit architecture is -2147483648 ) it will fail.. Yet, who would want to use that as the input value?
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
c simple question problem with switch case if13121 Standard C, C++ 1 10-24-2004 09:43 PM
operate overloading member function in C# sureshkumar_kc MS Technologies ( ASP, VB, C#, .NET ) 2 10-15-2004 02:36 AM
For those who have Learned C from "The C Programming Lanuage" DemosthenesB Standard C, C++ 5 07-13-2003 12:22 AM
edit? anon Lounge 10 11-21-2002 03:02 PM


All times are GMT -8. The time now is 09:59 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting