Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: C: how to check if the float is not a char

  1. #1
    Cheer is offline Newbie
    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    0

    Question C: how to check if the float is not a char

    Hello again,

    I would like to ask how could I check if the input is right:
    the program asks user to input a float from a keybord, but what if user inputs a char. Is there any funcion which can check that?

    Thanks in advance.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: C: how to check if the float is not a char

    Use float.TryParse() to see if the value is a float. If it is, the function returns "true", if not, "false". You can then use an "if" statement to determine whether or not to actually go ahead with whatever you want to do with the float.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  4. #3
    Cheer is offline Newbie
    Join Date
    Mar 2008
    Posts
    5
    Rep Power
    0

    Re: C: how to check if the float is not a char

    But float.TryParse() is a part of C++, it cannot be used in C or am I wrong?

  5. #4
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: C: how to check if the float is not a char

    Maybe. I've honestly got no idea. If you're using .NET (like all the Microsoft Express Editions), it should be the same across all languages. Try it, and if it works, great. If not, then there is one easy way to do it - use a try/catch block.

    In this way, if the user types a value that is incompatible, when you try to use the data, it will generate an error, as usual. This time, however, the application will not crash, as you have effectively told the program to expect an error.

    I'm not too sure about C, but here it is in C#:
    Code:
    try
    {
       //Use your code to get the input here.
       //Here's one example - use your own one.
       float f = (float) Console.ReadLine();
    
       //If the code execution gets here, we know it was successful.
       Console.WriteLine("Successful.");
    }
    catch (Exception exc)
    {
       //This code only runs if there was an error in the "try" section.
       //If so, we know the conversion was not successful.
       Console.WriteLine("You did not enter a float value, stupid.");
       Console.WriteLine("The error message was: " + exc.Message);
    }
    Presumably the process is similar in C, and hopefully you can work out how to write it in your language. Bear in mind, however, that the try block will activate with ANY error, not necessarily an error related to the data conversion. If you change the word "Exception" in the catch() bit to "InvalidCastException" then this should only target this specific error.

    Hope this helps,

    Xav

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  6. #5
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: C: how to check if the float is not a char

    I've just had a thought - you said that the float.TryParse() was a C++ function. I've used the function in Visual Basic and C#, and it works fine. I used the Microsoft Visual Studio Express editions, which are slightly different from normal code, as the code relies on the Microsoft .NET Framework to run. This is known as "managed code".

    Any language that you are writing for the .NET framework will have access to the .NET functions, so you should be able to access it from C. Otherwise, I can't guarantee it. Unfortunately, there is no C version, but there are C++ and C# versions, available from the links provided.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  7. #6
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: C: how to check if the float is not a char

    In C, take user input as a string. Then attempt to convert it to a float (perhaps using strtod). If conversion was successful, you had a float -- as a bonus, you get the converted result.

  8. #7
    Goodluck is offline Newbie
    Join Date
    Mar 2008
    Posts
    16
    Rep Power
    0

    Re: C: how to check if the float is not a char

    Try ASCII values and when user enters an unwanted character then please check whether it matches with the respective ASCII value. For capital alphabets you can specify the range 65-90 and for small alphabets it is 97-122 using if condition. There are ASCII values for special characters also. Each character in C is internally represented by it's ASCII value.

  9. #8
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: C: how to check if the float is not a char

    Good idea.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  10. #9
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: C: how to check if the float is not a char

    Quote Originally Posted by Goodluck View Post
    Try ASCII values and when user enters an unwanted character then please check whether it matches with the respective ASCII value. For capital alphabets you can specify the range 65-90 and for small alphabets it is 97-122 using if condition. There are ASCII values for special characters also. Each character in C is internally represented by it's ASCII value.
    This is actually a large step backwards from using the standard library functions. C is not tied to ASCII, although other implementations I imagine are rare. Using the numeric equivalents instead of 'a' (for example) only looks cool to newbs.

    But floating point values can contain several nondigit characters in certain locations. The string "-123.45e+6" represents a valid floating-point number.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
       const char *text[] = 
       {
          "-123.45e+6",
          "-123.45.e+6",
          "+123.45e-6",
          "+123.45e-6+",
       };
       size_t i;
       for ( i = 0; i < sizeof text / sizeof *text; ++i )
       {
          char *end;
          double value = strtod(text[i], &end);
          if ( *end == '\0' )
          {
             printf("value = %g\n", value);
          }
          else
          {
             printf("failed to convert %s\n", text[i]);
          }
       }
       return 0;
    }
    
    /*
    value = -1.2345e+008
    failed to convert -123.45.e+6
    value = 0.00012345
    failed to convert +123.45e-6+
    */

  11. #10
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: C: how to check if the float is not a char

    Interesting.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. converting int, float, char into bool
    By jackson6612 in forum C and C++
    Replies: 10
    Last Post: 05-19-2011, 11:28 AM
  2. Replies: 6
    Last Post: 02-16-2011, 06:33 PM
  3. Re: check if input is int float or char
    By ApprentiC in forum C and C++
    Replies: 1
    Last Post: 02-11-2011, 01:31 AM
  4. check if input is int float or char
    By raonavneet in forum C and C++
    Replies: 3
    Last Post: 07-15-2008, 10:49 AM
  5. Converting float to char[4]
    By Walle in forum C and C++
    Replies: 9
    Last Post: 03-21-2008, 11:35 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts