Scanning, Checking, Converting, Copying values ... How to ? -- C --
Posted
by ZaZu
on Stack Overflow
See other posts from Stack Overflow
or by ZaZu
Published on 2010-05-12T18:26:39Z
Indexed on
2010/05/12
18:34 UTC
Read the original article
Hit count: 213
Hi there,
Its been a while now and im still trying to get a certain code to work. I asked some question about different commands etc. before, but now I hope this is the final one (combining all questions in one code).
I basically want to :
*Scan an input (should be character ? )
*Check if its a number
*If not, return error
*Convert that character into a float number
*Copy the value to another variable ( I called it imp here)
Here is what I came up with :
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
main(){
int digits;
float imp=0;
int alpha;
do{
printf("Enter input\n\n");
scanf("\n%c",&alpha);
digits=isdigit(alpha);
if(digits==0){
printf("error\n\n");
}
imp=atof(alpha);
}while(digits==0);
}
The problem is this code does not work at all ... It gives me that atof must be of a const char and whenever I try changing it around, it just keeps failing. I am frustrated and forced to ask here, because I believe I have tried alot and I keep failing, but I wont be relieved until I get it to work xD So I really need your help guys.
Please tell me why isnt this code working, what am I doing wrong ? I am still learning C and really appreciate your help :)
© Stack Overflow or respective owner