[prog] switch statement

Wolf Rising wolfrising at gmail.com
Thu Dec 16 05:53:29 EST 2004


Hi,
This is a program I was trying to do, I think I have most of it
working except where default
comes into play. If you enter an incorrect number twice, it doesn't
really pick up on it. Any
suggestions for how I could improve this? Usual caveat applies, this
is not my homework,
I'm just working on this stuff on my own. Any suggestions/pointers are
always welcome.

Thanks!

#include <iostream>
using namespace std;

int main() {

//define variables
  int  product; // product to be ordered
  int  quantity;//quantity to be ordered
  double price;
  double cost; // calculate cost
  double totalSales;


// Get input from user

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


//begin loop, set product to break from loop when sentinel value is entered
while (product > 0) {

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

   case 2:
     price = 4.50;
     break;

   case 3:
     price = 9.98;
     break;

   case 4:
     price = 4.49;
     break;

   case 5:
     price = 6.87;
     break;

   default:
    cout << "INCORRECT NUMBER ENTERED. PLEASE TRY AGAIN. \n";
	cout << "Enter the product number: ";
	cin >> product;
    break;
	
	

}

 

//order quantity of product desired
  cout << "Enter the quantity of this product you wish to order: ";
//receive quantity
  cin >> quantity;

//calculate cost
  cost = quantity * price;

  cout << "Enter the product number: ";
  cin >> product;


  totalSales += cost;

}


  cout << "The total sales in dollars is: " << totalSales;
  
return 0;
}


More information about the Programming mailing list