By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,876 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,876 IT Pros & Developers. It's quick & easy.

Converting string to float

P: 4
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:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define MAX_SIZE 80
  6.  
  7. void readString(char prompt[], char str[]);// Exercise 1
  8. float readNumber(char prompt[], char str[]);// Exercise 2
  9.  
  10. int main()
  11. {
  12.     char str[MAX_SIZE];
  13.         printf("You entered: %f\n", readNumber("Please enter a number: ", str));
  14. }
  15.  
  16. void readString(char prompt[], char str[]) //This asks the user for a string
  17. {
  18.     printf("%s", prompt);
  19.     gets(str);
  20. }//Exercise 1
  21.  
  22. float readNumber(char prompt[], char str[]) //This function will convert the string   in to a float using readString
  23. {
  24.     int flag = 0; //0 means no; 1 means yes
  25.     int i;
  26.     float number;
  27.  
  28.     readString(prompt, str);
  29.     sscanf(str, "%f", &number);
  30.  
  31.     if(number == 0)
  32.     {
  33.         while(flag == 0)
  34.         {
  35.             printf("Data entered was not a number, Try again.\n");
  36.             readString(prompt, str);
  37.             sscanf(str, "%f", &number);
  38.             if(number != 0)
  39.                 flag = 1;
  40.         }
  41.     }
  42.     return number;
  43. }//Exercise 2
  44.  
My problem is using sscanf. I'm not exactly sure how to use it so that it will only return a number if and only if the user enters a number. I'm guessing my if statements are incorrect as well, but I don't know how to use sscanf at all so I don't know how to check if the string only contains floats.
Nov 21 '07 #1
Share this Question
Share on Google+
7 Replies


Shashi Sadasivan
Expert 100+
P: 1,435
is the code returning any problems?
what you also need to consider is if the user enters 0 (zero) as input.
In that case your code will tell that the user did not enter a number.

Have a look at the return type of sscanf, your code of
Expand|Select|Wrap|Line Numbers
  1. if(number == 0)
will throw errors if the string entered by the user is not a number
Nov 21 '07 #2

P: 4
No, its just that when I enter 4F it will return 4.000000. I want it to return: "Data entered was not a number. Try again."

So, somehow I need it to use the sscanf function and make sure that when it converts the string to a float that the string ONLY contains numbers and if it doesn't it will ask the user to enter a new value for the string until the user enters ONLY numbers including floats.
So if I enter 4.0 it will return 4.00000
or if I enter -2 it will return -2.0000
but if I enter 4f it should return "Data entered was not a number. Try again." and prompt me for another number.

Hopefully you understand and can help me.
I'm guessing my problem lies in the if statement. I have no idea on how to check if the string is made up of ONLY numbers.

EDIT
I changed my code around a bit
Expand|Select|Wrap|Line Numbers
  1. float readNumber(char prompt[], char str[])
  2. {
  3.     int flag = 0; //0 means no; 1 means yes
  4.     int i;
  5.     float number;
  6.  
  7.     readString(prompt, str);
  8.     i = sscanf(str, "%f", &number);
  9.  
  10.     if(i != 1)
  11.     {
  12.         while(flag == 0)
  13.         {
  14.             printf("Data entered was not a number, Try again.\n");
  15.             readString(prompt, str);
  16.             i = sscanf(str, "%f", &number);
  17.             if(i == 1)
  18.                 flag = 1;
  19.         }
  20.     }
  21.     return number;
  22. }//Exercise 2
  23.  
I just changed the if statements. Now if the user enters 0 it will work. and if the user enters a character first than it will ask the user for another number. but if i have a character after the number it will just print out the number which should NOT happen.
Nov 21 '07 #3

100+
P: 121
I'm guessing my problem lies in the if statement. I have no idea on how to check if the string is made up of ONLY numbers.
Hi.

There is a potentially useful function in the ctype.h lib for your consideration:

Function: int isdigit (int c)
Returns true if c is a decimal digit (`0' through `9').

If you take the user input as a string, then you can use a loop to check each character to validate it.

Also you can use another function: strtol
http://www.gnu.org/software/libc/man...-Integers.html

BTW I took a look at your code, it is made up of exercise fragments, which is not a good idea. For keeping the top-down design, you should think the case to handle as a whole, then divide and conquer. The variable parameters in each of your functions don't make any sense up to now.

My suggestion is: try to finish the input validation part first, then think further...
Nov 21 '07 #4

P: 4
Hi.

There is a potentially useful function in the ctype.h lib for your consideration:

Function: int isdigit (int c)
Returns true if c is a decimal digit (`0' through `9').

If you take the user input as a string, then you can use a loop to check each character to validate it.

Also you can use another function: strtol
http://www.gnu.org/software/libc/man...-Integers.html
Is there a way to do this using only the sscanf function? I think my prof would get mad if I did it any other way.
He didn't really tell us what we could or could not do though so I don't know. It's just when I started to use atof() he yelled at me so I don't know. So if there is a way to do this with sscanf mainly then any pointers in the right direction would be a great help. For now I'll just use isdigit.
Nov 21 '07 #5

100+
P: 121
Is there a way to do this using only the sscanf function? I think my prof would get mad if I did it any other way.
He didn't really tell us what we could or could not do though so I don't know. It's just when I started to use atof() he yelled at me so I don't know. So if there is a way to do this with sscanf mainly then any pointers in the right direction would be a great help. For now I'll just use isdigit.
Emmm, that is pretty strange.

This is not a research at all, google the references online shouldn't be blamed for...

This is some references regarding sscanf, I am looking at it:
http://www.crasseux.com/books/ctutorial/sscanf.html
http://www.cplusplus.com/reference/c...io/sscanf.html




Yeah, you can use the return value of sscanf to check if input is valid or not.

if(sscanf (input, "%f", &number))
{
//do what ever you want to for the valid input.
}
else
{
//ask to give a new input.
}


BTW, the gcc compiler warns me that:
Expand|Select|Wrap|Line Numbers
  1. bash-3.2$ gcc test.c
  2. /tmp/ccqdvPdm.o: In function `takeInput':
  3. test.c:(.text+0x42): warning: the `gets' function is dangerous and should not be used.
Nov 21 '07 #6

P: 4
I still get it where if I enter 4f it returns 4
Expand|Select|Wrap|Line Numbers
  1. float readNumber(char prompt[], char str[])
  2. {
  3.     int flag = 0; //0 means no; 1 means yes
  4.     float number;
  5.  
  6.     readString(prompt, str);
  7.  
  8.     if(sscanf (str, "%f", &number))
  9.         return number;
  10.     else
  11.     {
  12.         while(flag == 0)
  13.         {
  14.             printf("Data entered was not a number, Try again.\n");
  15.             readString(prompt, str);
  16.             if(sscanf(str, "%f", &number))
  17.                 {
  18.                     flag = 1;
  19.                     return number;
  20.                 }
  21.         }
  22.     }
  23.  
  24. }//Exercise 2
  25.  
I'm not sure what the if statement should be checking for... Thanks for your help though.
Nov 21 '07 #7

100+
P: 121
I still get it where if I enter 4f it returns 4
Expand|Select|Wrap|Line Numbers
  1. float readNumber(char prompt[], char str[])
  2. {
  3.     int flag = 0; //0 means no; 1 means yes
  4.     float number;
  5.  
  6.     readString(prompt, str);
  7.  
  8.     if(sscanf (str, "%f", &number))
  9.         return number;
  10.     else
  11.     {
  12.         while(flag == 0)
  13.         {
  14.             printf("Data entered was not a number, Try again.\n");
  15.             readString(prompt, str);
  16.             if(sscanf(str, "%f", &number))
  17.                 {
  18.                     flag = 1;
  19.                     return number;
  20.                 }
  21.         }
  22.     }
  23.  
  24. }//Exercise 2
  25.  
I'm not sure what the if statement should be checking for... Thanks for your help though.
Hi.

Use this:

Expand|Select|Wrap|Line Numbers
  1. float num;
  2.     char *temp;
  3.     if(sscanf(str, "%f%*s", &num, temp))
This can solve that particular problem.
Nov 21 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.