Thread: testing for int or float

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

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, 02: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, 05:26 PM