Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command Line Input Working Incorrectly

Tags:

c

Okay so I made this program to help me out with my homework and because I wanted to improve my C expertise. Everything compiles fine when I do "gcc file.c -lm", but when I run it with a number at the command line as an argument, my program only returns 70.0000.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>


double temp(double hour){
    double t = (3.14/12)*hour;
    double c = cos((double)t);
    double temp = 13 * c + 57;
    return temp; 
}

int main ( int argc, char *argv[]){
    double temperature = temp((double)atol(argv[0]));

    printf("%f\n", temperature);
}
like image 386
Breedly Avatar asked Feb 02 '12 01:02

Breedly


1 Answers

argv[0] is probably your program name. You want argv[1], I expect. Check out this tutorial for a quick and easy explanation.

Besides that, is there a reason you're using atol(3) and casting to double rather than just using atof(3) which returns a double directly?

like image 114
Carl Norum Avatar answered Oct 02 '22 21:10

Carl Norum