0

Having the user “insert” their money. The money insertion will be handled in a Loop. For example, before they start, their total inserted is $0. let’s say that a drink costs $1.00. If they say that they are inserting “$0.25” then we will add $0.25 to $0 and tell them to please insert $0.75. Then we will ask them how much they are inserting. If they say “$0.25”, then we add $0.25 to the $0.25 that they have already inserted giving them $0.50. Once they have inserted $1.00, we can give them their drink options. If they have not entered at least $1.00, we will continue to loop so they can insert more.

Put the entire main function in a loop so once the transaction is completed, the program will ask if you would like to purchase another drink. (y/n) If you choose y for yes, the program will loop again, starting over with $0.00 entered and begin asking the user to enter money. If the user chooses n for no, the loop will exit and an output statement will tell the user what all they have purchased.
Each time they purchase an item, add one to the number purchased.

#include <iostream>
using namespace std;

// declare and initiate variables and one array
	double selection = 0;
	double sodaPrice = 1.00;
	double inMachine = 0;
	double newDep = 0;
	double change = 0;
	char more = 'y';
    int a[] = {0,0,0};

	void insertMoney();
	void displayMenu();
	void giveChange();

int main()
{
	cout << " ======================================= "  <<endl;
	cout << "Hello, My name is Antonio Brown." << endl;
	cout << "This is my week 3 Program" << endl;
	cout << " ======================================= "  <<endl;

	insertMoney();
	displayMenu();
	giveChange();
	return 0;
}

void insertMoney()
{
    
while (more == 'y')
    {
    // This while loop will keep executing until the logic is false
    while (inMachine < sodaPrice)
    {
    // Outputing to console display and passing inputs to variables
    cout << "Please insert $" << sodaPrice << endl;
    

    // Subtracting the amount in the machine from the cost of the soda
    cout << "Amount Needed: $" << (sodaPrice - inMachine) << endl;
	cout << " ======================================= "  <<endl;
    cout << "Amount you wish to deposit: $" <<endl;
	cin >> newDep;
	cout << " ======================================= "  <<endl;
	cout << "You have deposited: $" << (inMachine + newDep) << endl;
    inMachine = (newDep + inMachine);
	}
}
}


void displayMenu()
{
    cout << "Thank you! Please select a type of soda:" << endl << endl;
    cout << "Coke              -  " << 1 << endl;
    cout << "Pepsi             -  " << 2 << endl;
    cout << "Dr. Pepper        -  " << 3 << endl;
    cout << "Diet Coke		   -  " << 3 << endl;
    cout << "Coke Zero           -  " << 4 << endl; 
    cin >> selection;
    
    if (selection == 1)
    {
    // Incrementing the array for each soda bought
    a[0] = a[0]+1;
    cout << "Thank you and enjoy your Coke!" <<endl;
    } 
    else if (selection == 2)
    {
    a[1] = a[1]+1;
    cout << "Thank you and enjoy your Pepsi!" <<endl;
    }
    else if (selection == 3)
    {
    a[2] = a[2]+1;
    cout << "Thank you and enjoy your Dr. Pepper!" <<endl;
    }
    else if (selection == 3)
    {
    a[3] = a[3]+1;
    cout << "Thank you and enjoy your Diet Coke!" <<endl;
    }
    else if (selection == 4)
    {
    a[4] = a[4]+1;
    cout << "Thank you and enjoy your Coke Zero!" <<endl;
    }

}      
void giveChange()
{
    if (inMachine > sodaPrice)
	{
    cout << "Your change is: $" << (inMachine - sodaPrice) << endl;
             }
    // Clearing the variables
    inMachine = 0;
    selection = 0;
    cout << "Would you like to purchase another soda? Y or N ";
    cin >> more;

    // Displaying the totals of soda bought
    cout << "You have purchased " << a[0] << " Coke(s)" << endl;
    cout << "You have purchased " << a[1] << " Pepsi(s)" << endl;
    cout << "You have purchased " << a[2] << " Dr. Pepper(s)" << endl;
	cout << "You have purchased " << a[2] << " Diet Coke (s)" << endl;
	cout << "You have purchased " << a[2] << " Coke Zero (s)" << endl;
    system("PAUSE");
 }
2
Contributors
1
Reply
4
Views
7 Years
Discussion Span
Last Post by hag++
0

I dont know if Im totally blind (which is an option) but what problems are you having with the code. I was going to read through it and see if I could spot something but its easier if I know whats wrong.... guess I could also compile/ run it too

*EDIT: okay so I compliled your code, and the problem is in the insert money function:

while (more == 'y')
    {
    // This while loop will keep executing until the logic is false
    while (inMachine < sodaPrice)
    {
    // Outputing to console display and passing inputs to variables
    cout << "Please insert $" << sodaPrice << endl;
    

    // Subtracting the amount in the machine from the cost of the soda
    cout << "Amount Needed: $" << (sodaPrice - inMachine) << endl;
	cout << " ======================================= "  <<endl;
    cout << "Amount you wish to deposit: $" <<endl;
	cin >> newDep;
	cout << " ======================================= "  <<endl;
	cout << "You have deposited: $" << (inMachine + newDep) << endl;
    inMachine = (newDep + inMachine);
	}
}

You never update the loop variant for the top while loop. After internal while loop finishes, ask the user if they want to buy andother soda, and update the "more" variable. Another thing to be careful for is that your a re using all global variables. This makes debugging difficult and it increases the chances for logical errors.

Edited by hag++: forgot

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.