# Problem with code

Page 1 of 1

## 6 Replies - 979 Views - Last Post: 19 October 2007 - 07:12 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=35693&amp;s=e95a1c171fe8a485312e76e8b8ca6333&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 James Bond C++ Spy

Reputation: 1
• Posts: 132
• Joined: 03-October 07

# Problem with code

Posted 19 October 2007 - 08:55 AM

I am struggling to get the code going correctly. The problem is to read a series of pairs. product and quantity then use a switch to determine product 1 thru 5 and get the cost (quantity * price) of each product. Then displaying the final costs. Thanks for any help with this problem. Here's my code (not working)

```#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{

int Product;

int Product1;
int Product2;
int Product3;
int Product4;
int Product5;

int Quantity1;
int Quantity2;
int Quantity3;
int Quantity4;
int Quantity5;

double OrderTotal1;
double OrderTotal2;
double OrderTotal3;
double OrderTotal4;
double OrderTotal5;

//user inputs product number and quantity
cout << "Please Enter the Product 1 Number or -1 to Exit: ";
cin >> Product1;
cout << "Please Enter the Quantity";
cin >> Quantity1;

cout << "Please Enter the Product 2 Number: ";
cin >> Product2;
cout << "Please Enter the Quantity";
cin >> Quantity2;

cout << "Please Enter the Product 3 Number: ";
cin >> Product3;
cout << "Please Enter the Quantity";
cin >> Quantity3;

cout << "Please Enter the Product 4 Number: ";
cin >> Product4;
cout << "Please Enter the Quantity";
cin >> Quantity4;

cout << "Please Enter the Product 5 Number: ";
cin >> Product5;
cout << "Please Enter the Quantity";
cin >> Quantity5;

switch ( Product ) // switch statement
{
case 1: // product 1
OrderTotal1 = Quantity1 * 2.98; // total charge for product 1
break; // necessary to exit switch

case 2: // product 2
OrderTotal2 = Quantity2 * 4.50; // total charge for product 2
break; // exit switch

case 3: // product 3
OrderTotal3 = Quantity3 * 9.98; // total charge for product 3
break; // exit switch

case 4: // product 4
OrderTotal4 = Quantity4 * 4.49; // total charge for product 4
break; // exit switch

case 5: // product 5
OrderTotal5 = Quantity5 * 6.87; // total charge for product 5
break; // exit switch

case '\n': // ignore newlines,
case '\t': // tabs,
case ' ': // and spaces in input
break; // exit switch

default: // catch all other characters
cout << "Incorrect Product Number."
<< " Enter a New Product Number." << endl;
break; // optional; will exit switch anyway
} // end switch

cout << "Total Amount for Order 1 Is: \$ " << OrderTotal1 << endl;
cout << "Total Amount for Order 2 Is: \$ " << OrderTotal2 << endl;
cout << "Total Amount for Order 3 Is: \$ " << OrderTotal3 << endl;
cout << "Total Amount for Order 4 Is: \$ " << OrderTotal4 << endl;
cout << "Total Amount for Order 5 Is: \$ " << OrderTotal5 << endl;

} // end main
```

This post has been edited by jayman9: 19 October 2007 - 10:58 AM

Is This A Good Question/Topic? 0

## Replies To: Problem with code

### #2 MorphiusFaydal

• D.I.C Lover

Reputation: 43
• Posts: 1,376
• Joined: 12-May 05

## Re: Problem with code

Posted 19 October 2007 - 09:03 AM

What seems to be the problem? At a first quick scan over, the only problem that pops out to me is the second line of 'default' in your switch; it's missing a 'cout' at the front of it.

### #3 James Bond C++ Spy

Reputation: 1
• Posts: 132
• Joined: 03-October 07

## Re: Problem with code

Posted 19 October 2007 - 09:10 AM

Well I don't understand how to get the user to input the product and figure out how to store each by product number that was entered and use a sentinel controlled loop to go until user quits. The way I have it set up the I am asking for the specific product number. I'm sure there are better ways of doing the problem.
Thanks for any help.

James Bond C++ Spy

This post has been edited by James Bond C++ Spy: 19 October 2007 - 09:34 AM

• D.I.C Regular

Reputation: 6
• Posts: 334
• Joined: 08-October 07

## Re: Problem with code

Posted 19 October 2007 - 11:51 AM

Well, first, in order to loop something you have to code in a loop. In this case you would be looking for something similar to this.

```//Priming Read
cout<<"Enter the product #1-5 (-1 to quit)";
cin>>prodNumber;

while(prodNumber != -1){
//Do your code in here to figure price, too lazy this morning

cout<<"Enter the product #1-5 (-1 to quit)";
cin>>prodNumber;
}

```

Hope that helps some.

### #5 James Bond C++ Spy

Reputation: 1
• Posts: 132
• Joined: 03-October 07

## Re: Problem with code

Posted 19 October 2007 - 04:45 PM

Thanks aceofspades686 for the tip. I was thinking of this all in the wrong way. I have finally figured it out. They don't want you to keep track of the products just use a simple while loop to loop thru the program until the user exits. Anyway Thanks for the help. Heres the working code:

```#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{

//  declaring variables
int Product;
int Quantity;
float OrderTotal;

//user inputs product number and quantity
cout << "Please Enter the Product Number or -1 to Exit: ";
cin >> Product;

// start while loop
while( Product != -1 )
{
cout << "Please Enter the Quantity: ";
cin >> Quantity;

switch ( Product ) // switch statement
{
case 1: // product 1
OrderTotal = Quantity * 2.98; // total charge for product 1
break; // necessary to exit switch

case 2: // product 2
OrderTotal = Quantity * 4.50; // total charge for product 2
break; // exit switch

case 3: // product 3
OrderTotal = Quantity * 9.98; // total charge for product 3
break; // exit switch

case 4: // product 4
OrderTotal = Quantity * 4.49; // total charge for product 4
break; // exit switch

case 5: // product 5
OrderTotal = Quantity * 6.87; // total charge for product 5
break; // exit switch

case '\n': // ignore newlines,
case '\t': // tabs,
case ' ': // and spaces in input
break; // exit switch

default: // catch all other characters
cout << "Incorrect Product Number.";
cout << "Enter a New Product Number." << endl;
break; // optional; will exit switch anyway
} // end switch

//user inputs product number and quantity
cout << "Total Amount for the Order Is: \$ " << OrderTotal << endl;
cout << "Please Enter the Product Number or -1 to Exit: ";
cin >> Product; // Loop

} // end while loop
} // end main

```

Thanks

James Bond C++ Spy

This post has been edited by James Bond C++ Spy: 19 October 2007 - 07:10 PM

• D.I.C Regular

Reputation: 6
• Posts: 334
• Joined: 08-October 07

## Re: Problem with code

Posted 19 October 2007 - 06:00 PM

Good job, but learn to use code tags before I beat you up in class.

### #7 James Bond C++ Spy

Reputation: 1
• Posts: 132
• Joined: 03-October 07

## Re: Problem with code

Posted 19 October 2007 - 07:12 PM

Sorry keep forgetting the code tags. Edited my earlier post.

James Bond C++ Spy