0

I have to write a program in C for a school project that registers a geriatric client, this is my code:

int position=0, correctData=0, day, month, year, phone, iRS, exercises;
float height, weight;
char confirmation, name[100], address[100], numberToCheck[9];
for(int i=0; Client[i]->name!="NULL";i++){
    position++;
    if(i==30){
        break;
    }
}

do{
    fflush(stdin);
    system("cls");
    printf("\n\tINSERT NEW CLIENT");
    printf("\nInsert client name: ");
    gets(name);
    printf("\nInsert client address: ");
    gets(address);
    printf("\nInsert client birthday(dd/mm/yyyy): ");
    scanf("%d/%d/%d", &day,&month,&year);
    do{
    fflush(stdin);
    strcpy(numberToCheck, "");
    printf("\nInsert client phone number: ");
    gets(numberToCheck); //THIS IS THE LINE ERASING MY ADDRESS VARIABLE
    if(isPhone(numberToCheck)==0){
        printf("\nNumber not valid");
    }
    }while(phone(numberToCheck)==0);
    phone= validateInput(numberToChek);
    do{
    fflush(stdin);
    strcpy(numberToCheck, "");
    printf("\nInsert client tax number: ");
    gets(numberToCheck);
    if(isIRS(numberToCheck)==0){
        printf("\nNumber not valid");
    }
    }while(isIRS(numberTocheck)==0);
    iRS= validateInput(numberToCheck);
    printf("\nInsert client height: ");
    scanf("%f", &height);
    printf("\nInsert client weight: ");
    scanf("%f", &weight);
    printf("\nInsert the number of exercises made: ");
    scanf("%d", &exercise);
    fflush(stdin);
    system("cls");
    printf("Name                    : %s", name);
    printf("\nAddress                 : %s", address);
    printf("\nBirthday                : %02d/%02d/%04d", dia, mes, ano);
    printf("\nPhone number            : %d", telefone);
    printf("\IRS                      : %d", contribuinte);
    printf("\nHeight and Weight       : %.2fm e %.2fKg", height, weight);
    printf("\nNumber of exercises     : %d", exercise);
    printf("\n\nIs the data correct??(Y/N): ");
    scanf("%c", &confirmation);
    if(confirmation=='Y' || confirmation=='y'){
            strcpy(Client[position]->name, name);
            strcpy(Client[position]->address, address);
            Client[position]->dayBirth = day;
            Client[position]->monthBirth = month;
            Client[position]->yearBirth = year;
            Client[position]->phone = phone;
            Client[position]->iRS = iRS;
            Client[position]->height= height;
            Client[position]->weight= weight;
            Client[position]->exercise = exercise;
        correctData = 1;
    }

    }while(correctData==0);

    printf("\nUser registered");
    printf("\n\nPress Enter to continue");
    system("pause >nul /nobreak");

I receive by reference the array Client[100] so that i can modify its elements. In the part of the code to check if it's a phone number/iRS number i use a pre-coded submodule to validate it.

The problem I'm having with the code is that it stores the address of the client but when the program receives the numberToCheck to check if it is a phone number the address variable is cleaned, becomes empty and I don't see why.

I appreciate any help and time given to help me solve the problem

  • 3
    You know, there's a *reason* why `gets` is one of the *few* things that have been explicitly deprecated and removed from the C standard :-) – paxdiablo Dec 16 '18 at 12:33
  • 2
    Never use `gets`. It has been obsoleted from the latest C standards since there is no safe way to use it. (If it is required by your teacher, the teacher is wrong.) Use `fgets` instead. – Arkku Dec 16 '18 at 12:33
  • 1
    First of all, never ***ever*** use `gets()`. It is always the wrong answer. For another thing, don't try to `fflush(stdin)`. – torstenvl Dec 16 '18 at 12:35
  • 2
    While `fflush(stdin)` works on some (even many) platforms, flushing an input stream (such as `stdin`) is undefined by the C standard and thus should not be something you learn to rely on. Unfortunately it is quite common to attempt to solve the `scanf` problem with `fflush(stdin)`, but it would be better to either use `fgets` exclusively (possibly followed by `sscanf`), or use `scanf` exclusively, adding a space to the beginning of each format string. `fgets` is easier to get right, IMO. – Arkku Dec 16 '18 at 12:38
  • 1
    Post the **exact** input used that causes trouble. – chux - Reinstate Monica Dec 16 '18 at 12:38
  • `numberToCheck[9]` is likely too small, try `numberToCheck[90]`. – chux - Reinstate Monica Dec 16 '18 at 12:40
  • thank you for all of the answers, I got it to work with fgets, I'm new to C so i didn't know that gets was obsolete. – David Abreu Dec 16 '18 at 12:43
  • 1
    See [The `gets()` function is too dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) Mixing `scanf()` calls with `gets()` calls is also problematic because [`scanf()` leaves the newline in the input stream](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-buffer). – Jonathan Leffler Dec 16 '18 at 18:12
  • the function: `gets()` has been depreciated for years. Earlier this century it was deleted from the C standard, Your compiler should have told you this. – user3629249 Dec 18 '18 at 02:41
  • instead of calling: `fflush( stdin )` use: `int ch; while( (ch = getchar() ) != EOF && ch != '\n' ) {;}` – user3629249 Dec 18 '18 at 02:43

0 Answers0