使用'fgets'读取char只是在运行时跳过

时间:2013-12-10 22:26:12

标签: c++ scanf fgets

问题在于read_in函数,当我使用fgets读取乐队/歌手的用户输入时,它将跳过这一行。我无法弄清楚为什么会这样?之前我确实使用了scanf,但问题是如果你输入带有空格的乐队名称,例如'foo fighters',它将跳过参考线的下一个打印名称。任何人都知道如何解决这个问题?

#include <stdio.h>
    #include <conio.h>
    #define MAX 1000`enter code here`

    int exit = 0;                                                                       // Declaring a global variable 

    struct data {                                                                       // using struct to put the data in an arrays
        char band [48], cd [48], ref [16];
        float cost;
        int year;
    }   cata [MAX];                                                                     // declaring a struct variable

    int read_in ( void )
    {
        int i = 0;
        printf("\n");

        while ( exit != 2 )                                                             // while exit != 2 then ask the user to enter the information
        {
            printf("\nPlease Enter the Name of Band/Singer: ");
            fgets(cata[i].band, 48, stdin);

            printf("\nPlease Enter the Name of CD: ");
            scanf("%s", &cata[i].cd);

            printf("\nPlease Enter the Name of the Reference: ");
            scanf("%s", &cata[i].ref);

            printf("\nPlease Enter the cost: ");
            scanf("%f", &cata[i].cost);

            printf("\nPlease Enter the Year of Release: ");
            scanf("%d", &cata[i].year);

            printf("\nWrite another CD: [1] \nMain Menu: [2]\nYour choice: ");          // Asking him a choice either write another one or go back to menu
            scanf("%d", &exit); 

            i++;
        }

        exit = 0;
        return i;
    }

    void print_out (void)
    {
        FILE *file_cata;
        int i = 0;

        printf("\n");

        while ( exit != 2 )                                             
        {
            file_cata = fopen ("catalogue.txt", "r");                               // open the file and read

            if (file_cata == NULL)
            {
                printf("Error: can't open files.\n");
            }
            else
            {
                while (!feof (file_cata))
                {
                    fgets(cata[i].band, MAX , file_cata);                           // it will scanf the file and get the string and then print it out on screen
                    printf("%s", cata[i].band);
                    fgets(cata[i].cd, MAX, file_cata);
                    printf("%s", cata[i].cd);
                    fgets(cata[i].ref, MAX, file_cata);
                    printf("%s", cata[i].ref);
                    fscanf(file_cata, "%.2f" , cata[i].cost);                       
                    fscanf(file_cata, "%d", cata[i].year);
                    i++;
                }
            }
            fclose (file_cata);                                                     // close file
            printf("Read it again: [1] \nMain Menu: [2]\nYour choice: ");
            scanf("%d", &exit);
        }
        exit = 0;
    }

    void save ( int num )
    {
        FILE *file_cata;
        int i = 0;

        printf("\n");

        while ( exit != 2 )
        {

            file_cata = fopen ("catalogue.txt", "a");                                   // file append, so it will write at the end of the file

            if (file_cata == NULL)
            {
                printf("Error: can't open files. \n");
            }
            else
            {
                while (i != num)
                {
                    fprintf( file_cata, "\n");
                    fprintf( file_cata, "The name of Band/Singer: %s \n", cata[i].band);
                    fprintf( file_cata, "The name of CD: %s \n", cata[i].cd);
                    fprintf( file_cata, "The name of Reference: %s\n", cata[i].ref);
                    fprintf( file_cata, "The Cost: %.2f \n", cata[i].cost);
                    fprintf( file_cata, "The Year of Release: %d \n", cata[i].year);
                    i++;
                }
            }
            fprintf( file_cata, "\n");
            fclose (file_cata);

            printf("Your data has been saved to the catalogue.\n\n");
            printf("Save it again: [1] \nMain Menu: [2]\nYour Choice: ");
            scanf("%d", &exit);
        }
        exit = 0;
    }

    void main ()
    {
        int num1 = 0;
        int option = 0;

        while ( option != 4 )
        {
            printf("The following options are availlable: \n");
            printf("Read in data [1] \n");
            printf("Print out catalogue to screen [2] \n");
            printf("Save data to file [3] \n");
            printf("Exit Program [4] \n");
            printf("Enter your choice now: ");
            scanf( "%d", &option);

            if (option == 1)
                num1 = read_in ();

            if (option == 2)
                print_out ();

            if (option == 3)
                save(num1);

            printf("\n");
        }

        printf("\n*** Your program will end when you press ANY button. ***\n");
        _getch();
    }

1 个答案:

答案 0 :(得分:0)

scanf将换行符留在输入缓冲区中。相关问题的答案非常好(提示:请勿使用scanf;请始终使用fgets,如果必须转换为sscanf,请使用while((c = getchar()) != '\n' && c != EOF) 。):Replacement of fflush(stdin)

这会刷新输入缓冲区,在尝试读取另一行之前应该这样做(从上面的答案中窃取):

{{1}}