error in the scanf in the do-while loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ehab mohamed
    New Member
    • May 2010
    • 18

    error in the scanf in the do-while loop

    #include<stdio. h>
    void main()
    {
    int i, n, month, day, calcday, calcmonth;
    char flag='y';
    char weekday[][10]={"Friday","Sat urday","Sunday" ,"Monday","Tues day","Wednesday ","Thursday "};
    int usermonth[]={31,28,31,30,3 1,30,31,31,30,3 1,30,31};
    do
    {
    printf("Enter the date (Day, Month): ");
    scanf("%d %d",&day ,&month);
    calcday=day-1;
    calcmonth=month-1;
    for(i=0;i<calcm onth;i++)
    {
    calcday+=usermo nth[i];
    }
    n=calcday%7;

    printf("%d/%d/2010 is %s \n",day, month, weekday[n]);

    printf("Do you want to continue (y/n)? ");

    scanf("%c",&fla g);
    }

    while(flag=='y' );

    }
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    Reproduced.
    Well, if the question was "what to do" it depends on what do you need to achieve. If it doesn't matter, i'd continue with scanf("%c") till I get either 'y' or 'n'.

    Comment

    • gaurav kumar

      #3
      use getch() instead of scanf() it will work fine

      Comment

      • ehab mohamed
        New Member
        • May 2010
        • 18

        #4
        i can't understand you mr.newb16
        i want in each time scanf for the flag to know if it complete in the loop or get out

        Comment

        • ehab mohamed
          New Member
          • May 2010
          • 18

          #5
          can u give me an example mr.gaurav kumar

          Comment

          • ehab mohamed
            New Member
            • May 2010
            • 18

            #6
            i try it mr.gaurav kumar
            and it still wrong
            nothing change

            Comment

            • newb16
              Contributor
              • Jul 2008
              • 687

              #7
              If scanf resulted in 'flag' that is not 'y' nor 'n', continue calling scanf until you get the desired letter.

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                Change "while(flag=='y ')" to "while(flag!='n ')".
                (This is just a restatement of newb16's suggestion.)

                Comment

                • newb16
                  Contributor
                  • Jul 2008
                  • 687

                  #9
                  This will not work - it will receive \r left after previous scanf and will never quit. It need to discard it in a separate loop.

                  Comment

                  • ehab mohamed
                    New Member
                    • May 2010
                    • 18

                    #10
                    how mr.newb16 ??

                    Comment

                    • BillyTKid
                      New Member
                      • Aug 2010
                      • 7

                      #11
                      change
                      Code:
                      char flag='y'; -> char flag[2];
                      scanf("%c",&flag); -> fgets( flag,2,stdin );
                      while(flag=='y'); -> while(*flag=='y');

                      Comment

                      Working...