The Question is :

A store has 6 items. Each item has a price and a bar-code that uniquely

identifies it. To simulate such a store, the program offers a menu showing the

codes, their corresponding items, and their prices. The user chooses one or

several items to buy, and pays for them. The program then returns the right

change (big bills first). For example, if the user pays 150 QR and the items cost

101 QR, then the amount returned is 49 QR. So, the change will be four 10

bills, one 5 bill, and four 1 bills. We assume that the user will buy one or more

items and checkout by choosing the code 0. If the amount is not sufficient, the

user is asked to repay for the items.

The codes, the items, and the prices to use are shown in the following table

(The code 0 is used to signal that the user completed the shopping):

Code Item Price
0 - -
2 Oil 8
3 Pizza 43
4 Laptop 1199
5 Macaroni 20
6 Tuna 7

Due to the problem of debugging and understanding programs with global

variables, your implementation must not contain any global variables.

Run and test the program with following scenarios:

The user buys Pizza (item 3) and pays 45.

The user buys Laptop (item 4) and pays 1500.

The user did not buy anything by choosing the code zero.

The user buys two Pizzas (3), Macaroni (5), and Oil (2). The user pays 100.

``````Hi every one,

#include <iostream>
#include <iomanip>

using namespace std;

void DispalyStoreItems ();

int ChoosingItems ();

double calculateChange ( int , int );

int main ()
{
int cost;

DispalyStoreItems ();

cost = ChoosingItems ();

calculateChange ( int pay , int cost );

system ("PAUSE");

return 0;
}
void DispalyStoreItems ()
{
cout <<"Code" << setw (10) << "Item" << setw (10) << "Price" << endl;

cout << "------------------------" << endl;

cout << setw (2) << "0" << setw (11) << " - " << setw (10) << " - " << endl;

cout << endl;

cout << setw (2) << "1" << setw (13) << "Bread" << setw (7) << "5" << endl;

cout << endl;

cout << setw (2)  << "2" << setw (11) << "Oil" << setw (9) << "8" << endl;

cout << endl;

cout << setw (2) << "3" << setw (13) << "Pizza" << setw (8) << "43" <<endl;

cout << endl;

cout << setw (2) << "4" << setw (14) << "Laptop" << setw (9) << "1199" << endl;

cout << endl;

cout << setw (2) << "5" << setw (16) << "Macaroni" << setw (5) << "20" << endl;

cout << endl;

cout << setw (2) << "6" << setw (12) << "Tuna" << setw (8) << "7" << endl;

cout << endl;
}
int ChoosingItems ()
{
int price;
int item = 0;
int cost = 0;
int code = 0;
int count = 0;
int Oil = 0;
int Pizza = 0;
int Laptop = 0;
int Macaroni = 0;
int Tuna = 0;

while ( true )
{
cout << "Enter the code of the Item you want to buy : ";

cin >> code;

cout << endl;

if ( code == 0 )
break;

switch ( item )
{
case 1:
break;

case 2:
Oil++;
break;

case 3:
Pizza++;
break;

case 4:
Laptop++;
break;

case 5:
Macaroni++;
break;

case 6:
Tuna++;
break;
}

if ( code == 1 )
{
price = 5;
}
else if ( code == 2 )
{
price = 8;
}
else if ( code == 3 )
{
price = 43;
}
else if ( code == 4 )
{
price = 1199;
}
else if ( code == 5 )
{
price = 20;
}
else if ( code == 6 )
{
price = 7;
}
else
{
price = 0;
}

cost += price;
}

if ( Bread != 0 )
cout << "The User Buys " << item << "\n";
if ( Oil != 0 )
cout << "The User Buys " << item << "\n";
if ( Pizza != 0 )
cout << "The User Buys " << item << "\n";
if ( Laptop != 0 )
cout << "The User Buys " << item << "\n";
if ( Macaroni != 0 )
cout << "The User Buys " << item << "\n";
if ( Tuna != 0 )
cout << "The User Buys " << item << "\n";

cout << "and Pays " << cost << "\n";

return cost;
}
double calculateChange ( int pay, int cost )
{
double units;
double change;

cout << "Enter the amount of the money you should pay : ";

cin >> pay;

cout << endl;

change = pay - cost;

if ( change >= 100)
{
change = ( change / 100 ) - ( units * 100 );

cout << change << "Hundreds" << "\n";
}
if ( change >= 50 )
{
change = ( change / 50 ) - ( units * 50 );

cout << change << "Fifties" << "\n ";
}
if ( change >= 10 )
{
change = ( change / 10 ) - ( units * 10 );

cout << change << "Tens" << "\n";
}
if ( change >= 1 )
{
change = change - units;

cout << change << "Ones" << "\n";
}

return change;
}``````
5
Contributors
11
Replies
12
Views
8 Years
Discussion Span

What actually is your problem? I tested and the program runs fine.

I have error message with this line
double calculateChange ( int pay, int cost )

and how can I seperate prices from the function choosingItems??

Try this code:

``````#include <iostream>
#include <iomanip>

using namespace std;

void DispalyStoreItems ();

int ChoosingItems ();

double calculateChange ( int pay , int cost );

int main ()
{
int cost, pay;

DispalyStoreItems ();

cost = ChoosingItems ();

calculateChange ( pay , cost );

//system ("PAUSE");

return 0;
}
void DispalyStoreItems ()
{
cout <<"Code" << setw (10) << "Item" << setw (10) << "Price" << endl;

cout << "------------------------" << endl;

cout << setw (2) << "0" << setw (11) << " - " << setw (10) << " - " << endl;

cout << endl;

cout << setw (2) << "1" << setw (13) << "Bread" << setw (7) << "5" << endl;

cout << endl;

cout << setw (2)  << "2" << setw (11) << "Oil" << setw (9) << "8" << endl;

cout << endl;

cout << setw (2) << "3" << setw (13) << "Pizza" << setw (8) << "43" <<endl;

cout << endl;

cout << setw (2) << "4" << setw (14) << "Laptop" << setw (9) << "1199" << endl;

cout << endl;

cout << setw (2) << "5" << setw (16) << "Macaroni" << setw (5) << "20" << endl;

cout << endl;

cout << setw (2) << "6" << setw (12) << "Tuna" << setw (8) << "7" << endl;

cout << endl;
}
int ChoosingItems ()
{
int price;
int item = 0;
int cost = 0;
int code = 0;
int count = 0;
int Oil = 0;
int Pizza = 0;
int Laptop = 0;
int Macaroni = 0;
int Tuna = 0;

while ( true )
{
cout << "Enter the code of the Item you want to buy : ";

cin >> code;

cout << endl;

if ( code == 0 )
break;

switch ( item )
{
case 1:
break;

case 2:
Oil++;
break;

case 3:
Pizza++;
break;

case 4:
Laptop++;
break;

case 5:
Macaroni++;
break;

case 6:
Tuna++;
break;
}

if ( code == 1 )
{
price = 5;
}
else if ( code == 2 )
{
price = 8;
}
else if ( code == 3 )
{
price = 43;
}
else if ( code == 4 )
{
price = 1199;
}
else if ( code == 5 )
{
price = 20;
}
else if ( code == 6 )
{
price = 7;
}
else
{
price = 0;
}

cost += price;
}

if ( Bread != 0 )
cout << "The User Buys " << item << "\n";
if ( Oil != 0 )
cout << "The User Buys " << item << "\n";
if ( Pizza != 0 )
cout << "The User Buys " << item << "\n";
if ( Laptop != 0 )
cout << "The User Buys " << item << "\n";
if ( Macaroni != 0 )
cout << "The User Buys " << item << "\n";
if ( Tuna != 0 )
cout << "The User Buys " << item << "\n";

cout << "Total Costs: " << cost << "\n";

return cost;
}
double calculateChange ( int pay, int cost )
{
int units;
int change;
int solve;
int extra;

do {
cout << "Enter the amount of the money you should pay : ";

cin >> pay;

cout << endl;

change = pay - cost;

}
while ( pay < cost );

if ( change >= 100 )
{
cout<<"Your change is : " << change <<endl;
solve = ( change / 100 ) - ( units * 100 );

cout << solve << " Hundreds" << "\n";

extra = change - (solve * 100);
if ( extra >= 50 ){
extra = ( extra / 50 ) - ( units * 50 );

cout << extra << " Fifties" << "\n";
} if ( extra >= 10 ){
extra = ( extra / 10 ) - ( units * 10 );

cout << extra << " Tens" << "\n";
} if ( extra >= 1 ){
extra = extra - units;

cout << extra << " Ones" << "\n";
}

}

else if ( change >= 50 )
{
cout<<"Your change is : " << change <<endl;
solve = ( change / 50 ) - ( units * 50 );

cout << solve << " Fifties" << "\n ";

extra = change - (solve * 50);
if ( extra >= 10 ){
extra = ( extra / 10 ) - ( units * 10 );

cout << extra << " Tens" << "\n";
} if ( extra >= 1 ){
extra = extra - units;

cout << extra << " Ones" << "\n";
}
}

else if ( change >= 10 )
{
solve = ( change / 10 ) - ( units * 10 );

cout << solve << " Tens" << "\n";

extra = change - (solve * 10);
if ( extra >= 1 ){
extra = extra - units;

cout << extra << " Ones" << "\n";
}
}

else if ( change >= 1 )
{
solve = change - units;

cout << solve << " Ones" << "\n";
}

return change;
}``````

Hope that this code will solve your problem. ^_^ goodluck!

Of course, that line should give you an error. Haven't you declared the function before?
When you want to use the function, you are not supposed to put the data type as a param, only the variables, so change that line to

``calculateChange  (pay,cost);``

and the program should compile. Always be careful of that mistake next time,pal.

you can mark this thread as solved if this solved your problem.

Edited by tkud: n/a

finally the program compile but there a logic error in the change

the thing i wanna do is ask the user for the money to pay for the cost of the items prices
then subtract his pay from cost and put it in the variable change
change = pay - cost;
the check this change to give the user his change in 100 or 50 or 10 or 5 or 1

``````if ( change >= 100)
{
Change = ( change / 100 ) - ( units * 100 );

cout << Change << " Hundreds " << "\n";
}
if ( change >= 50 )
{
Change = ( change / 50 ) - ( units * 50 );

cout << Change << " Fifties " << "\n ";
}
if ( change >= 10 )
{
Change = ( change / 10 ) - ( units * 10 );

cout << Change << " Tens " << "\n";
}
if ( change >= 5 )
{
Change = ( change / 5 ) - ( units * 5 );

cout << Change << " Fives " << "\n";
}
if ( change >= 1 )
{
Change = change - units;

cout << Change << " Ones " << "\n";
}``````

but this has a logic error how can i fix it

thank you guys

Edited by Nick Evan: Added code-tags

finally the program compile but there a logic error in the change

the thing i wanna do is ask the user for the money to pay for the cost of the items prices
then subtract his pay from cost and put it in the variable change
change = pay - cost;
the check this change to give the user his change in 100 or 50 or 10 or 5 or 1

``````if ( change >= 100)
{
Change = ( change / 100 ) - ( units * 100 );

cout << Change << " Hundreds " << "\n";
}
if ( change >= 50 )
{
Change = ( change / 50 ) - ( units * 50 );

cout << Change << " Fifties " << "\n ";
}
if ( change >= 10 )
{
Change = ( change / 10 ) - ( units * 10 );

cout << Change << " Tens " << "\n";
}
if ( change >= 5 )
{
Change = ( change / 5 ) - ( units * 5 );

cout << Change << " Fives " << "\n";
}
if ( change >= 1 )
{
Change = change - units;

cout << Change << " Ones " << "\n";
}``````

but this has a logic error how can i fix it

thank you guys

Have you tried this code("on my last post"):

``````if ( change >= 100 )
{
cout<<"Your change is : " << change <<endl;
solve = ( change / 100 ) - ( units * 100 );

cout << solve << " Hundreds" << "\n";
extra = change - (solve * 100);

if ( extra >= 50 ){
extra = ( extra / 50 ) - ( units * 50 );
cout << extra << " Fifties" << "\n";
} if ( extra >= 10 ){
extra = ( extra / 10 ) - ( units * 10 );
cout << extra << " Tens" << "\n";
} if ( extra >= 1 ){
extra = extra - units;
cout << extra << " Ones" << "\n";
}
}

else if ( change >= 50 )
{
cout<<"Your change is : " << change <<endl;
solve = ( change / 50 ) - ( units * 50 );

cout << solve << " Fifties" << "\n ";

extra = change - (solve * 50);
if ( extra >= 10 ){
extra = ( extra / 10 ) - ( units * 10 );
cout << extra << " Tens" << "\n";
} if ( extra >= 1 ){
extra = extra - units;
cout << extra << " Ones" << "\n";
}
}

else if ( change >= 10 )
{
solve = ( change / 10 ) - ( units * 10 );
cout << solve << " Tens" << "\n";

extra = change - (solve * 10);
if ( extra >= 1 ){
extra = extra - units;
cout << extra << " Ones" << "\n";
}
}

else if ( change >= 1 )
{
solve = change - units;

cout << solve << " Ones" << "\n";
}

return change;
}``````

Edited by valtikz: n/a

Hi,
yeah I did but it won't get me the right change

if the user bought a laptop = 1199

and he paid 1500

his change is 301 right

so he will have 3 bills of hunderds and one bens or something like that

so how can i put the condition to do me this?

Hi,
yeah I did but it won't get me the right change

if the user bought a laptop = 1199

and he paid 1500

his change is 301 right

so he will have 3 bills of hunderds and one bens or something like that

so how can i put the condition to do me this?

yes, I think its right. I bought 1 laptop = 1199 and paid 1500. The change is 301 and the program answers:

3 hundreds
1 ones

is this right?

the output has strange looking and it's with the negative sign

the output has strange looking and it's with the negative sign

are you sure?! I purchased 1 laptop and paid 1500. And this is what displayed on my screen:

``````Code      Item     Price
------------------------
0         -         -

2        Oil        8

3        Pizza      43

4        Laptop     1199

5        Macaroni   20

6        Tuna       7

Enter the code of the Item you want to buy : 4

Enter the code of the Item you want to buy : 0

Total Costs: 1199
Enter the amount of the money you should pay : 1500

3 Hundreds
1 Ones``````

what is your example of purchase and payment?!

Does look right.