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: -
#include <stdio.h>
-
#include <string.h>
-
#include <stdlib.h>
-
-
#define MAX_SIZE 80
-
-
void readString(char prompt[], char str[]);// Exercise 1
-
float readNumber(char prompt[], char str[]);// Exercise 2
-
-
int main()
-
{
-
char str[MAX_SIZE];
-
printf("You entered: %f\n", readNumber("Please enter a number: ", str));
-
}
-
-
void readString(char prompt[], char str[]) //This asks the user for a string
-
{
-
printf("%s", prompt);
-
gets(str);
-
}//Exercise 1
-
-
float readNumber(char prompt[], char str[]) //This function will convert the string in to a float using readString
-
{
-
int flag = 0; //0 means no; 1 means yes
-
int i;
-
float number;
-
-
readString(prompt, str);
-
sscanf(str, "%f", &number);
-
-
if(number == 0)
-
{
-
while(flag == 0)
-
{
-
printf("Data entered was not a number, Try again.\n");
-
readString(prompt, str);
-
sscanf(str, "%f", &number);
-
if(number != 0)
-
flag = 1;
-
}
-
}
-
return number;
-
}//Exercise 2
-
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.
7 4403
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
will throw errors if the string entered by the user is not a number
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 -
float readNumber(char prompt[], char str[])
-
{
-
int flag = 0; //0 means no; 1 means yes
-
int i;
-
float number;
-
-
readString(prompt, str);
-
i = sscanf(str, "%f", &number);
-
-
if(i != 1)
-
{
-
while(flag == 0)
-
{
-
printf("Data entered was not a number, Try again.\n");
-
readString(prompt, str);
-
i = sscanf(str, "%f", &number);
-
if(i == 1)
-
flag = 1;
-
}
-
}
-
return number;
-
}//Exercise 2
-
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.
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...
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.
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: - bash-3.2$ gcc test.c
-
/tmp/ccqdvPdm.o: In function `takeInput':
-
test.c:(.text+0x42): warning: the `gets' function is dangerous and should not be used.
I still get it where if I enter 4f it returns 4 -
float readNumber(char prompt[], char str[])
-
{
-
int flag = 0; //0 means no; 1 means yes
-
float number;
-
-
readString(prompt, str);
-
-
if(sscanf (str, "%f", &number))
-
return number;
-
else
-
{
-
while(flag == 0)
-
{
-
printf("Data entered was not a number, Try again.\n");
-
readString(prompt, str);
-
if(sscanf(str, "%f", &number))
-
{
-
flag = 1;
-
return number;
-
}
-
}
-
}
-
-
}//Exercise 2
-
I'm not sure what the if statement should be checking for... Thanks for your help though.
I still get it where if I enter 4f it returns 4 -
float readNumber(char prompt[], char str[])
-
{
-
int flag = 0; //0 means no; 1 means yes
-
float number;
-
-
readString(prompt, str);
-
-
if(sscanf (str, "%f", &number))
-
return number;
-
else
-
{
-
while(flag == 0)
-
{
-
printf("Data entered was not a number, Try again.\n");
-
readString(prompt, str);
-
if(sscanf(str, "%f", &number))
-
{
-
flag = 1;
-
return number;
-
}
-
}
-
}
-
-
}//Exercise 2
-
I'm not sure what the if statement should be checking for... Thanks for your help though.
Hi.
Use this: - float num;
-
char *temp;
-
if(sscanf(str, "%f%*s", &num, temp))
This can solve that particular problem.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Bushido Hacks |
last post by:
Hey c.l.c++ and/or c.g.a.opengl posters,
How do I convert a hexidecimal string, traditionally used for defining
colors with HTML, into a floating point array?
In other words, how do I convert...
|
by: Meya-awe |
last post by:
Hello there,
I am using a 3rd party library and a function which requires a float
value in one of its methods. My data is in a string format and i tried
using Convert.ToDouble and...
|
by: vivekaseeja |
last post by:
Hi ,
Trying to convert a string value to a float value after reading the
value from an XML file , but not sure what function to use. The
following only works for integers
Int32.Parse...
|
by: Jim Langston |
last post by:
I have a CSkill class which is rather complex as it is recursive. That is:
class CSkill
{
public:
CSkill( std::string Name, float Value ): Name_( Name ), Value_( Value )
{};
void Update(...
|
by: pipe.jack |
last post by:
I'm trying to convert string to float and my float after conversion is
0 (zero). Here is my code:
$c = $currencies->format($cart->show_total());
echo gettype($c);
echo (float)$c;
$c = 39.59...
|
by: psbasha |
last post by:
Hi ,
When ever we read any data from file ,we read as a single line string ,and we convert the respective field data available in that string based on the data type ( say int,float ).
...
|
by: joestevens232 |
last post by:
Okay, Im having some problems with my code. Im trying to use the <cstdlib> library and im trying to convert string data at each whitespace slot. I think if you see my code you'll get what im trying...
|
by: Hank Stalica |
last post by:
I'm having this weird problem where my code does the following
conversion from string to float:
27000000.0 -27000000.00
2973999.99 -29740000.00
2989999.13 -2989999.25
The number on the left...
|
by: ma740988 |
last post by:
Consider the equation (flight dynamics stuff):
Yaw (Degrees) = Azimuth Angle(Radians) * 180 (Degrees) /
3.1415926535897932384626433832795 (Radians)
There's a valid reason to use single...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |