繁体   English   中英

为什么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()正在获取除数字(十进制degits)之外的其他输入,例如字符,符号或转义序列,这就是无限循环的原因。

解决方案在获取输入之前清除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()手册页。

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

我也尝试过使用断点进行调试,但并没有陷入循环

在此处输入图片说明

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

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

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

PS :虽然这可能不是一个确定的解决方案,但希望它能为您指明正确的方向。 如果您发布代码段的相关部分,我会尽力提供帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM