[prog] switch statement

Gareth Anderson somecsstudent at gmail.com
Thu Dec 16 14:07:15 EST 2004


Personally I'd restructure the program, maybe like this:

"
int product = 1;

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

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

//begin switch statement, define prices of product by number
switch ( product) {
 case 0: break; (you don't want a zero to jump to default case)
 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:
   cerr << "INCORRECT NUMBER ENTERED. PLEASE TRY AGAIN. \n";
   break;
}

Note changed cout to cerr and you can see the rest :)

Note that I finished second year (mostly C/C++ programming) in
November so this could be a little rusty :)

Regards,
Gareth


More information about the Programming mailing list