1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

input data validation

Discussion in 'C' started by imrose_suleman, Oct 23, 2008.

  1. imrose_suleman

    imrose_suleman New Member

    Joined:
    Oct 18, 2008
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Programer in Tata consultancy services
    Location:
    native of hyderabad presently in chennai
    dude....how can i check whether given input is a string or number by using exception handling i.e by using try catch blocks...i m able to throw the variables..but initially i have to take that input into which variable?suggest me code...
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Depends how you determine whether something is a string or a number, often it's done simply by looking at the first character.
    Code:
    if (test_str[0]>='0' && test_str[0]<='9') // and maybe check for +, -, . and other number chars
    {
      // it's a number
    }
    else
    {
      // it's a string
    }
    
    Using exception handling doesn't get you away from the above structure:
    Code:
    try {
    if (test_str[0]>='0' && test_str[0]<='9') // and maybe check for +, -, . and other number chars
    {
      throw "it's a number";
    }
    else
    {
      throw "it's a string";
    }
    } catch (char *ex)
    {
     // what are you going to do here?  Compare the string and set a flag?
    }
    
    As you can see it's spectacularly inefficient to do it this way, because you've already determined whether it's a character or a string when you decide which exception to throw.

    Another possibility is to call atoi or something similar, and if that returns an error, throw an exception, but even then when you've got the error you know that the test string contains something that isn't a number (and is presumably a string).

    What exactly are you trying to do? Taken exactly as written the question makes no sense; try/catch is an exception handling mechanism, it does no testing for whether something is a number or a string or any other processing; if an exception is thrown in a try block, it is picked up by the catch block, and that's all.

    If you've got your own atoi equivalent that throws exceptions if the number isn't converted then the question could make sense:
    Code:
    try {
      num=my_atoi(test_str);
      itsanum=1; itsastr=0;  // if it worked, this is valid, if not then we got an exception
    } catch (my_atoi_ex)
    {
      // maybe check what the exception is, the failure could be down to a number of reasons
      if (my_atoi_ex.FailedBecauseItsAString)
      {
        itsanum=0; itsastr=1;
      }
    }
    
     
  3. imrose_suleman

    imrose_suleman New Member

    Joined:
    Oct 18, 2008
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Programer in Tata consultancy services
    Location:
    native of hyderabad presently in chennai
    thanks for ur answer.Initially u have taken a array in determining the elements in that are numbers or not...the code that u have provided fails for inputs like..123xsd34..
    how ever i have written code in cpp which will determine whether the given number is integer or not.... this worked on my com...i heard that there is a function isnumber()..do u know that?..how ever code i have written below will not fail for any of ur input....how long it may be and how mixed it may be....
    code:
    Code:
    #include<iostream>
    using namespace std;
    #include<cstring>
    int check(string s)
    {
        int x;
        char y;
        int count=0;
        for(int i=0;i<s.length();i++)
        {
            //cout<<s.at(i)<<"   ";
            x=int(s.at(i));
            //cout<<x<<"  ";
            if(x>47 &&  x< 58) count++;
            //cout<<"count"<<count<<" ";
        }
        if(count==s.length())
            return 0;
        else return 1;
    }
    main()
    {
        string s;
        cout<<"enter int"<<endl;
        cin>>s;
        if(!check(s))
            cout<<"int"<<endl;
        else cout<<"not an int"<<endl;
    }
     
    Last edited by a moderator: Oct 23, 2008
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Please use code blocks. It's very easy, just type [ code ] before the code, and [ /code ] after (without the spaces).

    Also half an idea...ellipses...hard to follow..also half a wo..not easy. Try to use complete words and sentences as it means people don't have to re-read your post about 20 times to work out what you're on about.

    Obviously it's up to you how you determine whether or not a particular input is numeric. What you could do in the conversion function is to check if each character is appropriate for a number then throw an exception if it isn't.

    I've heard of isdigit() which tests a single character. I don't know a function called isnumber() but that doesn't mean such a function doesn't exist.

    Try to use char constants rather than ASCII values. x>47 && x< 58 might mean the same as x>='0' && x<='9' but the latter is a lot more readable, and is portable to other architectures where '0' is not necessarily 48. isdigit() is better still, because it works off a table and looks up the individual character code in the table to see whether or not it's a digit, so this is portable to architectures where '0'~'9' are not consecutive.

    Your loop doesn't need to scan the whole string, you could just bail out when you find the first non-digit:
    Code:
    for (int i=0; i<s.length(); i++)
    {
      if (isdigit(s.c_str))
        count++;
      else
        throw "not a number"; // or just return 0;
    }
    return 1;
    
     
  5. imrose_suleman

    imrose_suleman New Member

    Joined:
    Oct 18, 2008
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Programer in Tata consultancy services
    Location:
    native of hyderabad presently in chennai
    hey...very nice...actually...this is my first time im sending reply...so i dont know how to write code block..anywayz now onwards i will use themm...and more over im really impressed with "not using ASCII values"...very nice..that is the coding style i alwys wanted to develop...

    thanks for ur reply..and suggestions....
     

Share This Page