-
floating point function
I am tring to make a function to check to make sure the floating piont value is a valid number. i am having some problem getting it to work correctly.
any help would be great.
thanks
Sal
Code:
#include <stdio.h>
#include "floatgetnum.h"
/* prototype */
float getnum();
int main()
{
float retval=0;
printf("Please enter a floating point number");
do
{
retval = getnum();
printf("The number was %f\n", retval);
}while (retval !=-1);
return 0;
}
float getnum()
{
char ch;
int nflag,dflag,number,pos=0;
int dplace=1;
ch=getchar();
while (ch!='\n'&&ch!=' ')
{
switch (ch)
{
case '-':
nflag=1;
pos++;
break;
case '+':
pos++;
break;
case '.':
if (dflag==1)
{
printf ("Invaild user input\n");
return EOF;
}
else
{
dflag++;
pos++;
}
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
number*=10;
number+=atoi(&ch);
if (dflag==1)
{
dplace*=10;
}
pos++;
break;
}
ch=getchar();
if (nflag==1)
{
number*=-1;
}
if (dflag==1)
{
number=number/dplace;
}
}
return number;
}/*function*/
-
Is this C or C++? If it is C you might get better responses on the C board, if it is C++ why are you using C tools?
What problems are you having? Do you get compile errors? Do you get different answers than you expect? What input do you give, what output do you expect and what output do you get?
One issue that I see is that you are using atoi on a single character. The atoi function expects a pointer to a null terminated array of characters, you are passing a pointer to a single character. If you want to convert a single character to an int, just subtract the '0' character:
Code:
number += ch - '0';
-
You never initialize the variable number in the getnum() function.
another thing
you cannot just take the address of a char variable and pass it to atoi(). Taking the address makes it a char * but doesn't make it a zero terminated string.
use
Code:
number += ch - '0';
instead
Kurt
-
it is tring to check for a valid floating number 12.34 , .1234, -1234
give back that input if valid return EOF if invaild.
i was told you could use
number+=atoi(&ch)
to send ch to atoi converting if need be.
-
You can't do it that way in your program for the reasons we mentioned.
Are you familiar with how a C style string works? Do you understand the difference between an array of characters and a single character?
-
no. You can't.
Kurt
EDIT: Sh.., too slow again. It's a shame. Twice in the same thread
-
the program works the same with both
it seems i need a little help with the difference array of characters and single ones.
-
> int nflag,dflag,number,pos=0;
Wouldn't it make more sense to declare number as a float or double?
-
>> the program works the same with both
Does that mean it still doesn't work? What problems are you having? Do you get different answers than you expect? What input do you give, what output do you expect and what output do you get?
>> it seems i need a little help with the difference array of characters and single ones.
For now, you can trust us that number += ch - '0'; is the correct version.
A string is just a sequence of 0 or more characters. In C, the way to store a string is with an array. However, you cannot always tell the size of an array just by looking at it. That means that in order to do real work with strings stored in character arrays, you would have to pass around the size to functions that work on strings. This would be cumbersome, so they used a trick to remember the size. They add a null character that signals the end of the string.
So when you use a function that expects a C style string (like atoi), it wants a pointer to an array of characters. It also wants the important characters to end with a null character: '\0'. For example, "123" is a string. It is represented as an array of 4 characters, '1', '2', '3' and '\0'. The atoi function gets a pointer to the '1', then keeps looking until it finds the '\0'. It then converts "123" to the number 123.
In your code, you are reading in a single character only. If the user types 123, then you read in '1' and process it. Since atoi is not meant for a single character, and since there is no '\0' after the '1', then atoi won't work for you. Instead you use the other trick we showed you.
-
if i enter a number '1', '2', '3' it reads and outputs like a float 123.00000
after that just locks up unitl i exit
i guess the real problem is that i want to pass an array of characters '1', '.', '2', '3'
-
You might want to initialize dflag too:
-
the whole line is set to 0
-
>> i guess the real problem is that i want to pass an array of characters '1', '.', '2', '3'
I don't think you want to pass an array of characters. Your algorithm is setup to read one character at a time.
>> the whole line is set to 0
No, that's not how initializations work, you have to initialize each one individually.
Can you show us your new code, with the proper initializations, with number declared as a float, and with ay other changes you've made?
-
It will return a the value you enter but will not put the decimal in the right spot.
if i enter 1234 i get 1234.000000
if i enter 12.34 i get 1234.000000
Code:
#include <stdio.h>
#include "floatgetnum.h"
/* prototype */
float getnum();
int main()
{
float retval=0;
do
{
printf("Please enter a floating point number");
retval = getnum();
printf("The number was %f\n", retval);
}while (retval !=EOF);
return 0;
}
float getnum()
{
char ch;
int pos=0;
int dplace=1;
int nflag=0;
int dflag=0;
float number=0;
ch=getchar();
while (ch!='\n'&&ch!=' ')
{
switch (ch)
{
case '-':
nflag=1;
pos++;
break;
case '+':
pos++;
break;
case '.':
if (dflag==1)
{
printf ("Invaild user input\n");
return EOF;
}
else
{
dflag+=1;
pos++;
}
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
{ number*=10;
number += ch - '0';
break;
}
if (dflag==1)
{
dplace*=10;
}
pos++;
break;
}
ch=getchar();
}
if (nflag==1)
{
number*=-1;
}
if (dflag==1)
{
number = number / dplace;
}
return number;
}/*function*/
-
TBH, I would just read the number up to the '.' as an integer. I would read what comes after it as a second integer. So if the user enters 12.34, I would have 2 ints: 12 and 34.
Then using basic math, combine them to a float: 12 + ((float)34/100) = 12.34