scanf, gets and fflush problems

Light Poster
31Dec2011,23:21   #1
silviatodorof's Avatar
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
Pro contributor
2Jan2012,01:55   #2
virxen's Avatar
after each scanf you use add a getchar command

Code:
........
scanf(....);
getchar();
........
Light Poster
3Jan2012,03:02   #3
silviatodorof's Avatar
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.
Pro contributor
3Jan2012,22:25   #4
virxen's Avatar
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
Light Poster
6Jan2012,21:59   #5
silviatodorof's Avatar
Yeah, this was perfect, it works. Now how I can write the "taxi registration number" - reg, to the text file.

Many Thanks