首页 > 解决方案 > C gets() 命令正在删除变量数据

问题描述

我必须为一个注册老年客户的学校项目用 C 语言编写一个程序,这是我的代码:

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");

我通过引用数组 Client[100] 接收,以便我可以修改它的元素。在检查它是否是电话号码/iRS 号码的代码部分中,我使用预编码的子模块来验证它。

我遇到的代码问题是它存储了客户端的地址,但是当程序收到 numberToCheck 以检查它是否是电话号码时,地址变量被清除,变为空,我不明白为什么。

我感谢任何帮助和时间帮助我解决问题

标签: cstructgets

解决方案


推荐阅读