c - 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 以检查它是否是电话号码时,地址变量被清除,变为空,我不明白为什么。
我感谢任何帮助和时间帮助我解决问题
解决方案
推荐阅读
- django - 如何响应 django ckeditor
- android - 在 NavigationView 上 colorSurface 和背景发生冲突
- python - 如何在 selenium python 中 xpath 两个不同的标签?
- ios - 将信息从 UIViewRepresentable 发送回 SwiftUI (SpriteKit)
- javascript - 在 Django 中使用 Openlayers
- php - RouteGroup Laravel 7中的错误路线
- artificial-intelligence - 从目标进行回归规划时,如何在 GOAP 中加入变量?
- angular - 为什么即使我放了跨域,我也有错误未授权?
- java - 将 .Class 文件注入现有的 Jar 文件
- if-statement - 为什么K不保持它的价值?