0

the issue is in the read_in function when i use fgets to read in the users input for a band/singer it will just skip this line. I cant work out why this is happening? I did use scanf for this before but the issue is that if you do that when you enter a band name with a space e.g 'foo fighters' it will skip the next print name of reference line. Anyone know how to fix this?

#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();
    }
Jack.S
  • 3
  • 4

1 Answers1

0

The scanf is leaving the newline in the input buffer. Related question with a very good answer (Hint: don't use scanf; use fgets always, plus sscanf if you must convert that way.): Replacement of fflush(stdin)

This flushes the input buffer, which you should do before attempting to read another line (stolen from answer above):

while((c = getchar()) != '\n' && c != EOF)
Community
  • 1
  • 1
PaulProgrammer
  • 16,175
  • 4
  • 39
  • 56