tags:

views:

56

answers:

2

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 :

EDITED CODE*

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
main(){  
  int digits;
  float imp=0;
  char* 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 :)

EDIT Error given atm is :

Argument no 1 of 'isdigit' must be of type 'const int', not '<ptr>char'

EDIT This code compiles fine, but crashes when an input is entered.

  #include<stdio.h>
  #include<stdlib.h>
  #include<ctype.h>
  main(){  
  int digits;
  float imp=0;
  char* alpha=0;
  do{

   printf("Enter input\n\n");
   scanf("\n%s",alpha);
   digits=(isdigit(alpha[0]));
   imp=atof(alpha);
  }while(digits==0);
 }
+1  A: 

You probably need to use %s instead of %c and to put it in char array (char*). You also probably get an error that you need to use const char* and not const char.

You don't want to read just one character - you want to read an entire string...

EDIT:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
main(){  
  int digits,i;
  float imp=0;
  char* alpha = malloc(100); /* 100 is for example */
  do{
    printf("Enter input\n\n");
    scanf("\n%s",&alpha);
    for (i = 0; i != 100; ++i)
    {
      if (alpha[i] == '\0')
        break;
      if (!isdigit(alpha[i]))
      {
        printf("error\n\n");
        return ...;
      }
    }
     imp=atof(alpha);
  }while(true);
}
brickner
Thanks for replying, I change %s to %c, got less errors. Now I have a new error about digits `Argument no 1 of 'isdigit' must be of type 'const int', not '<ptr>char'`
ZaZu
I've added an example for something closer to what you probably want.
brickner
Thank you very much for that structure .. I have made mine to compile with no errors .. but it crashes once I input anything, can you please check the edited code ? (see question)
ZaZu
brickner
Thanks for replying .. It says `An object of type '<ptr>void' cannot be assigned to an object of type '<ptr>char'` ...
ZaZu
+1  A: 

Why not have scanf do the atof conversion for you?

 #include <stdio.h>
 int main()
 {  
    float imp=0;
    while (1)
    {
       printf("Enter input\n\n");
       if (scanf("%f", &imp) < 1) break;
    }
    return 0;
}

Your most recent example is failing because alpha is a NULL pointer. Declare it as char alpha[40]; to allocate space for it. You'll probably want to use %40s in your format string to prevent scanf from overflowing alpha.

Also, use strtod instead of atof and you'll know whether the conversion was successful (better than your method of using isdigit which will fail on a negative integer).

tomlogic
Ahhh thank you very much !!! Your explanation got my code working !! I had to remove the NULL array as you mentioned, and prevent scanf from overflowing it !!! Legend, thank you:) :)
ZaZu