scanf, gets and fflush problems

silviatodorof's Avatar, Join Date: Dec 2011
Light Poster
This is the code:
Code:
/**********************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 100

void print_menu(int n);

void main()
{
    int c, a[MAX],temp[MAX],reg,top = 0;
    char carname[80];
    FILE *fp;            
                                            
    print_menu(1);

    scanf_s("%d", &c);
    while (c != 2)
    {
        if (c == 1)
        {
            // Scan in registration
            printf("Please enter Taxi Registration Number\n");
            scanf("%d", &reg);
            a[top] = reg;
            top++;

if((fp = fopen("Taxi Information.txt", "w"))==NULL) {               
printf("Cannot open file.\n");                                      
exit(1);                                                     
}
do {
   printf ("Please enter the car name (CR to quit):\n");
   fflush (stdout);
   /* get the car name */
   gets(carname);
   strcat (carname, "\n");
   fputs (carname, fp);
} while (*carname != '\n');    

fclose(fp);
// Call print_menu
print_menu(1);
scanf("%d", &c);
        }
        else if (c == 2) 
        {
            //remove from front
            int b;
    
            for(b = 0; b <= top; b++)
            {
                temp[b] = a[b+1];
            }

            for (b = 0; b <= top; b++)
            {
                a[b] = temp[b];
            }

            top--;
            printf("Taxi has left\n");
            //print menu
            print_menu(1);
            scanf("%d", &c);
        }
    }
}
// Print menu
void print_menu(int n)
{
    printf(" 1. Arrive \n");
    printf(" 2. Leave  \n");    
}
/**********************************/
The problem is:
After choosing "1", it does not stop at "Please enter the car name (CR to quit)".

Thanks

Last edited by shabbir; 31Dec2011 at 23:48.. Reason: Code blocks
virxen's Avatar, Join Date: Nov 2009
Pro contributor
after each scanf you use add a getchar command

Code:
........
scanf(....);
getchar();
........
silviatodorof's Avatar, Join Date: Dec 2011
Light Poster
Thanks alot. This solved part of the problem and created another problem. Only the last typed characters are saved in the text file. I mean, if you keep chosing option 1 and type characters and again you choose option 1 and type characters, and then you leave the loop and check the text file, you will find that it only keeps the characters that were written in the last iteration.
virxen's Avatar, Join Date: Nov 2009
Pro contributor
Quote:
Originally Posted by silviatodorof View Post
Thanks alot. This solved part of the problem and created another problem. Only the last typed characters are saved in the text file. I mean, if you keep chosing option 1 and type characters and again you choose option 1 and type characters, and then you leave the loop and check the text file, you will find that it only keeps the characters that were written in the last iteration.
Code:
...........
   if((fp = fopen("Taxi Information.txt", "w"))==NULL) {  
.........
change "w" with "a"
silviatodorof like this
silviatodorof's Avatar, Join Date: Dec 2011
Light Poster
Yeah, this was perfect, it works. Now how I can write the "taxi registration number" - reg, to the text file.

Many Thanks