Click here to Skip to main content
15,891,033 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionhow to input into array from keyboard Pin
atoivan3-Dec-10 16:20
atoivan3-Dec-10 16:20 
AnswerRe: how to input into array from keyboard Pin
Dr.Walt Fair, PE3-Dec-10 16:36
professionalDr.Walt Fair, PE3-Dec-10 16:36 
AnswerRe: how to input into array from keyboard Pin
Luc Pattyn3-Dec-10 17:20
sitebuilderLuc Pattyn3-Dec-10 17:20 
AnswerRe: how to input into array from keyboard [modified] Pin
Alain Rist3-Dec-10 23:55
Alain Rist3-Dec-10 23:55 
QuestionC++ Drink Machine Simulator Can anyone tell what I am doing wrong with this? Pin
Cachel3-Dec-10 15:00
Cachel3-Dec-10 15:00 
AnswerRe: C++ Drink Machine Simulator Can anyone tell what I am doing wrong with this? Pin
Dr.Walt Fair, PE3-Dec-10 16:26
professionalDr.Walt Fair, PE3-Dec-10 16:26 
GeneralRe: C++ Drink Machine Simulator Can anyone tell what I am doing wrong with this? Pin
Cachel3-Dec-10 19:34
Cachel3-Dec-10 19:34 
GeneralRe: C++ Drink Machine Simulator Can anyone tell what I am doing wrong with this? [modified] Pin
User 74293383-Dec-10 22:24
professionalUser 74293383-Dec-10 22:24 
It's hard to read in this format, but I think there is more wrong than just this function.
Your dispenseChange functions accepts a double m, which contains the amount of money inserted. The function is called when the amount is more than 1.00. The price of a drink is 25ct, so why not call this function when the amount is more than 0.25?

Then when you're in the function dispenseChange, you call GetMoney. So after you have inserted too much money already and change is calculated, you ask for another coin to be inserted and then base the change on that coin.
The change should be based on the amount paid and the price: so m-0.25 is the amount that is paid too much. Then you'd have to split this to coins.

Other observations:
In your main loop, you check until you have inserted enough money. You do this by calling getMoney(), but you don't update your money variable. So the enough(money) check won't be right either.
Your getDrink function calls itself.

Maybe try something like this:
/*
Program: Drink Machine
Programmer: Cachel Caldiero
Date: November 16, 2010
Purpose: Simulate a drink machine
*/

using namespace std;

// Function Prototypes
void banner();
void menu();
int getMoney();
bool enough(double);
void getDrink();
void dispenseChange(double);

// Main function

int main ()
{
	double money = 0.0;
	
	//display banner
	banner();
	menu();
	
	//While not enough money
	while (!enough(money))
	{
		//Add money based upon option selected
		switch (getMoney())
		{
		case 1: cout << "You have entered $1.00" << endl;
				money += 1.00;
			break;
		case 2: cout << "You have entered 25 cents" << endl;
				money += 0.25;
			break;
		case 3: cout << "You have entered 10 cents" << endl;
				money += 0.10;
			break;
		case 4: cout << "You have entered 5 cents" << endl;
				money += 0.05;
			break;
		}
		
		//Tell user how much money has been deposited already
		cout << endl << "Current amount deposited is " << money << endl;
	}
	
	//determine drink selection
	getDrink();
	
	//dispense change
	if (money > 0.25)
		dispenseChange(money);
	
	return 0;
}

// Function Definitions
void banner()
{
	cout << "MMM DDD" << endl
		<< "OOO EEE" << endl
		<< "UUU WWW" << endl
		<< "NNN " << endl
		<< "TTT " << endl
		<< "AAA " << endl
		<< "III " << endl
		<< "NNN " << endl;
}

void menu()
{
	cout << "Pick your drink" << endl
		<< "1. Pepsi" << endl
		<< "2. Diet Pepsi" << endl
		<< "3. Mountain Dew" << endl
		<< "4. Diet Mountain Dew" << endl
		<< "5. Bottled Water" << endl;
}

int getMoney()
{
	int opt;
	
	cout << "Please deposit $0.25." << endl;
	
	cout << "1.Dollar" << endl
		<< "2.Quarter" << endl
		<< "3.Dime" << endl
		<< "4.Nickel" << endl;
	
	cin >> opt;
	
	return opt;
}

bool enough(double m)
{
	//is there enough money?
	if (m >= 0.25){
		cout << "Enough money inserted" << endl;
		return 1;
	}
	else{
		cout << "Need more money" << endl;
		return 0;
	}
}

void getDrink()
{
	int option;
	
	//display selection menu
	cout << "Pick your drink" << endl;
	
	cout << "1.Pepsi" << endl
		<< "2.Diet Pepsi" << endl
		<< "3.Mountain Dew" << endl
		<< "4.Diet Mountain Dew" << endl
		<< "5.Bottled Water" << endl;
	
	//read input selection for drink
	cin >> option;
	
	
	//output here is your drink.. message
	cout << "Here is your drink" << endl;
}

void dispenseChange(double m)
{
	//determine the change in coins.. for example 2 dimes, 1 nickel.
	
	if (m == 1.20)
		cout << "Your change is $0.95 or 3 quarters and 2 dimes.";
	else if (m == 1.15)
		cout << "Your change is $0.90 or 3 quarters and a dime and a nickel.";
	else if (m == 1.10)
		cout << "Your change is $0.85 or 3 quarters and a dime.";
	else if (m == 1.05)
		cout << "Your change is $0.80 or 3 quarters and a nickel.";
	else if (m == 1.00)
		cout << "Your change is $0.75 or 3 quarters.";
	else if (m == 0.45)
		cout << "Your change is $0.20 or 2 dimes.";
	else if (m == 0.40)
		cout << "Your change is $0.15 or a dime and a nickel.";
	else if (m == 0.35)
		cout << "Your change is $0.10 or a dime.";
	else if (m == 0.30)
		cout << "Your change is $0.05 or a nickel.";
}


modified on Saturday, December 4, 2010 6:40 AM


modified 13-Sep-18 21:01pm.

GeneralRe: C++ Drink Machine Simulator Can anyone tell what I am doing wrong with this? Pin
Maximilien4-Dec-10 7:22
Maximilien4-Dec-10 7:22 
QuestionRead data using assembly Pin
MKC0023-Dec-10 2:39
MKC0023-Dec-10 2:39 
AnswerRe: Read data using assembly Pin
Rajesh R Subramanian3-Dec-10 2:50
professionalRajesh R Subramanian3-Dec-10 2:50 
GeneralRe: Read data using assembly Pin
MKC0023-Dec-10 3:45
MKC0023-Dec-10 3:45 
GeneralRe: Read data using assembly Pin
Rajesh R Subramanian3-Dec-10 3:53
professionalRajesh R Subramanian3-Dec-10 3:53 
GeneralRe: Read data using assembly Pin
MKC0023-Dec-10 22:09
MKC0023-Dec-10 22:09 
AnswerRe: Read data using assembly Pin
Aescleal3-Dec-10 6:17
Aescleal3-Dec-10 6:17 
GeneralRe: Read data using assembly Pin
MKC0023-Dec-10 22:10
MKC0023-Dec-10 22:10 
GeneralRe: Read data using assembly Pin
Emilio Garavaglia4-Dec-10 10:22
Emilio Garavaglia4-Dec-10 10:22 
JokeRe: Read data using assembly Pin
Luc Pattyn4-Dec-10 12:02
sitebuilderLuc Pattyn4-Dec-10 12:02 
QuestionMemory checker and error detector for C++ Pin
Ahmed Charfeddine3-Dec-10 1:38
Ahmed Charfeddine3-Dec-10 1:38 
AnswerRe: Memory checker and error detector for C++ Pin
Maximilien3-Dec-10 4:19
Maximilien3-Dec-10 4:19 
QuestionGet the process ID by the application itself Pin
CodingLover2-Dec-10 23:36
CodingLover2-Dec-10 23:36 
AnswerRe: Get the process ID by the application itself Pin
User 74293382-Dec-10 23:47
professionalUser 74293382-Dec-10 23:47 
GeneralRe: Get the process ID by the application itself Pin
CodingLover6-Dec-10 1:01
CodingLover6-Dec-10 1:01 
GeneralRe: Get the process ID by the application itself Pin
User 74293386-Dec-10 1:13
professionalUser 74293386-Dec-10 1:13 
GeneralRe: Get the process ID by the application itself Pin
CodingLover6-Dec-10 1:20
CodingLover6-Dec-10 1:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.