How i can copy a char
to an int
to do mathematical calculations with this program in C.
I've written a file with fields name
, sname
and age
.
Example of file :
Antonio Giannini 14
Mimmo Cava 22
Luck Santo 33
The code:
struct contact {
char name[20];
char sname[20];
char age[20];
};
struct contact users[MAX];
/**
* Read the file.
*/
void read() {
FILE *f;
i = 0;
f = fopen("file.txt", "r");
if (fopen == NULL) {
printf("The file does not exist");
} else {
system("cls");
while (!feof(f)) {
fscanf(f,"%s", users[i].name);
fscanf(f,"%s", users[i].sname);
fscanf(f,"%s", users[i].age);
i++;
}
fclose(f);
}
}
/**
* Prints the content.
*/
void stamp() {
system("cls");
printf("%-10s%-10s%-10s\n\n", "name", "sname", "age");
for (j=0; j < i-1; j++) {
printf("%-10s%-10s%-10s\n\n", users[j].name, users[j].sname, users[j].age);
}
}
The code works till now but, now how I can take only the age
to do mathematical calculations like the total or the average or the oldest and youngest?