c - 即使有scanf,它也跳过了行并且不要求输入
问题描述
这是代码。如果我问了明显的问题,我很抱歉
int main()
{
int stat;
char v_id[100], c_id[100], line[BUFFER_SIZE], tos[100], cust_stat[100], n_daysasd[100], tot_fee[100];;
printf("Enter Visitor ID: ");
scanf("%s", &v_id);
printf("Enter Customer ID: ");
scanf("%s", &c_id);
FILE* fptr2;
FILE* fptr;
FILE* ftemp;
fptr = fopen(v_id, "r");
fptr2 = fopen("servicestest.txt", "r");
ftemp = fopen("replacetemp.txt", "w");
while (fgets(line, BUFFER_SIZE, fptr))
{
printf("%s\n", line);
}
printf("\nEnter the information accordingly\n");
rewind(fptr);
stat = 0;
while (stat == 0)
{
printf("TOS: ");
scanf("%s", &tos);
getchar();
while (fgets(line, BUFFER_SIZE, fptr) != 0)
{
if (strstr(line, tos))
{
stat = 1;
}
}
}
rewind(fptr);
stat = 0;
while (stat == 0)
{
printf("Customer Status: ");
scanf("%s", &cust_stat);
getchar();
while (fgets(line, BUFFER_SIZE, fptr) != 0)
{
printf("it did enter here");
if (strstr(line, cust_stat))
{
stat = 1;
}
}
}
rewind(fptr);
stat = 0;
while (stat == 0)
{
printf("Needed days: ");
scanf("%s", &n_daysasd);
while (fgets(line, BUFFER_SIZE, fptr))
{
if (strstr(line, n_daysasd))
{
stat = 1;
}
}
}
rewind(fptr);
stat = 0;
while (stat == 0)
{
printf("Total Fee: ");
scanf("%s", &tot_fee);
while (fgets(line, BUFFER_SIZE, fptr))
{
if (strstr(line, tot_fee))
{
stat = 1;
}
}
}
while (fgets(line, BUFFER_SIZE, fptr2))
{
if (strstr(line, v_id))
{
fprintf(ftemp, "Visit ID: %s, Customer ID : %s, TOS : %s, Customer Status : %s, Needed Days : %s, Total Fee : %s, Payment : Not Made", v_id, c_id, tos, cust_stat, n_daysasd, tot_fee);
}
else
{
fputs(line, ftemp);
}
}
fclose(fptr);
fclose(fptr2);
fclose(ftemp);
remove("servicestest.txt");
rename("replacetemp.txt", "servicestest.txt");
总结就是这里
while (stat == 0)
{
printf("TOS: ");
scanf("%s", &tos);
getchar();
while (fgets(line, BUFFER_SIZE, fptr) != 0)
{
if (strstr(line, tos))
{
stat = 1;
}
}
}
这是示例输入
我尝试随机输入,但随后正确输入了它应该做的事情,但由于某种原因,它显示TOS: TOS: TOS: TOS:
了四次,我不知道为什么。
这也是另一个示例输入
它应该要求输入,但它没有,而是跳到total fee:
要求输入,甚至跳过了需要的日子。
我确实倒带了fptr,那么错误在哪里?为什么它不等待输入?
解决方案
推荐阅读
- python - 将目录条目添加到 mpl.backends.backend_pdf.PdfPages 文档
- python - 打印所有可能的字符串[1,2,3,4,5,6]:例如:(2,3,4,5,6,1) (3,4,5,6,1,2) (4, 5,6,1,2,3) 我必须在 python 中使用 for 循环来旋转每个数字
- python - 如何在 Python 中使用 docx 模块读取与 Checkbox 关联的值
- django - 在设定的日期和时间发布自定义 Django Flatpage
- mysql - mysql/mariadb 复制账户的权限
- javascript - 如何使用javascript在我的网站的一个页面中获取多个视频和播放器图像
- java - 如何解决 Angular 12 中的 CORS 问题?
- javascript - 我怎样才能得到 de xxx 行的最后一个单元格?
- python - 如何延迟 aiogram 命令?
- shell - 在 POSIX sh 中迭代位置参数时修改位置参数