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

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 06: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, 12:30 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM