Using Arrays with Structs

So my Professor had my class work on a project one way and is now asking us to change part of it which is what I'm confused about. Basically I think some things need to be moved around but I'm not sure how..I might just be confusing myself on this...here's what my Professor is asking for:

Should display the selection number, the drink name, and the cost. It also must display the amount left over in the drink inventory after a drink has been bought. If the user selects a drink, you must display a “Sold Out” message if the inventory is 0. Otherwise, have the user enter the amount of money being deposited into the machine.
Include a function to get a valid choice to be returned to the main routine. Include a second function to process the transaction. Hint: This should include a pass by reference variable.
Last edited on
put your code in Code Format and with proper indentation, since it increases readability !!
Sorry about that. Should be easier to read now.
Some coments on your code:

Lines 6-8: Why are these global? Globals should be avoided when possible.

Line 53: First time through the loop you going to add an uninitialized value (payment) to earnings. Also, this won't compute earnings correctly since the last time through the loop payment won't get added. You're calculating earnings from the amount the user paid before considering change. You should be using drinkInfo.cost.

Lines 60-71, 74-85, 88-99, 102-113, 116-127: All these code segments are identical except for the selection number. Perfect candidate for a function.

Lines 63-68, 77-82, 91-96, 105-110, 119-124: You're going to display "Invalid amount of change" the first time through the do/while loop even if the amount was value. You want a while loop, not a do/while.

Line 124: What if I want a Diet Coke which is 1.25? I can't put in 1.25 because you're comparing against 1.00

Line 134: You're ignoring the return value from displayMenu.

Line 143: Why are you passing choice as an argument? There is no reason to pass it. choice should be a local variable.

I see no adjustment to inventory nor a check for "Sold out".

I see no second function to process the transaction.

Lines 146-150: Your choices here don't agree with drinkList. You should be displaying from drinkList.








Basically she wants us to use array's and stuff but I have no idea how to actually get the information from the array to output into my program..
Last edited on
Well that was a little drastic. :)

You should have kept your displayMenu function. There were only minor issues with it. You can also enhance displayMenu to add a loop that ensures that choice is a valid select from 1 to ArraySize+1 (for quit). By passing Items as an argument to displayMenu, you have can access drinkInfo.drinkname when you display the menu.

Replace 38-41 of you new code with
1
2
 
  selection = displayMenu(Items);


Here's an updated version of displayMenu:
1
2
3
4
5
6
7
8
9
10
11
12
int displayMenu (drinkinfo items[])
{  int choice;
    cout << endl;
    for (int i=0; i<ArraySize; i++)
        cout << i+1 << ". " << items[i].drinkname << endl;
    cout << ArraySize+1 << ". Quit" <<endl;
    cout << endl;
    cout << "Your selection: ";
    cin >> choice;
    //  Do range checking here 
    return choice;
} 


What you need to focus on now is the ProcessTransaction function.

edit: struct drinkInfo and ArraySize should be global rather than in main.
Last edited on
Mhmm :) I get overwhelmed easily and decided I would just scrap everything and start over.

Okay I made the changes that you were talking about when it came to moving things into global. I also used the new displayMenu you suggested but when I run the program it repeats the word Quit for every selection choice.
Last edited on
Line 26: Belongs before main, not inside it.

Line 54: You missed my line 5. That is the line that should be within the scope of the for loop.

Now get to work on ProcessTransaction()
Ok so I've been working on the other function for it but it keeps displaying the same thing...like if i pick choice 1 it displays "Pepsi" but if i pick choice 4 it still displays "Pepsi"...I think I have it set up right so im not sure what's wrong..
Last edited on
That's progress.

Line 29-30 belong BEFORE main.

Line 68-71: You not checking that the value entered is valid. What if 0 or 7 was entered?

Line 81-113: You're using the assignment operator (=), not the equality operator (==) in your if statements.

Do you see a pattern between all your if branches (81-113)? The only thing different between them is the index to Items. There is no reason to use a separate if statement for each one. Simply use choice-1 as the subscript.

I don't understand the logic at line 129. The payment amount can be less than required for ANY item.

I still don't see any updating of inventory. What if I try to but two Pepsis?

Not seeing any calculations of earnings.
Topic archived. No new replies allowed.