Stop Console Input (cin) from reading input twice.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace 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;
}
Last edited on
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
}

BTW You can't compare floating point values with ==
https://noobtuts.com/cpp/compare-float-values
Topic archived. No new replies allowed.