-
Convert char to float
is there way you can convert a char into a float.I have some code but it's not working.
Code:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main()
{ char ch;
int flag;
printf("Type text, terminate with EOF\n");
flag = scanf("%c", &ch);
while (ch != '\n') {
printf("%c", ch);
flag = scanf("%c", &ch);
float aaa=(atof("ch"));
printf("%1.0f",aaa);
}
}
-
maybe you can use this link, http://www.cppreference.com/stdstring/atof.html. It seems like atof is not working the way you want it to.
-
You want to convert a string to a double, not a single character.
Read the input from the user into a char array, not a char.
You are doing atof("ch")... The quotes mean you are passing a literal string that contains the letters c and h, not the variable ch.
-
Here is the related FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284385
Option 3 seems to be the most useful.
-
Code:
float aaa=(atof("ch"));
look, atof fucntion is basically used to convert the string to float. in your case u have declared ch as single char. with the above code its really means that u are sending the litral vaue Ch to the fucntion ch. which u are really dont want that. u can use type cast thing to that.
Code:
#include<stdio.h>
int main()
{
char ch;
float res;
printf("Enter a char\n?");
scanf("%c",&ch);
res= (float)ch; // type casting
printf("%f",res);
getchar();
return 0;
}
/*my ouput
Enter a char
?g
103.000000
Enter a char
?4
52.000000
*/
ssharish2005
-
thanks to all those who are helping me. cwr we are not allowed to use arrays in our assign.ssharish2005 we are suppose to read characters but the value should not change for example if I put 1 I should get 1 back in return.dwks we are not allowed to use arrays and we have to use scanf. I am sorry if I was not clear in the requirements. Thanks for helping I am still trying to figure this out so if you have more suggestion let me know.
-
is this is the one are u wanted
Code:
#include<stdio.h>
int main()
{
char ch;
float res;
printf("Enter a char\n?");
scanf("%c",&ch);
res= (float)ch - '0'; // type casting
printf("%f",res);
getchar();
return 0;
}
/*my ouput
Enter a char
?8
8.000000
Enter a char
?6
6.000000
*/
ssharish2005
-
thanks. this program only reads the first char. so I implemented it in my program and now it doesnot work.
Code:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main()
{ char ch;
int flag;
float res;
printf("Type text, terminate with EOF\n");
flag = scanf("%c", &ch);
while (ch != '\n') {
printf("%c", ch);
flag = scanf("%c", &ch);
res= (float)ch - '0';
printf("%f",res);
}
return 0;
}
-
you have to accumulate the result, like this. Multiply current value by 10 to upen space for new digit.
Code:
float res = 0;
..
...
res= (res * 10.0F) + (float)(ch - '0');
-
thanks that worked!. I have one more question how can I check if the chracters entered by the user is numeber or a chracter.I have something like this
Code:
if( dcount>2 || (!isdigit(scanPrice)) || scanPrice!= '\n'){
printf("Illegal entry: press enter to reenter the price .\n");
continue;
}
else{
result=aft+pre;
fflush(stdin);
printf ("Enter item name: ");
scanf ("%s", &scanItem);
printf("%s%s%f",&scanItem," ",result);
add(result,scanItem);
}
numOfCharsInC = 0;
options();
This is not working.
-
isdigit() works on a single character, not a float or, double or other data types. just use it for each character entered
Code:
..
...
while (ch != '\n') {
printf("%c", ch);
flag = scanf("%c", &ch);
if( !isdigit(ch))
printf("Error\n");
else
res= (res * 10) + (float)ch - '0';
}
...