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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
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.
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