Confused on Subtracting from struct element

Hello,
My program is supposed to read from the user a soda name, the price and the quantity. Then the user makes makes a selection of what they want.

Here's where I'm stuck, in my transaction function will make sure the product is in stock and if it's not it will tell the user.
It also must input the money from the user, it must be less than a dollar and also calculate the change.

Anyway, here's what I've got so far. This is my first program dealing with structs so I'm still learning.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <string>
#include <iomanip>
using namespace std; 


const int SIZE = 10;
struct Drink {
    string Name;
    double Cost;
    int numDrinks;
};

void fillMachine (Drink machine[], int &numitems);
void getChoice (Drink machine[], int numitems, int i, int &choice);
void transaction (Drink machine[], int &numitems, int i, int &choice);

int main()
{
    int i = 0;
    int numitems = 0;
    int choice = 0;
    Drink machine[SIZE];

	
	fillMachine (machine, numitems);
	getChoice (machine, numitems, i, choice);
	transaction (machine, numitems, i, choice);
   
    
    return 0;
}


    
void fillMachine (Drink machine[], int &numitems)
{
    string mydrink;
    // Get the drink name
   cout << "Enter the first drink, Q to quit: ";
   getline(cin, mydrink);
   
   while (mydrink != "Q" && numitems < SIZE)
   {
	   machine[numitems].Name = mydrink;
	   // Get the price
	   cout << "Enter the price: ";
	   cin >> machine[numitems].Cost;

	   // Get the quantity
	   cout << "Enter the quantity: ";
	   cin >> machine[numitems].numDrinks;
	   cin.ignore();
	   numitems++;
	   cout << "Enter the first drink, Q to quit: ";
	   getline(cin, mydrink);
	}

}

void getChoice (Drink machine[], int numitems, int i, int &choice)
{
cout << endl;
for (i=0;i<numitems;i++)
	cout << (i+1) << ". " << machine[i].Name  << endl;
cout << (i+1) << ". Exit" << endl;

cin >> choice;

if (choice > (numitems+1))
	cout << "Bad choice." << endl;

cout << endl;


}




void transaction (Drink machine[], int &numitems, int i, int &choice)
{






} 


Any suggestions or tips?
Thanks
Last edited on
If you insist on using void for your function return types then you have to pass pointers of the local variables you wish to change.

Think about how you would rewrite "fillMachine(...)" so that Lines 40 & 55 aren't copies of eachother? If you can't think of how to do it with a while(...) loop , there is a way though, maybe do...while(...) is a better choice?

Line 54 should not be needed, your structure already has a variable to keep track of the quantity. Each "Brand" of soda should be it's own version of the same object.

I stopped reading at line 57. I can't address your question unless you make the changes I've pointed out.

Last edited on
numitems keeps track of how many drinks are actually entered. SIZE is how many elements the array can hold but since it's the size of the array it can't be adjusted. The user doesn't have to fill the array therefore the numitems variable it required to keep track of how many drinks the user actually entered.

If you have a way around the I'd like to know.

And the reason lines 40 and 55 are the same is because if the user enters "Q" the loop will never even execute. At the end of the loop it asks the user again in case they have another drink. I suppose a way around having the same line twice would be possibly just to call the function at the end of the while loop? Again I don't know so if you have an idea I'd appreciate if you'd share your knowledge because I'm a beginner.

But besides that (as far as I know) the part I have done is doing everything correctly.
If the user enters "Q" that means they didn't want the loop to execute :p.

As for numitems, you don't have a variable at the moment to keep track of how many of a given item are in stock, which I imagine is part of the reason you are stuck on your transactions function. "numitems" seemed to me like the logical choice for this since it will be different for each item in the "machine" array and therefore will not be useful for what you are describing. I got numitems and numDrinks confused, sorry that one is on me.

I suppose a way around having the same line twice would be possibly just to call the function at the end of the while loop?

Interesting solution, how would that look? Use comments "\\" or "/*...*/" to edit out your current code without destroying it and write it up. Does it run like you think it would? Repost it when you're done. I'm not trying to be a PITA, so if I get on your nerves tell me I won't just give you the code but I'll try to be less obnoxious.

EDIT: You didn't mention anything about what I said regarding pointers and\or return values for your functions. Did you need me to explain this part?
Last edited on
If I called the function from the end of the while statement it would look like this;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <string>
#include <iomanip>
using namespace std; 


const int SIZE = 10;
struct Drink {
    string Name;
    double Cost;
    int numDrinks;
};

void fillMachine (Drink machine[], int &numitems);
void getChoice (Drink machine[], int numitems, int i, int &choice);
void transaction (Drink machine[], int &numitems, int i, int &choice);

int main()
{
    int i = 0;
    int numitems = 0;
    int choice = 0;
    Drink machine[SIZE];

	
	fillMachine (machine, numitems);
	getChoice (machine, numitems, i, choice);
	transaction (machine, numitems, i, choice);
   
    
    return 0;
}


    
void fillMachine (Drink machine[], int &numitems)
{
    string mydrink;
    // Get the drink name
   cout << "Enter the first drink, Q to quit: ";
   getline(cin, mydrink);
   
   while (mydrink != "Q" && numitems < SIZE)
   {
	   machine[numitems].Name = mydrink;
	   // Get the price
	   cout << "Enter the price: ";
	   cin >> machine[numitems].Cost;

	   // Get the quantity
	   cout << "Enter the quantity: ";
	   cin >> machine[numitems].numDrinks;
	   cin.ignore();
	   numitems++;
	   getChoice (machine, numitems, i, choice);
	}

}

void getChoice (Drink machine[], int numitems, int i, int &choice)
{
cout << endl;
for (i=0;i<numitems;i++)
	cout << (i+1) << ". " << machine[i].Name  << endl;
cout << (i+1) << ". Exit" << endl;

cin >> choice;

if (choice > (numitems+1))
	cout << "Bad choice." << endl;

cout << endl;


}




void transaction (Drink machine[], int &numitems, int i, int &choice)
{






} 



Does it run like you think it would?

Right now, it does.
EDIT: You didn't mention anything about what I said regarding pointers and\or return values for your functions. Did you need me to explain this part?

Were just now learning pointers in CS so I'm REALLY not good at those. At first I had my getChoice function returning Choice but I was getting a weird error when trying to pass Choice to another function. When I tried to cout choice in my Transaction function it was 0. So i changed the getChoice function to a void and passed it by reference and it's working the way it's supposed to. I did a cout in Transaction to test it out and make sure it was passing the correct value and it was.
Let's start with pointers then, their intimidating little buggers but their also very handy.

First you should understand that, with few exceptions, a variable does not exist outside of it's function (it's scope). So "i, numitems, choice and machine" do not exist in your other functions until you pass them there, which you seem to understand fine. When you pass them though you're really just giving the function local copies of those variables to use so any modifications that you make are NOT made to those variables you pass, but to copies of them that only exist inside of that function. NOTE: By passing a variable from main to fillMachine and then to getChoice you are copying the value from main to fillMachine making any changes there then copying that copy of the variable and giving the new copy to getChoice, so in this way changes seem like they can be made to variables but no matter what you do in the other two functions the copy of the variable in main does not change this way.

When you pass a pointer to a function you're not telling the function to work with this value, your saying work with the value AT this memory address. So when the function makes changes, those changes are reflected on that address and therefore on that origional variable. This is important to your program because you are changing multipule variables at a time within the functions.

With me so far?

EDIT: I really am horrible at this I know that I'm making changes probably as you read this and I'm sorry.
Last edited on
I promise I'm done editing and will not post further until I see a response.
I understand the idea behind pointers (although that does clear up a few questions that I had) but its integrating them I'm not good with yet. I'm still trying to figure out structs.

Is my program okay AS IS to be able to complete the tasks that I need it to? I know my CS teacher doesn't expect pointers to be in this program although I bet they'll be in the next one.

How do I get my function to make sure that the selected choice is in stock? Then ask them to enter the money and calculate the change.
Here's an example of what it should look like:
~/cs2020c$ runprog2.exe                                                         
Enter the first drink, Q to quit: Mountain Dew
Enter the price: .75
Enter the quantity: 4
Enter the next drink, Q to quit: Sunkist
Enter the price: .80
Enter the quantity: 6
Enter the next drink, Q to quit: Q

1) Mountain Dew   0.75
2) Sunkist        0.80
3) Exit

Choose one: 1
Enter an amount of money: .95

Thump, thump, thump, splat!
Enjoy your beverage!

Change calculated: 0.20
Your change, 0.20 has just dropped into the Change Dispenser.

There are 3 drinks of that type left.

1) Mountain Dew   0.75
2) Sunkist        0.80
3) Exit

Choose one: 3
Total earnings: $0.75
Last edited on
Use an "if(...)" check to determine if the type of soda they chose has a numDrinks greater then 1. If true, then ask them for the amount of money entered. Subtract the price of the soda from the amount tendered and the remainder is the change to dispense.
Use an "if(...)" check to determine if the type of soda they chose has a numDrinks greater then 1. If true, then ask them for the amount of money entered. Subtract the price of the soda from the amount tendered and the remainder is the change to dispense.

Thanks, I'll try to make that work. What I'm more confused about is subtracting one from the struct, or do I subtract it from the array?
You subtract one from numDrinks of the appropriate member in the array to track inventory. Don't worry wrapping your head around the convoluted stuff gets easier.
What gets me is this part right here. Like numDrinks[numitems]-1?
something.something is what confuses me.

Then again I also thought arrays were confusing a few months ago when I first learned them and now I can simultaneously sort 3 of them.
It's more like machine[WhichEverOne].numDrinks = machine[WhichEverOne].numDrinks - 1 but you were close, just keep in mind that members of of a struct have to be addressed in relation to that struct.
That makes more sense. It's somewhat like an array then. I'll start integrating this all tomorrow and this weekend, the programs due Sunday at 11:59 so I'll definitely be referencing this thread and probably be posting back.
Thanks for your help computergeek.
Edit:
Figured it out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void transaction (Drink machine[], int &numitems, int &i, int &choice)
{
	
	
	i = choice - 1;	

if (machine[i].numDrinks < 1)
		cout << "This item is not in stock." << endl;

if (machine[i].numDrinks > 0 )
{	
	machine[i].numDrinks = machine[i].numDrinks - 1;
	cout << "There are " << machine[i].numDrinks << " drinks left" << endl;
}





Now I just need the user to enter the amount of money, if it's enough I need to calculate the change due. This will be fun.
Last edited on
Topic archived. No new replies allowed.