testing for int or float

This is a discussion on testing for int or float within the C++ Programming forums, part of the General Programming Boards category; atoi typically returns 0 on failure, so if you check the string for 0, and it's FALSE, then that mean ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    atoi typically returns 0 on failure, so if you check the string for 0, and it's FALSE, then that mean it's an invalid number.
    But, oh crap, I was going to suggest that >_< Oh well.

  2. #17
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    atoi typically returns 0 on failure, so if you check the string for 0, and it's FALSE, then that mean it's an invalid number.
    But, oh crap, I was going to suggest that >_< Oh well.
    Yeah, but what if the number the user entered IS 0?

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    That's why I said check the input for 0.
    Code:
    char buf[10];
    cout << "Enter number: ";
    cin >> buf;
    if (atoi(buf) == 0 && buf[0] != '0'); // User did not enter 0, so it's not a number
    if (atoi(buf) == 0 && buf[0] == '0'); // User actually did input 0.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21