Hi
Is there anyway to take a number out of a char and use it in a calculation.
Say someone enters a char of form Character#.#
is there a command to take the number out?
for example:
A2.1 -> 2.1
the 2.1 can then be used in a calculation?
Printable View
Hi
Is there anyway to take a number out of a char and use it in a calculation.
Say someone enters a char of form Character#.#
is there a command to take the number out?
for example:
A2.1 -> 2.1
the 2.1 can then be used in a calculation?
Yes. There's three choices:
[str = char array containing your string, d a double]
1. Use sscanf(str, "A%lf", &d);
2. strtod() passing in the relevant part of your string (check for the start of digits or some such)
3. parse the string yourself.
--
Mats
you might want to finish the psuedocode first its due before the code :)
i saw this:
atof (yourString)
but i dunno if that works for my case?
hahaha well that can be done, jus dunno how to use the f1.2 etc in a calculation, do you know wat the formula for exposure time is @juststartedC
Yes, I missed out atof() as that is essentially a "worse" version of "strtod" - worse in that it doesn't deal with errors very well.
--
Mats
You need to work on your terminology. A 'char' is a single ASCII letter: 'A', 'f', '1', '%' are all chars, what you're talking about are 'strings' or 'char arrays'.
QuantumPete
P.S. Please search the forums first before you post, we've covered that topic extensivly with one of your classmates.
Ok so basically:
sscanf(str,"A%lf",&charss); //Do i have to use sscanf or can i use fscanf?
strtod(charss);
printf("your number in char is: %lf",charss);
?
hahahaha, then the exam comes and youll find it hard wink
----------------------------------
hey matsp, so does this look like the programming:
sscanf(str,"A%lf",&charss); //Do i have to use sscanf or can i use fscanf?
strtod(charss);
printf("your number in char is: %lf",charss);
?
It was sscanf() or strtod(). Don't use both.
sscanf() parses a string, whereas fscanf() reads from files. So if you have the string ready-read in, such as with fgets(), then use sscanf(). If you're reading from a file stream, you could use fscanf() (or plain old scanf() for stdin), but it's harder to recover from errors with fscanf(), and I'd recommend fgets() + sscanf() instead.Code:sscanf(str,"A%lf",&charss); //Do i have to use sscanf or can i use fscanf?
strtod takes two parameters: http://www.cplusplus.com/reference/c...ib/strtod.html
Here are some examples:
or, if you don't care about the character,Code:char c;
double d;
sscanf(str, "%c%lf", &c, &d);
printf("Character: %c Number: %f\n", c, d);
strtod() (assuming that str[0] is a character and str[1] is the start of the number):Code:double d;
sscanf(str, "%*c%lf", &d);
printf("Number: %f\n", d);
If you use sscanf(), consider checking its return value to make your program more robust.Code:double d = strtod(&str[1], NULL);
printf("Character: %c Number: %f\n", str[0], d);
Following what dawks said before here is a sample code to understand how to use the strtod function.
Bokarinho.Code:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Get a string from input.
char *GetBuffer(int Buf_Size)
{
char *Buffer = malloc(Buf_Size * sizeof(char));
if(Buffer == NULL)
return NULL;
else
{
if(fgets(Buffer, Buf_Size, stdin) == NULL)
{
free(Buffer);
return NULL;
}
else
{
if(Buffer[strlen(Buffer)-1] == '\n')
{
Buffer[strlen(Buffer)-1] = '\0';
return Buffer;
}
}
}
}
//Use of strtod.
double GetDouble(char *Buffer)
{
if(Buffer == NULL)
{
printf("NULL Buffer.\n");
return 0.0;
}
else
{
//Variables.
int i;
char *endPtr = NULL;
double retVal = 0.0;
//Use here.
for(i = 0; i < strlen(Buffer) && !isdigit(Buffer[i]); i++);
if(i == strlen(Buffer))
{
printf("No number was found to convert from buffer.\n");
return 0.0;
}
else
{
//The for loop stops when the first digit is found.
retVal = strtod(&Buffer[i], &endPtr);
if(retVal == 0.0)
{
printf("No valid conversion was made.\n");
return 0.0;
}
else
return retVal;
}
}
}
int main(int argc, char *argv[])
{
char *Buf = GetBuffer(1024);
printf("%lf\n", GetDouble(Buf));
free(Buf);
system("PAUSE");
return 0;
}
Some notes about Bokarinho's code:
- system("PAUSE") is unportable and should be avoided if possible.
- Casting malloc() is a bad idea. I wouldn't if I were you.
- sizeof(char) is always 1, so you could leave it out if you wanted to.
- strlen() is expensive (in terms of time) to compute. I'd compute it once, store the result in a variable, and use that rather than using strlen(Buffer) all of the time.
- //-style comments are C99, though many C89 compilers support them.
- isdigit() is in <ctype.h>, and you should include it.
There's no need for the else there. The return statement takes care of that.Code:double GetDouble(char *Buffer)
{
if(Buffer == NULL)
{
printf("NULL Buffer.\n");
return 0.0;
}
else
{
- Your indentation makes it difficult to see which else belongs to which if in GetDouble().
Buf_Size should perhaps be a size_t.Code:char *GetBuffer(int Buf_Size)
Thanks for your comments.
1. I will stop casting malloc, its a University habit a teacher always casting malloc in C, still does though i have told him why not.
2. The code i write is in a way very fast and typical, i want to help not to sell a 'perfect project' to someone.
3. About strlen that was something i never had in mind, thanks for the tip, i will use it.
4. Oh my god even the comments bother you??????
5. The else statement goes(it is no harm to use else statements in your code) to help my mind work better and never forget the previous code, but if there is wrong with that i will stop it too.
6. On closer look i mispeled your name i am curious how you couldnt see that too.......
7. I am not an advanced C programmer, i want to state that, i have some C knowledge i want to help other people solve their problems.
However, thanks for the comments i always learn from my mistakes.
4. Well, not really. It's just something you should keep in mind.
5. No, nothing wrong with it. If it's more readable to you, by all means use it.
6. I'm used to having my name misspelled. If someone has an unpronounceable name, it's bound to happen. And if my name is misspelled, it's not going to cause a program to crash or run slowly or be more difficult to read. :) But yes, I would appreciate it if you spelled it properly.
7. It's really quite a good program, using dynamic memory allocation and functions and good error handling. I was just in a pedantic mood. ;)