[prog] switch statement

Jacinta Richardson jarich at perltraining.com.au
Thu Dec 16 17:50:55 EST 2004


Wolf Rising wrote:
> Hi,
> 
> Thank you for responding :-)
> 
> When I run the code with the changes below I get this print out:
> 
> Enter the product number (enter 0 to end): 1
> Enter the quantity of this product you wish to order: 2
> Enter the product number (enter 0 to end): 0
> INCORRECT NUMBER ENTERED. PLEASE TRY AGAIN. 
> The total sales in dollars is: 5.96
> 
> How would I adjust it without the if statement to prevent the 
> INCORRECT NUMBER ENTERED
> PLEASE TRY AGAIN from appearing?

This seems very strange.  This should be caught by your 0 case here:

>>>              switch ( product) {
>>>                      case 0:
>>>                              break;

However, after compiling the project myself I find that I get the 
following (different problem):

Enter the product number (enter 0 to end): 1
Enter the quantity of this product you wish to order: 2
Enter the product number (enter 0 to end): 0
Enter the quantity of this product you wish to order:


Which is obviously wrong.  I didn't spot that you had the the 0 as exit 
flag.  This can be handled in a number of ways.  You can add a special 
check:

	do {
		product = 0;
		//order product desired by product number
		cout << "Enter the product number (enter 0 to end): ";
		//receive product number
		cin >> product;

		// end if requested
		if(product == 0) {
			break;
		}

		//begin switch statement, define prices of product by number
		switch ( product) {
			case 1:
				price = 2.98;
				break;

	....

Of course this ensures that you'll never fall out of the loop naturally, 
as you'll always be thrown out by the special handling.  If you don't 
like this then you will need to put an if statement around the final 
question:


		// If we reach here, we must have valid input.
                  if(product != 0) {
  			//order quantity of product desired
			cout << "Enter the quantity of this product you wish to order: ";
			//receive quantity
			cin >> quantity;

			//calculate total
			totalSales += quantity * price;
		}
	}while (product != 0);

Which will usually please the computer science lectures and recent 
graduates.  ;)  It's "cleaner" which is something that experience shows 
you can sometimes ignore.  It'll usually please the very experienced 
programmers who've learned enough to know that there are good reasons 
for clean code, too.  ;)

If you're still having problems with the default case triggering on 
zero, send me your code (directly), and I'll be happy to see what 
happens with my compiler.

All the best,

    Jacinta

-- 
    ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
     `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
     (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
   _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
  (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |




More information about the Programming mailing list