我有一个用 C 语言编写的简单电话簿,可以将输入信息保存到我电脑上的一个文本文件中。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
void validateName(char name[], FILE *fptr);
void validateID(char id[], FILE *fptr);
void validateScore(int score, FILE *fptr);
void addContact(char name[], char id[], int score, FILE *fptr);
void printContact(FILE *fptr);
void printAll();
int main()
{
char name[30], id[10];
int score;
int i;
FILE *fptr;
fptr = fopen("C:\\c\\program.txt","w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
else {
bool flag = true;
do{
printf("\n1. Press 1 to display record\n"
"2. Press 0 to add a new contact\n"
"3. Press -1 to display all records\n\n");
int option;
scanf("%d", &option);
switch(option) {
case 1:
printContact(fptr);
flag = false;
break;
case 0:
addContact(name, id, score, fptr);
flag = false;
break;
case -1:
printAll();
flag = false;
break;
default:
printf("wrong choice, try again\n\n");
}
} while(flag);
fclose(fptr);
return(0);
}
}
void validateName(char name[], FILE *fptr){
bool flag = true;
while(flag){
fgets(name, 30, stdin);
if ((strlen(name) < 3) || (strlen(name) > 20)) {
printf("error\nplease enter name again: ");
} else {
flag = false;
}
}
}
void validateID(char id[], FILE *fptr){
bool flag = true;
while(flag) {
if (!isalpha(id[0])){
printf("enter a letter as first char\n");
}
else if(strlen(id) > 10) {
printf("error too much numbers \n");
} else {
flag = false;
}
}
}
void validateScore(int score, FILE *fptr) {
if(score >= 0 && score <= 100) {
fprintf(fptr, "SCORE: %d\n", score);
} else {
printf("invalid input");
(exit(1));
}
}
void addContact(char name[], char id[], int score, FILE *fptr) {
//first name
printf("Enter name: ");
scanf("%s", name);
validateName(name, fptr);
fprintf(fptr,"NAME: %s \n", name);
//id
printf("Enter id: ");
scanf("%s", id);
validateID(id, fptr);
fprintf(fptr,"STUDENT NO: %s\n", id);
//score
printf("enter score: ");
scanf("%d", &score);
validateScore(score, fptr);
}
void printContact(FILE *fptr) {
int c;
char contacts[50];
while((c = getchar()) != EOF) {
fscanf(fptr, "%s", &contacts);
for(int i = 50; i < 50; i++){
printf("%s", &contacts);
}
}
}
void printAll() {
}
我的第一个问题是在 validateID 函数中: 当我尝试验证第一个条件时,我不断陷入无限循环,即确保第一个索引是一个字符。
我的第二个问题是,当我输入姓名、学号和分数时,程序结束了,我没有返回到菜单切换语句来选择其他选项?
我做错了什么或者我错过了什么?
最佳答案
在我看来,拥有像 void validateXXX
这样的函数的想法是错误的。将其与类似于验证的 isalpha
进行比较。 isalpha
返回一个 int
以便您可以使用它进行检查。您的验证函数可能应该做同样的事情。所以不是:
void validateID(char id[], FILE *fptr){
bool flag = true;
while(flag) {
if (!isalpha(id[0])){
printf("enter a letter as first char\n");
}
else if(strlen(id) > 10) {
printf("error too much numbers \n");
} else {
flag = false;
}
}
}
函数可以是:
int validateID(char id[]){
if (!isalpha(id[0])){
printf("enter a letter as first char\n");
return 0;
}
if(strlen(id) > 10) {
printf("error too much numbers \n");
return 0;
}
return 1;
}
然后你可以像这样使用它:
do {
scanf("%s", id); // See below !!!!
} while(!validateID(id));
以便您继续阅读,直到输入有效 ID。
下一步:
从不使用scanf("%s", ...)
- 从不
没有检查输入的长度,因此用户可以输入一些太长而无法存储在您提供的缓冲区中的文本(例如 id
)。这对您的程序来说将是灾难性的,因为它会写入数组边界之外。您的程序可能会崩溃或做各种奇怪的事情。
您应该使用fgets
来读取用户输入。它更容易也更安全。
对于你的主循环:
是的,当您在 switch
中的所有 case
中将 flag
设置为 false 时,它当然会终止。
关于C电话本程序: how to exit avoid infinite loop and how to return to the switch statement menu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42290780/