【问题标题】:My program continuously goes through a loop in switch case 4. Could someone please take a look and tell me what im doing wrong?我的程序在 switch case 4 中不断地循环。有人可以看看并告诉我我做错了什么吗?
【发布时间】:2021-07-12 09:52:59
【问题描述】:

我正在尝试创建一个程序,在 switch case 4 中将记录添加到文件中,但它不断地循环。有人可以在这里帮忙吗?我希望它添加一个学生的记录,然后跳出那个案例并进入主菜单。

我是 C 编程新手,不熟悉 switch case。请看一下,让我知道我需要更改什么。

我认为问题出在开关盒期间,而不是实际代码,因为我确实创建了任何循环,所以我试图了解开关盒的哪一部分需要更改,或者是否有更好的方法来做到这一点。

#include #include #include

int  createFile();
char writeFile();
void mainMenu();

struct Update
{
    char studentName[50];
    int studentID;
    char emailID[100];
    char courseID[5];
    int grade;
} update2;

int main ()
{
  int num;
  
  do
  {
  printf("                   M A I N    M E N U          \n");
  printf("1. Create the Binary File\n");
  printf("2. Display Contents of the file\n");
  printf("3. Seek a specific record\n");
  printf("4. Update the contents of a record\n");
  printf("5. Delete a record for the specific name\n");
  printf("6. Exit\n");



  switch(num)
  {
    case 1:
    createFile();
    break;
    case 4:

    
    writeFile();
    mainMenu();

    break;

    case 6:
    break;
  }

  scanf("%d", &num);
   } while (num != 6);
    return 0;



}

int  createFile() {
    
    
    FILE *fp;

    fp = fopen ("BINARY_FILE.txt", "w");

    printf("created file called BINARY_FILE.txt\n");
    return 0;

     }

char writeFile() {
    FILE *fp;
    fp = fopen ("BINARY_FILE.txt", "w");

    int j;
    int i =0;

    
    printf("Enter the studentID\n");
    scanf("%d", &update2.studentID);
    fprintf(fp, "the studentID:%d\n",update2.studentID);

    
    printf("Enter the student name\n");
    scanf("%s", update2.studentName);

    fprintf(fp, "the student name:%s\n",update2.studentName);
    
    printf("Enter the emailID\n");
    scanf("%s", update2.emailID);
    
    fprintf(fp, "the emailID:%s\n",update2.emailID);
    
    printf("Enter the courseID\n");
    scanf("%s", update2.courseID);
    
    fprintf(fp, "the courseID:%s\n", update2.courseID);
    
    printf("Enter the grade\n");
    scanf("%d", &update2.grade);

    fprintf(fp, "the grade:%d\n", update2.grade);


    fclose(fp);
    return 0;

    }


void mainMenu()
{
     printf("                   M A I N    M E N U          \n");
  printf("1. Create the Binary File\n");
  printf("2. Display Contents of the file\n");
  printf("3. Seek a specific record\n");
  printf("4. Update the contents of a record\n");
  printf("5. Delete a record for the specific name\n");
  printf("6. Exit\n");
}

【问题讨论】:

  • num 未初始化?还是在某处缺少scanf()
  • 你应该把 scanf 放到外面和里面,或者重新排序你的代码
  • 您正在开启num读取任何值之前。
  • 旁注: mashed_potaotes -> mashed_potatoes ???
  • 已更新我的答案。看看最后的建议是否有帮助?

标签: c


【解决方案1】:

揭开这种奇怪情况的最佳选择是

  1. 使用-Wall 启用编译器警告
  2. 使用像GDB/LLDB这样的调试器调试程序(如果你使用CLANG工具链编译)
  3. 如果出现内存崩溃等情况,请使用地址清理程序,例如ASAN

对于您的特定用例,如果您使用第一个选项,则最好点击以下警告:

<source>:20:7: warning: variable 'num' is used uninitialized whenever function 'main' is called [-Wsometimes-uninitialized]
  int num;
  ~~~~^~~
<source>:34:10: note: uninitialized use occurs here
  switch(num)
         ^~~
<source>:20:10: note: initialize the variable 'num' to silence this warning
  int num;
         ^
          = 0

尝试了解编译器发出此警告的原因,然后查看您的代码逻辑是否有必要遵守该警告(我的个人经验:通常是这样!)。修复它,然后运行您的代码。一旦步骤#1 完成并且仍然存在奇怪的行为,请继续执行步骤#2,依此类推。完成所有步骤后,SO 是发布您的烦恼/疑问的好地方。

您正在开启num,它未初始化,它很有可能(非常)不匹配您的任何@987654335 @ 语句,因为许多编译器可能只是将它初始化为 0 或者它可能是一些垃圾值,这两者都没有被你的任何 case 语句满足(尝试在 @987654337 之前打印它的值@)。这将我带到switch-case 的下一点。最好在 switch-case 构造中包含 default 块,以查看是否有任何意外值潜入您的代码逻辑。例如:

switch(num)
{
    case 1:
    createFile();
    break;

    case 4:
    writeFile();
    mainMenu();
    break;

    case 6:
    break;

    default:
    printf("Give me a break!\n");
    break;
}

另外,从您分享的屏幕截图中,您输入的成绩为A,而不是数字。尝试输入数字而不是字符,或将grade 类型更改为char 并使用scanf(" %c", &amp;update2.grade); 注意%c 之前格式字符串中的额外空格。引用 scanf 手册页:

通常会跳过前导空白。跳过白色 空格优先,在格式中使用显式空格。

所以基本上:

  1. switch 之前移动 scanf() 以初始化 num
  2. 正确读入grade成员

另外,请考虑改用scanf() 的更安全替代方案,并改用fgets()strtol() / strtoul() / strtod()

【讨论】:

    【解决方案2】:

    我尝试编译并运行它,一切正常。你是如何运行代码的,你使用的是哪个编译器?

    顺便说一句,当使用 scanf 扫描字符串时,您可以使用标志 %[^\n] 而不是 %s,因为第二个在空格后停止,而第一个仅在 (^) 换行之前停止 ( \n) 被发现。如何做到这一点的一个例子是:

    char writeFile() {
    
        FILE *fp;
        fp = fopen ("BINARY_FILE.txt", "w");
    
        int j;
        int i =0;
    
    
        printf("Enter the studentID\n");
        scanf("%d", &update2.studentID);
        fprintf(fp, "the studentID:%d\n",update2.studentID);
    
    
        printf("Enter the student name\n");
        scanf("\n%[^\n]", update2.studentName);
    
        fprintf(fp, "the student name:%s\n",update2.studentName);
    
        printf("Enter the emailID\n");
        scanf("\n%[^\n]", update2.emailID);
    
        fprintf(fp, "the emailID:%s\n",update2.emailID);
    
        printf("Enter the courseID\n");
        scanf("\n%[^\n]", update2.courseID);
    
        fprintf(fp, "the courseID:%s\n", update2.courseID);
    
        printf("Enter the grade\n");
        scanf("%d", &update2.grade);
    
        fprintf(fp, "the grade:%d\n", update2.grade);
    
    
        fclose(fp);
        return 0;
    
    }
    

    【讨论】:

    • 我添加了运行代码后发生的屏幕截图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 2021-12-08
    • 2012-07-14
    • 1970-01-01
    • 2020-02-18
    相关资源
    最近更新 更多