为什么printf语句无限执行?

时间:2018-05-01 09:01:10

标签: c infinite-loop

程序中使用的头文件。

#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);

/ ***** INFINITE LOOP在这里开始:只是继续打印选项和选择,直到我强行退出程序******* /

    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....=(

感谢您提前提供任何帮助。

2 个答案:

答案 0 :(得分:0)

罪魁祸首是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(),首先使用[1]之上的代码清除输入缓冲区,然后再调用scanf()函数。 有关更多详细信息,请参阅scanf()手册页。

答案 1 :(得分:0)

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

我也尝试使用断点来解决问题并且没有进入循环

enter image description here

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

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

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

P.S :虽然这可能不是一个明确的解决方案,但希望这会指向正确的方向。 如果您发布代码段的相关部分,我可以尝试更好地协助