-
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
-
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.
-
Re: do-while loop
I am confused, can you show the complete output?
-
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 ?
-
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 ?
-
Re: do-while loop
hmmm thanks frnds fr ur replies.....