How do I skip non-integer values in a list?

This is a discussion on How do I skip non-integer values in a list? within the C++ Programming forums, part of the General Programming Boards category; Say I have a list of numbers and that my program is only supposed to deal with integer values... Does ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    How do I skip non-integer values in a list?

    Say I have a list of numbers and that my program is only supposed to deal with integer values... Does anyone know a simple way to determine if one of the values is a decimal (like 5.6) , output a message and skip it?

    I already know a way to locate letters/negative and output an error message, but the if statement i used does not work for decimals.

    the list looks like this:

    10
    45
    86
    5.6
    6
    abc
    12

    I have to convert the integers to another base...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Use fgets() to read each line.
    Use strtol() to parse each line, then look at the various error states and 'endptr' to determine the success (or otherwise) of your conversion.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    Actually, I can't just use functions for this. Does anyone know how to do this with a if loop or bool expression?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Actually then, you need to have a go at it yourself and we'll tell you where you need to improve.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    Ok, I catch your trend.
    I was thinking of taking the value into a string and then locating the "." in the number, but that would involve more functions to do that and by using a string my other tests (to see if number is negative or letter), would not work. (I am using a simple if statement for those).

    So, does anyone have any other suggestions as to how this can be done?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    Since this is C++, why not try to (std::cin >> num), where num is an int? If it fails, you know you have an error, so you can then do std::cin.clear(), ignore what is left until the next newline, then continue reading.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list problems (null values)
    By scwizzo in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2008, 05:04 PM
  2. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  3. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 11:30 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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