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 03-04-2006, 02:57 PM   #1 (permalink)
kevin42
Registered User
 
Join Date: Mar 2006
Posts: 2
kevin42 is on a distinguished road
isdigit problem error false

I'm trying to validate a string (char) input. I'm using the IsDigit function. Here is my Code.

Code:
#include <cctype>
#include <iostream>

using namespace std;



int main()
{
  	  
 	cout << "Enter Anything" << endl;
 	
 	char a_char;
 	
 	cin >> a_char;

    if(isdigit(a_char))
    {
        cout<<"Is a digit!";
    }
    else
    {
        cout<<"Is not a digit!";
    }
    cout << endl;
    

    int end;
    cin >> end;

}
It all goes well when I insert a number, but the program just stops when I insert I char. I need to be able to test for both. How?? Please Help. Thanks
kevin42 is offline   Reply With Quote
Old 03-04-2006, 03:42 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Well.. isdigit() takes a single byte representation of an int, and tells you if the value is within {0, 1, 2, ..., 9}, any given char can be seen as an int representing any of the the values {0, 1, 2, ..., 255} so, when your input is a number, which seen as a single char can only represent values within the orriginal {0, 1, 2, ..., 9}, but when given a char ie 'c', it will infact be converted to the int value 99, which is way beond the scope for isdigit() to check.
Testing for chars, you'd might want to take a look at isascii(), which is not part of the ISO/ANSI definition, so perhaps a combination of
Code:
if(isdigit(a_char))
    // an int
else
    if(isprint(a_char))
        // a printable char besides int
    else
        // must be something else
If this dosn't give you a hint, then you might give a better description on what you're trying to achieve here, since from what I can tell it looks as if you want to test if input is a char or an int.
__________________
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 03-04-2006, 04:02 PM   #3 (permalink)
kevin42
Registered User
 
Join Date: Mar 2006
Posts: 2
kevin42 is on a distinguished road
Wowzas it worked. Thx cool guy! You r0ck4z3r!
kevin42 is offline   Reply With Quote
Old 03-04-2006, 04:05 PM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Thats what we're here for, to help people find solutions to their problems
__________________
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
parse error problem in game.. can't seem to find problem solution. slashdot Standard C, C++ 5 08-03-2005 09:15 PM
JSP code problem j.gohel Java 7 04-15-2005 03:07 PM
Hashing problem jodders Standard C, C++ 1 02-09-2005 02:51 PM
Problem Assignment (Urgent help req.) Boltress Standard C, C++ 0 01-12-2005 08:59 AM
Help debugging a power problem Belisarius Lounge 0 10-25-2003 05:44 PM


All times are GMT -8. The time now is 03:37 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