So I have a C program where you type a name, age and height as command line prompts and these arguments are written to a text file, however, with the height (which is a float), there's a problem. It writes down a value that is really high and isn't what you type in. I feel like there's a problem with the memory or something similar.
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
if (argc != 4) {
printf("Please enter four parameters");
return;
} else {
char name[20];
strcpy(name, argv[1]);
int age = atoi(argv[2]);
double height = atof(argv[3]);
FILE *fp;
fp = fopen("name.txt", "w+");
fprintf(fp, "%s\n%d\n%.2f", name, age, height);
fclose(fp);
printf("File written!");
return 0;
}
}
So what have I done wrong with the float height?
#include <stdlib.h>to useatoi()andatof().