By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,475 Members | 989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,475 IT Pros & Developers. It's quick & easy.

what do i do to make my program compare price and money deposited?

P: 3
i have to develop this snack machine program that makes you chose a snack put money in and give change here is my code so far, what do i use so the program compare the price and money deposited, I also dont know how to initialize the snack prices and the deposited money.I know i declared and initialized them wrong Thank you so much in advance

char *selection;
double *payment;

int main()
{
char item_cost;
char selection=&item_cost;
double P=1.25,
S=1.35,
T=0.95,
C=1.50,
B=1.75,
N=1.40;
int money;
double payment=&money;
double 1=5.00,
2=1.00,
3=0.25,
4=0.10,
5=0.05;
//double *payment=&money;

void Menu(char *selection); // Displays the list of snack items and prompts for the user’s choice
{
printf("Please enter a selection :\n");
printf("P – Potato Chips $1.25\n");
printf("S - Snickers Bar $1.35\n");
printf("T – Pop Tart $0.95\n");
printf("C – Cookies $1.50\n");
printf("B – Brownie $1.75\n");
printf("N – Nuts $1.40\n");
printf("Please enter your snack selection:\n");
scanf(" %c", &item_cost);

}


// void CoinMenu(double *payment); //displays menu of coins and gets user input of the coins deposited
{
printf("Please deposit your money by selection:\n");
printf("1 - $5.00\n");
printf("2 - $1.00\n");
printf("3 - $0.25\n");
printf("4 - $0.10\n");
printf("5 - $0.05\n");
printf("Deposit amount:\n");
scanf("%lf", &money);

}

int CheckMoney(double *payment, double item_cost); //compares the amount the user has deposited to the price of item selected.
//It returns 1 if the amount is at least enough to cover
//the cost, 0 if there is not enough.

if(money<item_cost)
return 0;
else if(money>=item_cost)
return 1;
printf("Welcome to Vend-O-Matic\n");

return 0;
Oct 26 '09 #1
Share this Question
Share on Google+
1 Reply

P: 13
I hope the below code will help you...there may be compilation errors..I haven't compiled...

#include<stdio.h>
char selection;//selection can be
// P,S,T,C,B or N
double payment;// amount corresponding to
//the selection

void Menu();
int CheckMoney(double item_cost);
void CoinMenu();

int main()
{

double item_cost;
double P=1.25,
S=1.35,
T=0.95,
C=1.50,
B=1.75,
N=1.40;

Menu();

if(selection=='P')
{
item_cost= 1.25;
}
else if(selection=='S')
{
item_cost = 1.35;
}
else if(selection == 'T')
{
item_cost = 0.95;
}
else if(selection=='C')
{
item_cost = 1.50;
}
else if(selection=='B')
{
item_cost = 1.75;
}
else if(selection=='N')
{
item_cost = 1.40;
}

CoinMenu();

int returnValue = CheckMoney(item_cost);

if (returnValue == 1)
printf("Pls collect your snacks from the tray;-)");
else
printf("you have not enough money to by snacks(-;");

return 0;
}

void Menu() // Displays the list of snack items and prompts for the user’s choice
{
printf("Please enter a selection :\n");
printf("P – Potato Chips $1.25\n");
printf("S - Snickers Bar $1.35\n");
printf("T – Pop Tart $0.95\n");
printf("C – Cookies $1.50\n");
printf("B – Brownie $1.75\n");
printf("N – Nuts $1.40\n");
printf("Please enter your snack selection:\n");
scanf(" %c", &slection);

if(selection=='P'||selection=='S'||selection=='T'| |selection=='C'
||selection=='B'||selection=='N')
{
return ;
}
else
{
printf("You have entered wrong choice\n");
menu();
}
return 0;
}


void CoinMenu() //displays menu of coins and gets user input of the coins deposited
{
printf("Please deposit your money by selection:\n");
printf("1 - $5.00\n");
printf("2 - $1.00\n");
printf("3 - $0.25\n");
printf("4 - $0.10\n");
printf("5 - $0.05\n");
printf("Deposit amount:\n");
int choice;

scanf("%d", &choice);
if(choice == 1)
payment = 5.00;
else if(choice == 2)
payment = 1.00;
else if(choice == 3)
payment = 0.25;
else if(choice == 4)
payment = 0.10;
else if(choice == 5)
payment = 0.05;

}

int CheckMoney(double item_cost) //compares the amount the user has deposited to the price of item selected.
//It returns 1 if the amount is at least enough to cover
//the cost, 0 if there is not enough.
{
if(payment<item_cost)
return 0;
else if(payment>=item_cost)
{
printf("Welcome to Vend-O-Matic\n");
return 1;
}
return 0;
}
Oct 26 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.