Please HELP !!!

I would not be doing this if it wasn't this late in the semester, but i am stuck if anyone finds it in them to help me i would be forever grateful, this assigment is due today at 11:00 AM and i just can't i've been at it for hrs and haven't gotten anywhere and this is the last assigment of the semester, so if anyone can help me her it is:

Instructions:

Please see the input file ("DrinkMachineInventory.txt"). Each time the program runs, it should read the data from the input file and then enter a loop that performs the following steps: A list of drinks is displayed on the screen. The user should be allowed to either quit the program or pick a drink. If the user selects a drink, he or she will next enter the amount of money that is to be inserted into the drink machine. The program should display the amount of change that would be returned and subtract one from the number of that drink left in the machine. If the user selects a drink that has sold out, a message should be displayed. The loop then repeats. When the user chooses to quit the program it should display the total amount of money the machine earned.

My code so far:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int SIZE = 5;
const int CHAR_SIZE = 17;

struct VendingMachine
{
char sodaName[CHAR_SIZE];
double sodaPrice;
int sodaAvailable;
};

struct Money
{
double custMoney, change;
};

void displayChoices(VendingMachine [], int );
void captureItemValues(ifstream& , VendingMachine [], int );

int main(int argc, char *argv[])
{
VendingMachine sodaType[SIZE];
Money customerMoney;
ifstream inFile;
ofstream outFile;
int userChoice;

inFile.open("DrinkMachineInventory.txt");
captureItemValues(inFile, sodaType, SIZE);
inFile.close();

cout << "1.Buy drink" << endl;
cout << "2.Exit" << endl;
cout << "Enter your choice: ";
cin >> userChoice;

if (userChoice == 1)
{
cout << "Select your Drink" << endl;

displayChoices(sodaType, SIZE);

cout << "Enter money into the machine: ";
cin >> customerMoney;

while (customerMoney > 1.00)
{
cout << "Only amounts lower than 1.00 are accepted." << endl;
cout << "Try Again: ";
cin >> customerMoney;
}

}
else
{
cout << "Thank you, have a great day.";
}

system("PAUSE");
return EXIT_SUCCESS;
}

void displayChoices(VendingMachine soda[], int size)
{
for (int i=0; i < size; i++)
{
cout << left << setw(12) << soda[i].sodaName;
cout << left << setw(8) << soda[i].sodaPrice;
}
}

void captureItemValues(ifstream& inFile, VendingMachine sodaType[], int size)
{
for (int i=0; i<size; i++)
{
inFile.getline(sodaType[i].sodaName, CHAR_SIZE, '\0');
inFile.clear();
inFile.sync();
inFile >> sodaType[i].sodaPrice;
inFile >> sodaType[i].sodaAvailable;
inFile.clear();
inFile.sync();
}
}

Txt file: (use it exactly as it is formated here so that what i have runs properly)

Coca-Cola .75 20
Root Beer .75 20
Sprite .75 20
Spring Water .80 20
Apple Juice .95 20

Thx in advance !
So what's your excat problem?

What I can see is that your file format is bad. The fields name/price/available are separated by space. But within the first field (name) you have also a space. So getline() and >> won't work. What you can do is reading it char by char and determine if the next field starts (ispunct() / isdigit())

http://www.cplusplus.com/reference/clibrary/cctype/

oh I see it's too late...
well I think im too much of a begginer, but besides that the concept of references confuses me so it's hard for me to think in functions. My main problem is getting the file to update the number of items left for each sodaType.
Think of references like this:
You are 'main' and someone you know wants to write something down for you.

//getting a return value.
You pass him a piece of paper.
He then returns it to you with something on it.

//pass by refference
As main, you pass by reference. so when you pass him the paper, he doesn't have to give it back to you. You already know what's written down.

When you pass by reference, what ever changes are made within a function, they are the same in the function that called it.

As for the soda available:
if sodaAvailable is greater than 0,
take away 1 soda.
else
display a message saying there are none left
Thank you that was actually the best anyone has explained it to me.
Last edited on
You're welcome. Are you still going to keep on coding this, even though it's been handed in?
Yes better late than never.
Well, better to finish it for yourself rather than making an old deadline, even just for practise!

I've taught myself everything I know, my old teacher was s... lol. Most of my knowledge of C++ is finishing projects that were late, ahaha

If you need any more help with this, just post back! (:
Topic archived. No new replies allowed.