How to error check that input is int or something else

This is a discussion on How to error check that input is int or something else within the C++ Programming forums, part of the General Programming Boards category; Hi, here is my problem. I have to make a program that determines the distance between 2 points of latitude ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    14

    How to error check that input is int or something else

    Hi, here is my problem. I have to make a program that determines the distance between 2 points of latitude and longitude. I've done all that, but I'm having a problem with the error checking. The first location is supposed to be enter with a space between the latitude and longitude (34 151). A negative in front of the number denotes south or west for latitude and longitude. So if i enter (34 abc), it thinks that abc is a number and calculates something and i get a non-garbage output. How would I put an error checking feature into my program so it would says that "abc" is a bad input?

    thanks!

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    Just a suggestion

    Why don't you create a string variable (character array), accept the input initially into it, verify that the array consists of digits or spaces only. If it contains any other symbol, you know that an error has occured (the data is invalid).

    Code:
    int validate(char *);
    
    int validate(char *p)
    {
          while(*p)
          {
                if(! ((*p >= '0' && *p <= '9') || *p == ' ') )
                      return 0;
                p++;
           }
           
            return 1;
    }
    If we assume that your array name is "inputval" then you could use this function in this fashion

    Code:
    if( validate(inputval) )
    {
             std::cout << "Valid input... proceed with converting the character values into integers";
    }
    else
    {
              std::cout << "Invalid Input";
    }
    Last edited by shiv_tech_quest; 01-17-2003 at 02:11 AM.
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  3. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 06:34 AM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM

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