Question:
can someone help me fix my c++ code please?
anonymous
2008-12-09 18:54:40 UTC
my code is not compiling can anyone help please!
This is what i need to do:
I need to create a class (called MachineManager) that simulates a soft drink machine. The class should use a struct that stores
the following information:

Drink name
Drink price
Number of drinks in machine

The class should have an array of five of these struct.
The elements should be initialized (using a default constructor) to handle the
initialization with the following data:


--------------------------------------------------------------------------------
Drink Name < Cost <<< Number in Machine
Cola < .75 <<<< 20
Root beer < .75 <<<<< 20
Orange Soda < .75 <<<<20
Grape Soda < .75 <<<<20
Bottled water 1.00 <<<20

--------------------------------------------------------------------------------




#include
#include
#include

using namespace std;

struct Drinks
{
string Name;
double Price;
int Quantity;
};

class MachineManager
{
private:
void inputMoney();
void dailyReport();
public:
void displayChoices();
void buyDrink (int);
};

void displayChoices ()
{
cout << "**** Machine Beverage Choices ****" << endl;
cout << " 1) Cola $ 0.75"< cout << " 2) Root Beer $ 0.75"< cout << " 3) Orange Soda $ 0.75"< cout << " 4) Grape Soda $ 0.75"< cout << " 5) Bottled Water $ 1.00"< cout << " 6) Exit" << endl;
cout << " Please enter the number of your drink choice" << endl;
cin >> int choice;
buyDrink (int);
}

void buyDrink (choice)
{
inputMoney (choice);
}

void inputMoney (choice)
{
if (Quantity = 0)
cout << "Sold out. Please remove your change." << endl;

cout << "Please insert sufficient money for your beverage choice" << endl;
cout << "Enter 0 if you do not want to purchase the drink" << endl;
cin >> Price;
if (int = 6)
cout << "Please remove your change" << endl;
if (int <= 4)
{if (Price < 0.75)
cout << "Incorrect amount of money, please remove your money from the coin return slot" << endl;
else
double change = Price - 0.75;
{if (change = 0)
cout << "Enjoy your drink" << endl;
else
cout << "Your change is" << change << endl;
cout << "Enjoy your drink" << endl;}}
if (int = 5)
{ if (Price < 1)
cout << "Incorrect amount of money, please remove your change from the coin slot" << endl;
else
{double change = Price - 1.0;
if (change = 0)
cout << "Enjoy your drink" << endl;
else
cout << "Your change is" << change << endl;
cout << "Enjoy your drink" << endl;}
}

void dailyReport ()
{
Drinks drinkinfo[4];
Drinks[0].Name = Cola, Price = 0.75, Quantity = 20;
Drinks[1].Name = RootBeer, Price = 0.75, Quantity = 20;
Drinks[2].Name = OrangeSoda, Price = 0.75, Quantity = 20;
Drinks[3].Name = GrapeSoda, Price = 0.75, Quantity = 20;
Drinks[4].Name = BottledWater, Pirce = 1.00, Quantity = 20;
if (choice = 1, choice++,)



int getChoice(int);

int main()
{
MachineManager machine; // Create a MachineManager object
int quitChoice = 6;
int choice; // Patron menu choice

do
{ machine.displayChoices(); // Lists choices and prices
cout << setw(5) << quitChoice << ") Exit \n\n" ;
choice = getChoice(quitChoice);
if (choice != quitChoice)
machine.buyDrink(choice);

} while (choice != quitChoice);

return 0;
}

int getChoice (int quitChoice)
{
int choice;

cin >> choice;
while (choice < 1 || choice > quitChoice)
{
cout << "Valid choices are 1 - " << quitChoice
<< ". Please re-enter choice: ";
cin >> choice;
}
return choice;
}
Three answers:
The Phlebob
2008-12-09 19:15:52 UTC
I see two major problems with this snippet:



if (choice = 1, choice++,)







int getChoice(int);



First, although the if statement may compile, it won't do what you expect it to. choice = 1 doesn't test choice for being one, it sets it to one. The choice++, while legal after a comma (old C syntax), will always be executed, setting choice then to 2.



The int getChoice(int) will then appear to be the executable target of the if -- but it isn't an executable statement. It's a declaration. In fact, the function that contains the if statement doesn't appear to have a terminating }.



That's what I see this far.
Hex
2008-12-10 17:38:22 UTC
Ok dude. Wow. Come on now, what is this?



You're not using operators correctly, you're not adding the appropriate braces with your ifelse statements and your code has no comments.



When you write code you might not always need to use a brace, but when you're starting out -You- need to use them.



if( statement ) { // BRACE



// Stuff here



} // BRACE



else

{ // BRACE



// Stuff here

} // BRACE





I cannot read anything you're doing and since no one can read it we're not going to go through and rewrite it all just so we can. You're this far into programming, you need to go back to chapter 1 where they talked about formatting your syntax correctly before you move on. You're skipping way too much.







Words of Wisdom,

- Hex
Moxie1313
2008-12-10 03:06:08 UTC
You've got quite a few basic errors here...



For example, whenever you're comparing two numbers, you're only using one "=" sign, when you should be using two.



I didn't read through the entire bit of code, but it's also a bit disorganized. Clean it up a bit, add comments, and most of the basic problems will begin to stand out more.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...