Soda Vending machine Problem

I am having trouble with enumeration and array in my soda vending machine code. This program is composed of numerous functions, including inserting money, selecting soda, calculating change (if any), a loop to continue or not, then my file IO includes printing a receipt, and in this section with the file IO, I added array with soda count. SO, the problem is I declared enumeration variables, but I do not know how to implement it in the code, I just declared it. Also, with the File IO and array (it's in the same section, near bottom) I intended the file output to print out a receipt with the soda count and the amount deposited and change, but it does not print it out, it prints out something else).
So can anyone please help me with the enumeration, and the file IO and array?
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>                //Basic input and output library//
#include <iomanip>  
#include <fstream>               
using namespace std;

double machineTotal = 0.0;            //Declare Variables//
int sodasPurchased = 0;
const double price = 1.00;
enum soda {COKE, SPRITE, DRPEPPER} type;

// Return total amount of money inserted.
double insertMoney()
{
   // These can be local variables, and we'll return
   // the total to the caller f
    //when we need to give change.
   double totalDeposited = 0.0;
   double deposit = 0.0;

   cout <<"Please enter $1.00" <<endl;
   cin >> deposit;

   totalDeposited += deposit;

   // Loop until we've received enough money
   while (totalDeposited < price)
   {
      cout << "Amount Needed: $" << price - totalDeposited << endl;
      cin >> deposit;
      totalDeposited += deposit;
   }

   machineTotal += totalDeposited;
   return totalDeposited;
}

double giveChange(const double amountDeposited)
{
   double changeDue = amountDeposited - price;
   if (changeDue > 0.0)
   {
      cout << "You have inserted $" << amountDeposited << endl;
      cout << "Please take your change of $" << changeDue << endl;
      machineTotal -= changeDue;
   }
}

soda displayMenu()
{
   int selection = 0;

   cout << "Thank you, please make your selection!" <<endl;
   cout << "Coke              -  " << 0 << endl;
   cout << "Sprite            -  " << 1 << endl;
   cout << "Dr. Pepper        -  " << 2 << endl;
   cin >> selection;
   cout << endl;

   while ( selection < 0 && selection > 3 )
   {

      cout << "Please select a choice between 0 and 2" << endl;
      cout << "Thank you, please make your selection!" <<endl;
      cout << "Coke              -  " << 0 << endl;
      cout << "Sprite            -  " << 1 << endl;
      cout << "Dr. Pepper        -  " << 2 << endl;
      cin >> selection;
      cout << endl;
   }

   switch (selection)
   {
   case 0:
      cout << "Thank you and enjoy your Coke!" <<endl;
      break;
   case 1:
      cout << "Thank you and enjoy your Sprite!" <<endl;
      break;
   case 2:
      cout << "Thank you and enjoy your Dr.Pepper!" <<endl;
      break;
   }
}

int main()
{

   cout << "Welcome to the Coke Machine" << endl;
   int question = 0;
   soda sodas[3];
   int count = 0;
   do
   {
      double amountDeposited = insertMoney();
      sodas[count] = displayMenu();
      double changeDue=giveChange(amountDeposited);
      ++sodasPurchased;
      ofstream outfile;  
      outfile.open("Receipt.txt"); 
      outfile <<  amountDeposited << sodas[count] << changeDue;
      outfile.close(); 
      cout << "Would you like to purchase another soda? (1=yes 2=no)" << endl;
      cin >> question;
     count++;
   } while (question == 1 && count < 3);
  
   return 0;
}

Last edited on
BUMP
BUMP
You have an array soda sodas[3]; - but nowhere do you set any of the values in this array - so the second value on a line in your receipt which is sodas[count] is just rubbish.

OOPS I need to edit this:
You are not returning the value of the soda from the displayMenu function - so the
value of sodas[count] is rubbish.


You do not return the change value from your giveChange function - so the line double changeDue=giveChange(amountDeposited); gives changeDue an undefined value (this means that the third value on a line on the receipt is rubbish - if this value shows as NAN that means Not A Number ).

Another EDIT:
By the way - if you expect the actual words COKE, SPRITE, DRPEPPER to appear in your receipt file -
they won't - you will get a value of 0, 1 or 2 - if you want the words you will have to add some more
code.
Last edited on
ok i just fixed enum but, i still am having trouble with the array.
guestgulkan what do you suggest, i already know that it gives me back and undefined value
this is what came out on the text file 244697121.#QNAN, how do i fix it, below is my updated code with enum
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>                //Basic input and output library//
#include <iomanip>  
#include <fstream>               
using namespace std;

double machineTotal = 0.0;            //Declare Variables//
int sodasPurchased = 0;
const double price = 1.00;
enum soda {COKE, SPRITE, DRPEPPER} type;

// Return total amount of money inserted.
double insertMoney()
{
   // These can be local variables, and we'll return
   // the total to the caller f
    //when we need to give change.
   double totalDeposited = 0.0;
   double deposit = 0.0;

   cout <<"Please enter $1.00" <<endl;
   cin >> deposit;

   totalDeposited += deposit;

   // Loop until we've received enough money
   while (totalDeposited < price)
   {
      cout << "Amount Needed: $" << price - totalDeposited << endl;
      cin >> deposit;
      totalDeposited += deposit;
   }

   machineTotal += totalDeposited;
   return totalDeposited;
}

double giveChange(const double amountDeposited)
{
   double changeDue = amountDeposited - price;
   if (changeDue > 0.0)
   {
      cout << "You have inserted $" << amountDeposited << endl;
      cout << "Please take your change of $" << changeDue << endl;
      machineTotal -= changeDue;
   }
}

soda displayMenu()
{
   int selection = 0;

   cout << "Thank you, please make your selection!" <<endl;
   cout << "Coke              -  " << 0 << endl;
   cout << "Sprite            -  " << 1 << endl;
   cout << "Dr. Pepper        -  " << 2 << endl;
   cin >> selection;
   cout << endl;

   while ( selection < 0 && selection > 3 )
   {

      cout << "Please select a choice between 0 and 2" << endl;
      cout << "Thank you, please make your selection!" <<endl;
      cout << "Coke              -  " << 0 << endl;
      cout << "Sprite            -  " << 1 << endl;
      cout << "Dr. Pepper        -  " << 2 << endl;
      cin >> selection;
      cout << endl;
   }
   if (selection==0)
   {type=COKE;
   }
   if (selection==1)
   {type=SPRITE;
                    }
   if (selection==2)
   {type=DRPEPPER;
   }
   switch (type)
   {
   case COKE:
      cout << "Thank you and enjoy your Coke!" <<endl;
      break;
   case SPRITE:
      cout << "Thank you and enjoy your Sprite!" <<endl;
      break;
   case DRPEPPER:
      cout << "Thank you and enjoy your Dr.Pepper!" <<endl;
      break;
   }
}

int main()
{

   cout << "Welcome to the Coke Machine" << endl;
   int question = 0;
   soda sodas[3];
   int count = 0;
   do
   {
      double amountDeposited = insertMoney();
      sodas[count] = displayMenu();
      double changeDue=giveChange(amountDeposited);
      ++sodasPurchased;
      ofstream outfile;  
      outfile.open("Receipt.txt"); 
      outfile <<  amountDeposited << sodas[count] << changeDue;
      outfile.close(); 
      cout << "Would you like to purchase another soda? (1=yes 2=no)" << endl;
      cin >> question;
     count++;
   } while (question == 1 && count < 3);
  
   return 0;
}
 

closed account (LTXN8vqX)
I just set return statements at the end of two functions. Here is your fixed 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>                //Basic input and output library//
#include <iomanip>  
#include <fstream>               
using namespace std;

double machineTotal = 0.0;            //Declare Variables//
int sodasPurchased = 0;
const double price = 1.00;
enum soda {COKE, SPRITE, DRPEPPER} type;

// Return total amount of money inserted.
double insertMoney()
{
   // These can be local variables, and we'll return
   // the total to the caller f
    //when we need to give change.
   double totalDeposited = 0.0;
   double deposit = 0.0;

   cout <<"Please enter $1.00" <<endl;
   cin >> deposit;

   totalDeposited += deposit;

   // Loop until we've received enough money
   while (totalDeposited < price)
   {
      cout << "Amount Needed: $" << price - totalDeposited << endl;
      cin >> deposit;
      totalDeposited += deposit;
   }

   machineTotal += totalDeposited;
   return totalDeposited;
}

double giveChange(const double amountDeposited)
{
   double changeDue = amountDeposited - price;
   if (changeDue > 0.0)
   {
      cout << "You have inserted $" << amountDeposited << endl;
      cout << "Please take your change of $" << changeDue << endl;
      machineTotal -= changeDue;
   }
   return changeDue;
}

soda displayMenu()
{
   int selection = 0;

   cout << "Thank you, please make your selection!" <<endl;
   cout << "Coke              -  " << 0 << endl;
   cout << "Sprite            -  " << 1 << endl;
   cout << "Dr. Pepper        -  " << 2 << endl;
   cin >> selection;
   cout << endl;

   while ( selection < 0 && selection > 3 )
   {

      cout << "Please select a choice between 0 and 2" << endl;
      cout << "Thank you, please make your selection!" <<endl;
      cout << "Coke              -  " << 0 << endl;
      cout << "Sprite            -  " << 1 << endl;
      cout << "Dr. Pepper        -  " << 2 << endl;
      cin >> selection;
      cout << endl;
   }

   switch (selection)
   {
   case 0:
      cout << "Thank you and enjoy your Coke!" <<endl;
      break;
   case 1:
      cout << "Thank you and enjoy your Sprite!" <<endl;
      break;
   case 2:
      cout << "Thank you and enjoy your Dr.Pepper!" <<endl;
      break;
   }
   return type;
}

int main()
{

   cout << "Welcome to the Coke Machine" << endl;
   int question = 0;
   soda sodas[3];
   int count = 0;
   do
   {
      double amountDeposited = insertMoney();
      sodas[count] = displayMenu();
      double changeDue=giveChange(amountDeposited);
      ++sodasPurchased;
      ofstream outfile;  
      outfile.open("Receipt.txt"); 
      outfile <<  amountDeposited << sodas[count] << changeDue;
      outfile.close(); 
      cout << "Would you like to purchase another soda? (1=yes 2=no)" << endl;
      cin >> question;
     count++;
   } while (question == 1 && count < 3);
  
   return 0;
}


the text file only prints out the last transaction (money inserted, and change), how can i make it so that it prints out more than one transaction?
bump
Topic archived. No new replies allowed.