my if statement is being ignored.

if statement is being ignored, total being output too soon.

Page 1 of 1

5 Replies - 2370 Views - Last Post: 05 May 2007 - 11:36 AM Rate Topic: -----

#1 grimes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-April 07

my if statement is being ignored.

Post icon  Posted 04 May 2007 - 03:59 PM

Ok, program is hw, it 'works' for the most part, except that the total is being displayed to early. The if statement for checkout is being ignored when it's false. Can someone please explain to me why that is? Everything looks ok to me.

I appreciate any pointers or advice.

kindly,

-g


#include<iostream>
#include<iomanip>

using namespace std;

int main()
{

	int		productNumber = 0;
	int		quantitySold = 0;
	double	sales = 0;
	double	total = 0;
	char	checkout = 'X';

	do
	{
	
			// perform input
			cout << "Enter Product Number (1-5): ";
			cin >> productNumber;

			if(productNumber != 0);
			{ 
				cout << "Enter Quantity Sold: ";
				cin >> quantitySold;
		
					switch (productNumber)
					{
						case 1:
						sales = (quantitySold * 2.98);
						break; 

						case 2:
						sales = (quantitySold * 4.50);
						break; 

						case 3:
						sales = (quantitySold * 9.98);
						break; 

						case 4:
						sales = (quantitySold * 4.49);
						break; 

						case 5:
						sales = (quantitySold * 6.87);
						break; 

					} // end switch 

				cout << "Keep Shopping? 'n' to checkout: ";
				cin  >> checkout;

				if(checkout =='n');
				{
					total = total + sales;
					cout << "Total Sold: $" << fixed << setprecision(2) << total <<endl;
				}

			}// end of if

	}while(checkout != 'n');	//end of do / while loop

system("pause");

return 0;



}//end of main




Is This A Good Question/Topic? 0
  • +

Replies To: my if statement is being ignored.

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: my if statement is being ignored.

Posted 04 May 2007 - 04:39 PM

the problem is you have a ; at the end of the if line, remove it and all will be well
this: if(productNumber != 0);
should be: if(productNumber != 0)

check your if statements as you repeat this problem on all of them.

Only do while statements use the ; to end them, simple do, for and if statements/loops to not use them
Was This Post Helpful? 0
  • +
  • -

#3 grimes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-April 07

Re: my if statement is being ignored.

Post icon  Posted 04 May 2007 - 10:46 PM

View PostWilliam_Wilson, on 4 May, 2007 - 04:39 PM, said:

the problem is you have a ; at the end of the if line, remove it and all will be well
this: if(productNumber != 0);
should be: if(productNumber != 0)

check your if statements as you repeat this problem on all of them.

Only do while statements use the ; to end them, simple do, for and if statements/loops to not use them


Ok, Thanks, I removed the ; as you specified, but kept the ; on this line

 if(checkout =='n'); 


and all is well. HOWEVER, if I remove both ; the program then does not retain the previous data that was input.

So, can someone explain why it is ok to have the ; at the end of one if statement but not the other?

Damn idiosyncrasies that I just don't understand yet.

Thanks a bunch for the gentle push.

- g
Was This Post Helpful? 0
  • +
  • -

#4 roguefeebo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-May 07

Re: my if statement is being ignored.

Posted 05 May 2007 - 12:42 AM

You're only modifying total when the user wants to exit. Take the
total = total + sales;
out of the if statement to modify it when the user wants to stay in the program. Now if you remove the ; it should update correctly.
Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: my if statement is being ignored.

Posted 05 May 2007 - 09:04 AM

To add to roguefeebo's statement...it is not ok to keep the ; on any of the if statements.
Was This Post Helpful? 0
  • +
  • -

#6 grimes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 29-April 07

Re: my if statement is being ignored.

Post icon  Posted 05 May 2007 - 11:36 AM

View Postroguefeebo, on 5 May, 2007 - 12:42 AM, said:

You're only modifying total when the user wants to exit. Take the
total = total + sales;
out of the if statement to modify it when the user wants to stay in the program. Now if you remove the ; it should update correctly.



Thanks for the hint and info, so here is what that section of code now looks like and it works!

  
(checkout == 'n');
total = total + sales;



- g

This post has been edited by grimes: 05 May 2007 - 01:32 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1