//flex table opened by JP

Click to See Complete Forum and Search --> : Help understanding a problem


nothing
11-25-2003, 08:22 AM
Read this please:

A mail order house sells five different products whose retail prices are: product 1 - $2.98, product 2 - $4.50, product 3 - $9.98, product 4 - $4.49, product 5 - $6.87. Write a program that reads a series of pairs of numbers as follows:

a) Product number
b) Quantity sold for one day

Your program should use a switch statement to help determine the retail price for each product. Your program should calculate and display the total retail value of all products sold last week.

This is what I have so far:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{
bool switch_loop = true;
double price;
double total = 0;
int product_number, quantity_sold;

while( product_number != EOF ){

cout << "Enter product number(1-5, Ctrl + C to exit): ";
cin >> product_number;

while( switch_loop != false ){

switch( product_number ){

case 1:
price = 2.98;
switch_loop = false;
break;

case 2:
price = 4.50;
switch_loop = false;
break;

case 3:
price = 9.98;
switch_loop = false;
break;

case 4:
price = 4.49;
switch_loop = false;
break;

case 5:
price = 6.87;
switch_loop = false;
break;

default:
cout << "Number must be in range 1-5.\n";
break;
}

}

cout << "Enter quantity sold for one day: ";
cin >> quantity_sold;


What I plan to do is to keep reading a pair of numbers until the user wants to stop, and keep the total of products sold so far and the total of money made so far stored in two variables so when the user quits entering numbers I can multiply the quantity of products sold by the total price. Am I on the right track or I did not understand this problem at all? Thanks :p

fishybawb
11-25-2003, 09:22 AM
The question mentions inputting pairs of numbers, so you'd be better off accepting both the product number and quantity sold at the same point in the program. In pseudocode (or my version of it :) ), the program would look like this:


while(valid input)
get product number
get product quantity

switch construct
case is 1
total = total + (product 1 value * quantity)
...
...
end while
output total


The value of "total" then gives the answer for the part of the question that says: "Your program should calculate and display the total retail value of all products sold last week." :t

DocEvi1
11-25-2003, 09:44 AM
//read in

n=0
while(valid input)
{
product[n] = input product #
quant[n] = input quant #
n++
}


//calculate

for(int i=0 to 5) //loop through the number of products sold
{
for(int j = 0 to array length)
{
if(i = product[j]
{
total[i] += price(i) //where price is a method returning teh value of product i
//record other needed stuff
}
}
}