-3

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?

neeh
  • 2,777
  • 3
  • 24
  • 32

1 Answers1

0

It is simple- int total_age = atoi(user[0].age) + atoi(user[1].age) and so on till last record.

And you can convert the integer into string by using itoa() if you want to do so. Please use following read() function.

void read() {
    FILE *f;
    int total_age = 0;
    int i = 0;
    f = fopen("file.txt", "r");

    if (f == NULL) {
        printf("The file does not exist");
    } else {
        system("cls");
        while (!feof(f)) {
            fscanf(f,"%s %s %d", users[i].name,users[i].sname,users[i].age);
            total_age = total_age + (users[i].age);
            i++;
        }
        printf("Total age is %d",total_age);
        fclose(f);
    }
}

And change structure as follows-

 struct contact {
        char name[20];
        char sname[20];
        int age;
    };
Ganeshdip
  • 389
  • 2
  • 10
  • i try it but print total:0 i don't know why don't read the age of users – Gabriele Ardei Sep 09 '17 at 16:34
  • @GabrieleArdei - in your code you checked fopen == NULL but you should check f==NULL. – Ganeshdip Sep 09 '17 at 16:39
  • don't understand what you mean – Gabriele Ardei Sep 09 '17 at 16:43
  • if (fopen == NULL) - this is wrong.. write if(f==NULL) as your file pointer is f. And after fscanf you can use atoi as mentioned above in answer to calculate total. – Ganeshdip Sep 09 '17 at 16:46
  • but with if (fopen == NULL) works; now there is a way to do total without whrite int total_age = atoi(user[0].age) + atoi(user[1].age) and write for ecample total=atoi(user[i].age); (not work) – Gabriele Ardei Sep 09 '17 at 16:50
  • @GabrieleArdei - fopen is a function which will return value, But it is not good way to use it in comparison as used by you in your code. It can lead to undefined behavior. Please use read() as mentioned in answer.And let me know if you still have problem calculating total age. – Ganeshdip Sep 09 '17 at 17:04
  • work, the problem is how i can calculate the total automatically? without write atoi(user[0].age)+ for each user – Gabriele Ardei Sep 09 '17 at 17:39
  • Use this line and remove atoi- fscanf(f,"%s %s %d", users[i].name,users[i].sname,users[i].age); – Ganeshdip Sep 09 '17 at 18:24
  • i need to take only the age, so need atoi to do matematical calculations – Gabriele Ardei Sep 10 '17 at 16:28
  • @GabrieleArdei Please describe in details what do you want? – Ganeshdip Sep 11 '17 at 11:50
  • i try to use ou code but the program crash void read() { FILE *f; int total_age = 0; int i = 0; f = fopen("file.txt", "r"); if (f == NULL) { printf("The file does not exist"); } else { system("cls"); while (!feof(f)) { fscanf(f,"%s %s %d", users[i].name,users[i].sname,users[i].age); total_age = total_age + (users[i].age); i++; } printf("Total age is %d",total_age); fclose(f); – Gabriele Ardei Sep 11 '17 at 15:36
  • @GabrieleArdei Have you changed structure contact? Make age as int in it. – Ganeshdip Sep 11 '17 at 16:32