Menu

C problem with atof

2003-02-04
2012-09-26
  • Nobody/Anonymous

    Hello, I am new to programming and came acrossed a problem with some code and the Bloodshed C++ 4.9.7
    this is the code;
    #include <stdio.h>
    #include <conio.h>

    int main()
    {
           int age;
           float hat_size;
           float iq;
           char key[4];
          
           printf("how old are you?");
           gets(key);
           age=atoi(key);
          
           printf("What is your hat size?");
           gets(key);
           hat_size=atof(key);
          
           iq=age*hat_size;

           printf("you have an IQ of %.2f!\n",iq);
          
           return;     
           }

    simple little learning program that shoots out incorrect math, like;

    How old are you? 1
    What is your hat size? 1
    You have an IQ of 76015680.00

    the numbers got bigger with different inputs.
    My guess is there must be a problem with function atof and DEV-C++. Is there a workaround in C that gets me the desired results wich are correct decimal out puts.

    Jim G

     
    • Nobody/Anonymous

      Personally I use the compiler option -Wall
      It helps me find errors or inconcistensies in my code.

      Your code contains two errors.

      1) Missing #include of stdlib.h
      2) Missing return value on your 'return' line.

      These errors should make it impossible to compile and run the program. How did you do it?

      If corrected the program compiles and runs as expected.

      /Lars.

       
    • Anonymous

      Anonymous - 2003-02-04

      Thanks I replaced conio with stdio.h and that fixed it...duh
      I looked at everything but my header files....
      lame hu?

      thanks again

      jim g

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.