I need some struct guidance


Okay I'm new to structs and this is my first program dealing with them. I'm lost on the part where I get the user to enter the information and I need some help. I think if I can get past this part I should be able to figure out the rest.


Here's where I am now.
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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std; 


struct Drink {
    string Name;
    double Cost;
    int numDrinks;
};

void fillMachine (Drink&);

int main()
{
    const int SIZE = 10;
    Drink machine[SIZE];

for (int i=0;i<SIZE;i++)
{
fillMachine (machine[i]);
}   
    
    
    return 0;
}


    
void fillMachine (Drink& mydrinkvariable)
{
    
    // Get the drink name
   cout << "Enter the first drink, Q to quit: ";
   cin.getline(mydrinkvariable.Name);

   // Get the price
   cout << "Enter the price: ";
   cin >>mydrinkvariable.Cost;

   // Get the quantity
   cout << "Enter the quantity: ";
   cin >> mydrinkvariable.numDrinks;

}










Here's an example of the final result.
~/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
You sort of have that part already, you just need a mechanism to quit.
I suggest that you make fillMachine return a bool.
On line 37, if mydrinkvariable.name == "Q" return false;
On line 45, return true;

Line 22 would be if(!fillMachine( machine[i] )) break;
Also, declare i in int main() before the for loop, so that when the for loop ends, you know how many drinks were entered.
I tried to integrate your suggestions and here is the revised code.
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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std; 


struct Drink {
    string Name;
    double Cost;
    int numDrinks;
};

void fillMachine (Drink&);

int main()
{
    const int SIZE = 10;
    Drink machine[SIZE];
	int i = 0;

for (int i=0;i<SIZE;i++)
	if(!fillMachine( machine[i] )) break;

  
    
    
    return 0;
}


    
void fillMachine (Drink& mydrinkvariable)
{
    
    // Get the drink name
   cout << "Enter the first drink, Q to quit: ";
   cin.getline(mydrinkvariable.Name);
   if (mydrinkvariable.Name == "Q")
	   return false;
   // Get the price
   cout << "Enter the price: ";
   cin >>mydrinkvariable.Cost;

   // Get the quantity
   cout << "Enter the quantity: ";
   cin >> mydrinkvariable.numDrinks;
   return true;
}


It's returning with these errors:
1>prog 2.cpp(39): error C2562: 'fillMachine' : 'void' function returning a value
1> prog 2.cpp(13) : see declaration of 'fillMachine'
1>prog 2.cpp(47): error C2562: 'fillMachine' : 'void' function returning a value
1> prog 2.cpp(13) : see declaration of 'fillMachine'
1>
1>Build FAILED.

I understand that a void function can't return values so is there something else I could do or just change the function?
It should return a boolean; so you would change the return value to bool instead of void.
Topic archived. No new replies allowed.