dowhile doesn't work out properly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • navda
    New Member
    • Jan 2010
    • 9

    #1

    dowhile doesn't work out properly

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    char another,
    int num;
    printf("\nenter a number to get its square\n");
    scanf("%d",&num);
    printf("\nsquare of %d is %d",num,num*num);
    printf("\ndo you want to enter another no y/n");
    scanf("%c",&another);
    }while(another=='y');
    getch();
    }
    " when i try to give input 'y' for another char variable instead of following and continuing do loop my programme closes" plz help out
    Last edited by Banfa; Sep 27 '10, 09:04 AM. Reason: Added [code] ... [/code] tags round the code, please use them in future.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I see no do in you program.

    For maximum portability in C main returns int not void. In C++ it is mandated that main returns int and not doing so is undefined behaviour.

    You never read any of the newlines \n out of the input buffer so when you come to read a character at line 12 the newline character is still in the input buffer and the system happily returns that newline in the character read on line 12. Since newline is not 'y' the loop ends.

    The console is basically a line mode input device, that is your program never sees anything until the user presses enter and puts a newline character into the buffer. Reading it as a line mode input device will make your life easier, read the input with fgets into a character array (a good size one) then parse the data you have got with sscanf or strtoul or atoi.

    Comment

    • navda
      New Member
      • Jan 2010
      • 9

      #3
      thanks a lot for repling me back but i wish if u could send me the code for my better understanding and pointing out the lines where i had made mistakes i have tried this code from let us c eight edition but it was not working in my computer so i need more clear code to get throught this problem

      Comment

      • ashitpro
        Recognized Expert Contributor
        • Aug 2007
        • 542

        #4
        Have you read what 'Banfa' has said? Have you tried to implement what he said?

        You are missing 'do' in your code
        You are missing ';' after line 6
        Here is the modified code.
        It still won't work as you are expecting, cause scanf will read '\n' that you entered after number to be squared. (Here '\n' means 'Enter' key)

        Code:
        #include<stdio.h>
        #include<conio.h>
        void main()
        {
        clrscr();
        char another;
        int num;
        do{
        printf("\nenter a number to get its square\n");
        scanf("%d",&num);
        printf("\nsquare of %d is %d",num,num*num);
        printf("\ndo you want to enter another no y/n");
        scanf("%c",&another);
        }while(another=='y');
        getch();
        }
        At line 13 try to use 'getch' instead of 'scanf' to read the input from user.

        Comment

        Working...