So this is what I don't understand with this code of mine. When program control enters the do-while loop during execution, cin repeats whenever a zero is input for choice or divisor. I could alter the code to prevent this from happening but am just curious as to the operation of console input (cin) in this case. Any ideas will be much appreciated.
#include <iostream>
usingnamespace std;
int main()
{
double dividend,divisor; int choice;
cout<<"Please enter the dividend:>> ";
cin>>dividend;
cout<<"Plaese enter the divisor:>> ";
cin>>divisor;
cout<<"\n";
do
{if(divisor==0)
cout<<"\n";
cout<<"Please the divisor you entered is 0 and not acceptable\nThe program requires the divisor not to be 0\n\n";
cout<<"Pleaae do the following:\nEnter 1 to change the values of both the dividend and the divisor\nOr, enter 0, to change only the divisor:>> ";
cin>>choice;
cout<<"\n";
if(choice==1)
cout<<"Please enter the dividend:>> ";
cin>>dividend;
cout<<"Plaese enter the divisor:>> ";
cin>>divisor;
if(choice==0)
cout<<"Plaese enter the divisor:>> ";
cin>>divisor;
}
while(divisor==0);
cout<<"\n";
cout<<dividend<<" divided by "<<divisor<<" is:>> "<<dividend/divisor;
return 0;
}
The output doesn't belong to the if statement
If you format your code better it would be easy to spot:
1 2 3 4 5 6 7 8
do
{
if(divisor==0)
cout<<"\n";
cout<<"Please the divisor you entered is 0 and not acceptable\nThe program requires the divisor
not to be 0\n\n";
// and so on
}