Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-06-2008, 04:43 AM
Cheer Cheer is offline
Newbie
 
Join Date: Mar 2008
Posts: 5
Credits: 0
Rep Power: 0
Cheer is on a distinguished road
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-06-2008, 08:01 AM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 7,055
Last Blog:
Web slideshow in JavaS...
Credits: 1,024
Rep Power: 58
Xav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to behold
Send a message via MSN to Xav
Default 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.
__________________
[TRUTH] TcM is the best moderator ever! [/TRUTH]
"Valid XHTML is like sex - everybody claims to have the same goal, but everybody has their own tricks and results vary wildly."

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-06-2008, 09:35 AM
Cheer Cheer is offline
Newbie
 
Join Date: Mar 2008
Posts: 5
Credits: 0
Rep Power: 0
Cheer is on a distinguished road
Default 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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-06-2008, 12:34 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 7,055
Last Blog:
Web slideshow in JavaS...
Credits: 1,024
Rep Power: 58
Xav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to behold
Send a message via MSN to Xav
Default 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
__________________
[TRUTH] TcM is the best moderator ever! [/TRUTH]
"Valid XHTML is like sex - everybody claims to have the same goal, but everybody has their own tricks and results vary wildly."

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-06-2008, 12:42 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 7,055
Last Blog:
Web slideshow in JavaS...
Credits: 1,024
Rep Power: 58
Xav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to behold
Send a message via MSN to Xav
Default 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.
__________________
[TRUTH] TcM is the best moderator ever! [/TRUTH]
"Valid XHTML is like sex - everybody claims to have the same goal, but everybody has their own tricks and results vary wildly."

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 04-06-2008, 07:50 PM
dcs dcs is offline
Programmer
 
Join Date: Mar 2008
Posts: 108
Credits: 147
Rep Power: 3
dcs will become famous soon enough
Default 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-07-2008, 06:02 AM
Goodluck Goodluck is offline
Newbie
 
Join Date: Mar 2008
Posts: 13
Credits: 0
Rep Power: 0
Goodluck is on a distinguished road
Default 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-07-2008, 06:44 AM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 7,055
Last Blog:
Web slideshow in JavaS...
Credits: 1,024
Rep Power: 58
Xav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to behold
Send a message via MSN to Xav
Default Re: C: how to check if the float is not a char

Good idea.
__________________
[TRUTH] TcM is the best moderator ever! [/TRUTH]
"Valid XHTML is like sex - everybody claims to have the same goal, but everybody has their own tricks and results vary wildly."

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-07-2008, 11:45 AM
dcs dcs is offline
Programmer
 
Join Date: Mar 2008
Posts: 108
Credits: 147
Rep Power: 3
dcs will become famous soon enough
Default 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+
*/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-07-2008, 12:00 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 7,055
Last Blog:
Web slideshow in JavaS...
Credits: 1,024
Rep Power: 58
Xav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to beholdXav is a splendid one to behold
Send a message via MSN to Xav
Default Re: C: how to check if the float is not a char

Interesting.
__________________
[TRUTH] TcM is the best moderator ever! [/TRUTH]
"Valid XHTML is like sex - everybody claims to have the same goal, but everybody has their own tricks and results vary wildly."

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem removing from ArrayList (affects Vector as well) chunkit Java Help 0 03-30-2008 10:36 PM
need someone to check my coding debug C and C++ 3 03-01-2008 10:22 AM
I need help please! zizimetalique C and C++ 5 11-17-2007 09:39 PM
Works when i have an int NoName C and C++ 1 08-27-2007 11:25 PM


All times are GMT -5. The time now is 12:25 AM.

Contest Stats

Xav ........ 1024.41
MeTh0Dz|Reb0rn ........ 974.08
morefood2001 ........ 850.04
John ........ 841.93
WingedPanther ........ 661.52
marwex89 ........ 575.59
Brandon W ........ 447.33
chili5 ........ 292.12
orjan ........ 187.41
Steve.L ........ 181.88

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 79%

Ads