Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   scanf, gets and fflush problems (http://www.go4expert.com/forums/scanf-fflush-t27499/)

silviatodorof 31Dec2011 23:21

scanf, gets and fflush problems
 
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

virxen 2Jan2012 01:55

Re: scanf, gets and fflush problems
 
after each scanf you use add a getchar command

Code:

........
scanf(....);
getchar();
........


silviatodorof 3Jan2012 03:02

Re: scanf, gets and fflush problems
 
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 3Jan2012 22:25

Re: scanf, gets and fflush problems
 
Quote:

Originally Posted by silviatodorof (Post 90842)
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 6Jan2012 21:59

Re: scanf, gets and fflush problems
 
Yeah, this was perfect, it works. Now how I can write the "taxi registration number" - reg, to the text file.

Many Thanks


All times are GMT +5.5. The time now is 15:14.