473,902 Members | 5,194 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

verify float number

Hey everybody. I need help on this one. I need to verify that a number
entered by a user is not either a negative number (-100.00), or an
alphabet (a, b, c, X, Y) as well as other number other than positive
integers or a decimal point. For example:

Enter amount:

and was capturing the float varialbe as in:

scanf ("%f", &myVar)

I was using scanf to capture the data, but I'm having a hard time
verifying this float with isdigit or isalpha. Any ideas would be
greatly appreciated.

Thanks

Oct 23 '06
43 6603
Xancatal wrote:
>>I compiled the code myself, and it compiled with no errors or
warnings. Perhaps Xancatal is doing something odd (using something
other than a C compiler, or compiling something other than the posted
source), but I have no idea what. If Xancatal can give us some more
details, we might be able to help.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Keith,

Thanks for your insight. I'm using a toy compiler as you can see. I
will try this out with a better compiler. I have Sun Studio 11 in
another system, but is quite complicated to compile and run simple
programs, for it develops more robust software
What's complicated about cc filename.c?

--
Ian Collins.
Oct 26 '06 #31

Ian Collins wrote:
>
What's complicated about cc filename.c?

--
Ian Collins.
I'm using the GUI Ian.

Oct 26 '06 #32
Xancatal said:
>
Ian Collins wrote:
>>
What's complicated about cc filename.c?

I'm using the GUI Ian.
I sympathise. GUIs do make computers much harder to use, don't they?
Fortunately, they are not always compulsory.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 26 '06 #33
In article <11************ **********@m7g2 000cwm.googlegr oups.com>,
Xancatal <pe**********@g mail.comwrote:
>What's complicated about cc filename.c?
>I'm using the GUI Ian.
Doctor, it hurts when I do this...

-- Richard

Oct 26 '06 #34
Richard Heathfield wrote:
Xancatal said:
>Ian Collins wrote:
>>>
What's complicated about cc filename.c?

I'm using the GUI Ian.

I sympathise. GUIs do make computers much harder to use, don't
they? Fortunately, they are not always compulsory.
Well put. I prefer to tell the machine what to do rather than to
have it give me permission to do limited things.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
Oct 27 '06 #35

Xancatal wrote:
Ian Collins wrote:
What's complicated about cc filename.c?

I'm using the GUI Ian.
If you find the GUI hard to use, why on earth do you use it? Why not
just follow Ian's comprehensive instructions?

Oct 27 '06 #36

Xancatal wrote:
Hey everybody. I need help on this one. I need to verify that a number
entered by a user is not either a negative number (-100.00), or an
alphabet (a, b, c, X, Y) as well as other number other than positive
integers or a decimal point. For example:

Enter amount:

and was capturing the float varialbe as in:

scanf ("%f", &myVar)

I was using scanf to capture the data, but I'm having a hard time
verifying this float with isdigit or isalpha. Any ideas would be
greatly appreciated.

Thanks
So basically, you need to verify that an input is a properly formed,
positive floating point value.

The easy way would be to read the input as a string with fgets(), then
use strtod() to both convert it to a double value and to verify that
the input is valid, and then test that it's positive:

#include <stdio.h>
#include <stdlib.h>

#define MAXBUF ... // large enough to hold largest input

int main(void)
{
char inBuf[MAXBUF+1];
char *endInputChar;
double inValue;

printf("Gimme a number: ");
fflush(stdout);

if (fgets(inBuf, sizeof inBuf, stdin) != NULL)
{
inValue = strtod(inBuf, &endInputCha r);
/**
* endInputChar points to the first character in the inBuf
* string that is *not* converted to a floating-point
* value by strtod(). If this character is whitespace or 0,
* the string is a valid float. If the character is anything
* else, then the string is not a valid float.
*/
if (isspace(*endIn putChar) || *endInputChar != 0)
{
if (inValue 0.0)
{
printf("\"%s\" is a valid, positive floating-point
value\n", inBuf)
}
else
{
printf("\"%s\" is not a positive-valued floating-point
value:\n",
inBuf, );
}
}
else
{
/**
* strtod() encountered an invalid character, which
* is pointed to by endInputChar.
*/
printf("\"%s\" is not a valid floating-point number: bad
character '%c'\n",
inBuf, *endInputChar);
}
}
else
{
printf("Error occurred while reading standard input\n");
}

return 0;
}

The slightly harder way is to write a simple lexer that scans the
string one character at a time and changes state based on the
character. It's a bit more work (enough so that I won't provide an
example at this time; maybe later when I'm not supposed to be getting
real work done), but it allows you to constrain the input to your
specific needs (for example, you may not want to allow input such as
1.0e3, which the above code will convert without complaint).

Oct 27 '06 #37
On 25 Oct 2006 01:01:24 -0700, "Nick Keighley"
<ni************ ******@hotmail. comwrote:
<in sig>
Soon I moved onto the hard stuff - computed goto. Lord, that was cool.
I
could jump to any of a dozen places with one line of code. Talk about
POWER!!
Richard Heathfield "goto: Just Say No."
Maybe a dozen places, or even more, but only as many as you can write
out in one statement, and anyone can easily find the list in a single
place, even a reviewer or maintainer or other such troublemaker.

Now, _assigned_ goto, that's the ticket. And usually with only little
effort you can trash the bits and go to places that aren't labels, nor
even statements, and if you're lucky nor even instructions.

<G!>

- David.Thompson1 at worldnet.att.ne t
Nov 20 '06 #38
>On 25 Oct 2006 01:01:24 -0700, "Nick Keighley"
><ni*********** *******@hotmail .comwrote:
<in sig>
>Soon I moved onto the hard stuff - computed goto. Lord, that was cool.
I
could jump to any of a dozen places with one line of code. Talk about
POWER!!
Richard Heathfield "goto: Just Say No."
In article <po************ *************** *****@4ax.com>,
Dave Thompson <da************ *@worldnet.att. netwrote:
>Maybe a dozen places, or even more, but only as many as you can write
out in one statement, and anyone can easily find the list in a single
place, even a reviewer or maintainer or other such troublemaker.

Now, _assigned_ goto, that's the ticket. And usually with only little
effort you can trash the bits and go to places that aren't labels, nor
even statements, and if you're lucky nor even instructions.

<G!>
Alas, C lacks assigned goto (although GNUC provides it, with
the unary && operator to take the address of labels).

For similar destructive power, there is always COBOL with its ALTER
statement.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 20 '06 #39
Chris Torek said:

<snip>
>
Alas, C lacks assigned goto (although GNUC provides it, with
the unary && operator to take the address of labels).
setjmp comes pretty close, doesn't it?
For similar destructive power, there is always COBOL with its ALTER
statement.
....and its prohibition almost invariably appears on page 1 of any COBOL
in-house coding standards document. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 20 '06 #40

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
8002
by: Bryan R. Meyer | last post by:
I am a relatively new C++ programmer and am attempting to write a function that takes a number of type float and adds commas to it in the appropriate places. In order to manipulate the number to add the commas, I convert it to a string (specifically a char rather than an actual string object) using the gcvt function as shown below. char amt; gcvt(amount,50,amt);
0
1760
by: Support | last post by:
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40"> <head> <meta http-equiv=Content-Type content="text/html; charset=windows-1252"> <meta name=ProgId content=Word.Document> <meta name=Generator content="Microsoft Word 11"> <meta name=Originator content="Microsoft Word 11">
2
32351
by: Goran | last post by:
Hi! I need to convert from a unsigned char array to a float. I don't think i get the right results in the program below. unsigned char array1 = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in what order the data is stored so i try both ways. unsigned char array2 = { 0x23, 0x44, 0xc2, 0xde}; float *pfloat1, *pfloat2;
2
1519
by: Chris | last post by:
Hi The following code is giving strange results........... float fTest = 536495.61f; cout << _T("float: ") << fTest << endl; int dec, sign; char* pszTest = _fcvt(fTest, 10, &dec, &sign); cout << _T("string: ") << pszTest << endl;
2
5273
by: Wayne Wengert | last post by:
I want to write a Windows application to go through all the email addresses in an SQL Server table and to report which ones are invalid. From Googling and perusing NGs it is my understanding that the process to validate an email address is done at 3 levels: 1. Verify that it is syntactically valid 2. Verify that the domain exists (SMTP verification) 3. Verify that the email address exists at that domain (MX verification) The first one I...
6
7621
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards, Karthi
7
30457
by: Partho | last post by:
I have a float variable which I need to add to a short variable. How do I do this? Do I have to typecast or is there a way around? I tried typecasting the float to a short, but that gives me a 0 or +/-1, which is unacceptable. Any pointers to how to get over this?
7
4491
by: DirtyRasa | last post by:
Here is what my professor told me to to. Write a function that converts a string to a float and returns a float. Test your function by entering f4 and with 4f. Both should say Data entered was not a number. Try again. and ask the user to type in another number. Here's what I have so far. Code:
2
5487
by: alishaikhji | last post by:
I am working on a program which will need several different integer and float random numbers at different stages, for example: - At one point, I need a random number (float) in the range 0.1 to 10.0 - At one point, I need a random number (float) in the range 0.5 to 1.5 - At one point, I need a random number (float) in the range 0.3 to 3.0 - At one point, I need a random number (float) in the range 0.1 to 10.0 Also, I need to make it sure...
0
9845
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10981
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9675
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7205
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4725
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3323
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.