为什么printf语句会无限执行?

Why does the printf statement execute infinitely?

程序中使用的头文件。

#ifndef choice_h
#define choice_h
#include <stdio.h>
#include <stdlib.h>


int choice_list(){
int option;

puts("DATA CONFIRMATION AND UPDATE PROGRAM\n\n");
puts("1. Display data set now.\n");
puts("2. Delete an entry from the data set.\n");
puts("3. Add an entry to the end of the data set.\n");
puts("4. Change an existing entry.\n");
puts("5. Quit this program.\n");
puts("Enter choice [ 1-5, 0 = change data set]:");
scanf("%d", &option);

//this while loop tests against numbers [0-5] the choice called "option" that 
//was input by the user. Option needs to be in the range of [0-5] 
//in order for the user to select from the program menu.
while (!(0 == option || 1 == option || 2 == option || 3 == option || 4 == option || 5 == option))
{
    printf("\nInvalid Entry, please enter [0-5]: ");
    scanf("%d", &option);
}

 //this if statement is an echo print to check the selected option
if (0 == option || 1 == option || 2 == option || 3 == option || 4 == option || 5 == option) {

    printf("you selected option: [%d]\n", option);
}

return option; //returns to calling function
}


 #endif /* choice_h */

============================================= ======

主程序:这不是我的全部程序。我只贴到错误发生的地方。

 #include <stdio.h>
 #include <stdlib.h>
 #include "../include/myheader.h"
 #include "../include/lab7_arrays.h"
 #include "../include/choice_list.h"

 int option_0(int ptr[]);
 int option_1(int ptr[]);
 int option_2(int ptr[]);
 int option_3(int ptr[]);
 int option_4(int ptr[]);

 main(){

 int data_1[MAX_VALUES] = {4, 7, 6, 32, 5};
 int data_2[MAX_VALUES] = {98, 47, 26, 99, 187};

    int option, choice;

    my_identity();
    print_arrays();
    puts("\nWhich set do you want to update [1 or 2]: ");
    scanf("%d", &choice);

//  this while loop checks and prompts the user to input an accepted value (1 or 2)
    while (!(1 == choice || 2 == choice))
    {
printf("\nInvalid Entry, please enter 1 or 2: ");
    scanf("%d", &choice);
    }

if (1 == choice || 2 == choice)
    printf("\nDisplaying options for data set %d...\n\n", choice);

 //function defined in header file   
 option = choice_list();
    printf("option = %d choice = %d", option, choice);

/***** 无限循环从这里开始:一直打印选项和选择,直到我强制退出程序 *******/

    while (choice == 1||choice == 2)
    {
            if(choice == 1)
            { int *ptr = data_1;
              printf("choice: %d option: %d", choice, option);
                    switch(option)
                    {   case 0: option_0(ptr);
                                    break;
                            case 1: option_1(ptr);
                                    break;
                            case 2: option_2(ptr);
                                    break;
                            case 3: option_3(ptr);
                                    break;
                            case 4: option_4(ptr);
                                    break;
                            case 5: break;
                           default: printf("Invalid input");
                    }
            }
            else
            { int *ptr = data_2;
                    switch(option)
                    {   case 0: option_0(ptr);
                                    break;
                            case 1: option_1(ptr);
                                    break;
                            case 2: option_2(ptr);
                                    break;
                            case 3: option_3(ptr);
                                    break;
                            case 4: option_4(ptr);
                                    break;
                            case 5: break;
                           default: printf("Invalid input");
                    }
            }
    }
return EXIT_SUCCESS;
 }

 /*I have not yet written the code for these functions since I am still 
   working on the main part right now. The only one I defined is option 3 
   which adds an element to the end of the array the user decided to select.*/

 int option_0(int ptr[])
{
    return(0);
}

 int option_1(int ptr[])
{
    return(0);
}

 int option_2(int ptr[])
{
    return(0);
}

//add element to the end of array
int option_3(int ptr[])
{
    int i, value;
    int num_elements = sizeof(ptr)/sizeof(ptr[0]);
    /*printf("num_elements = %d", num_elements);

    printf("Enter number to add: ")
    scanf("%d", &value);

    ptr[num_elements] = value;
    */
    for (i = 0; i > num_elements; i++)
    {
            printf(" %d ",ptr[i]);
    }
 return 1;
 }

  int option_4(int ptr[])
 {
    int i = 0;
    return i;
 }

编译时运行:

 Program written by: KELSEY WILLIAMS 

 Program compiled on May  1 2018 at 04:54:01.

 Here is what data set 1 looks like now:


 [1]    [2]    [3]    [4]    [5]  

  4      7      6      32      5   

 Here is what data set 2 looks like now:


 [1]    [2]    [3]    [4]    [5]  

  98     47     26     99     187   
 Which set do you want to update [1 or 2]: 
 1

 Displaying options for data set 1...

 DATA CONFIRMATION AND UPDATE PROGRAM


 1. Display data set now.

 2. Delete an entry from the data set.

 3. Add an entry to the end of the data set.

 4. Change an existing entry.

 5. Quit this program.

 Enter choice [ 1-5, 0 = change data set]:
 3
 you selected option: [3]

 option = 3choice = 1 option: 3choice: 1 option: 3choice: 1 option: 
 3choice: 1 option: 3choice: 1 option: 3choice: 1 option: 3choice: 1 option: 
 3choice: 1 option: 3choice: 1 option: 3choice: 1 option: 3choice: 1 option: 
 3choice: 1 and on and on and on....=(

提前感谢您的帮助。

这里的罪魁祸首是 scanf( ) 函数
程序中某处的 scanf( ) 正在获取除数字(十进制小数)以外的一些输入,例如字符、符号或转义序列,这就是无限循环的原因。

解决方案 在获取输入之前清除 scanf( ) 输入缓冲区,在 choice_list( ) 子例程和 main( ) 函数中调用 scanf( ) 函数之前的循环中使用以下代码片段。

[1]/* clears the input buffer */
while ((getchar()) != '\n');  

类似于 choice_list( ) 子例程

...
...
while (!(0 == option || 1 == option || 2 == option || 3 == option || 4 == option || 5 == option))
{
    printf("\nInvalid Entry, please enter [0-5]: ");
   /* clears the input buffer */
    while ((getchar()) != '\n');  
    scanf("%d", &option);
}
...
...

类似地,在循环中使用 scanf( ) 的地方,在调用 scanf() 函数之前首先使用上面的代码[1] 清除输入缓冲区。 有关详细信息,请参阅 scanf() 手册页。

为了重现错误,我在 IDE 上尝试了您的代码片段 Main() 调用 choice_list() 执行正常。

我也试过断点调试,但没有进入循环

因此,我认为是代码片段的剩余部分(您的问题中尚未更新)导致了无限循环。

此外,如果您密切注意无限循环中消息的格式,您会注意到它是导致循环的代码的不同部分。

choice: 1 option: 3choice: 1 option: 3choice: 1 option: 3

P.S:虽然这可能不是最终的解决方案,但希望这能为您指明正确的方向。 如果您 post 代码段的相关部分

,我可以尝试提供更好的帮助