1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

scanf, gets and fflush problems

Discussion in 'C' started by silviatodorof, Dec 31, 2011.

  1. silviatodorof

    silviatodorof New Member

    Joined:
    Dec 31, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Dec 31, 2011
  2. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    after each scanf you use add a getchar command

    Code:
    ........
    scanf(....);
    getchar();
    ........
    
     
  3. silviatodorof

    silviatodorof New Member

    Joined:
    Dec 31, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  4. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    Code:
    ...........
       if((fp = fopen("Taxi Information.txt", "[COLOR=Red]w[/COLOR]"))==NULL) {  
    .........
    
    change "w" with "a"
     
    silviatodorof likes this.
  5. silviatodorof

    silviatodorof New Member

    Joined:
    Dec 31, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Yeah, this was perfect, it works. Now how I can write the "taxi registration number" - reg, to the text file.

    Many Thanks
     

Share This Page