CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: do-while loop

  1. #1
    Join Date
    Apr 2009
    Posts
    15

    do-while loop

    i have the following code with me :

    #include<iostream.h>
    #include<conio.h>
    #define PI 3.14

    void main()
    {
    char ch;
    int radius;
    do
    {
    cout<<"\nEnter the radius of the circle : "<<endl;
    cin>>radius;
    cout<<PI*radius*radius;
    cout<<"\nDo you want to find area of another circle (y/n) : ";
    cin>>ch;
    }while(ch=='y');
    }

    after the code has run once.....and it asks :

    Do you want to find area of another circle (y/n) : y5

    as shown above if i enter y5...i get the area of my circle with radius y...can anyone plz explain this...why does any value given with "y" being accepted as the radius for new circle..


    thanks in advance

  2. #2
    Join Date
    Mar 2009
    Posts
    94

    Re: do-while loop

    i think, when you enter y5, you put y5 on the istream cin. Then >> takes only a char from the istream. So there is still the 5 left. So your programm will not ask for another int, but take the 5 still on the istream.

    you should #include <iostream> without h. c++ headers dont have a .h ending.

  3. #3
    Join Date
    Mar 2008
    Posts
    38

    Re: do-while loop

    I am confused, can you show the complete output?

  4. #4
    Join Date
    Apr 2009
    Posts
    15

    Re: do-while loop

    Here is the complete output :

    Enter the radius of the circle :
    5
    78.5

    Do you want to find area of another circle (y/n) : y7

    Enter the radius of the circle :
    153.86

    Do you want to find area of another circle (y/n) :


    As said when i enter a digit with :" y" it doesnt again ask me the radius but takes that digit as the new radius and displays the result

    Why does this happen ?

  5. #5
    Join Date
    Mar 2008
    Posts
    38

    Re: do-while loop

    w3rd is correct then. The digit is buffered and gets picked up by the next cin. So if you typed y7y6y5, you'd have the area of the radii 7, 6, 5.

    I think there is a way to stop this. Flushing cin at the top of the do while might work, but I get a feeling it was something else. Maybe cin.get ?

  6. #6
    Join Date
    Apr 2009
    Posts
    15

    Re: do-while loop

    hmmm thanks frnds fr ur replies.....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured