[prog] switch statement

Wolf Rising wolfrising at gmail.com
Thu Dec 16 15:10:37 EST 2004


eureka! Thank you :-) I can follow changing the while to a do/while, and
this may be a foolish question, but how did you know the program
needed the if statement?



#include <iostream>
using namespace std;

int main() {
	
	//define variables
	int  product; // product to be ordered
	int  quantity;//quantity to be ordered
		double price;
		double totalSales;
		
		
		// Get input from user
		
		do {
			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;
					
					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";
						break;
						
				}
				
				
				if (product > 0 && product < 6) {
					//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);
			
			cout << "The total sales in dollars is: " << totalSales;
			
			return 0;
		}



On Wed, 15 Dec 2004 21:45:09 -0600 (CST), Kathryn Hogg
<kjh at flyballdogs.com> wrote:
> 
> Wolf Rising said:
> > Is this what you meant?
> 
> Not quite you need to do a loop like this:
>   do {
>     // get a valid product number
>     product = 0;  // initialize to zero incase we get EOF
>     cout << "Product number" << endl;
>     cin >> product;
>     switch (product) {
>       case 0:
>          break;
> 
>        case 1:
>           price = ....;
> 
>           ....
>            default:
>               cout << "invalid product number" << endl;
>             break;
>     }
> 
>     // if its a good product
>     if (product > 0 && product < 6) {
>        // get quantity
>        ...
> 
>        // calculate total
>        total += quantity * price;
>     }
>  } while (product != 0);
> 
>  cout << "total = " << total << endl;
> 
> --
> Kathryn
> http://womensfooty.com
> 
> _______________________________________________
> Programming mailing list
> Programming at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/programming
>


More information about the Programming mailing list