Help with Virtual Store Program

Hi guys. So I needed to create a virtual store program using file input/output, functions, and vectors. I have the program complete, but there is just few questions I had. First one is: Towards the end of the program it asks you to enter in money. If you don't enter enough, the program will ask you to enter more. If you enter in too much, it will give you change.

Can someone tell me how to make a variable that adds to itself every time you cin>>? I want the program to loop infinitely when asking for you to enter money, if you dont enter enough.

Also, could someone possibly show me how to add quantities to the items and how to remove them from inventory when there is no more?

All help appreciated, thanks.

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include<iostream>
#include<string>
#include<vector>
#include<fstream>

using namespace std;

void readIn(vector<string>& inventory);
void readOut(vector<string>& inventory);
void yourPurchases(vector<string>& purchases);

char checkout;
int change;
int payment;
int payment2;
int payment3;
int payment4;
int total = 0;
int steakCount = 0;
int beansCount = 0;
int riceCount = 0;
int lettuceCount = 0;
int cornCount = 0;

int main()
{
  int userInput;
  cout<<"Welcome to 99cent.com!"<<endl;
  cout<<"Here is what we have in store:"<<endl;
  vector<string> inventory;
  vector<string> purchases; 
  readIn(inventory);
  readOut(inventory);
  cout<<"Steak-$10.00, Beans-$5.00, Rice-$2.00, Lettuce-$4.00, Corn-$3.00"<<endl;
  do
  {
     
    cout<<"What would you like to purchase?"<<endl;
    cout<<"Enter 1 for steak, 2 for beans, 3 for rice, 4 for lettuce, or 5 for corn"<<endl;
    cin>>userInput;
    
    if(userInput==1)
    {
     purchases.push_back("Steak");
     steakCount++;
     total = total + 10; 
    }
    if(userInput==2)
    {
      purchases.push_back("Beans");
      beansCount++;
      total = total + 5;
    }
    if(userInput==3)
    {
      purchases.push_back("Rice");
      riceCount++;
      total = total + 2;
    }
    if(userInput==4)
    {
      purchases.push_back("Lettuce");
      lettuceCount++;
      total = total + 4;
    }
    if(userInput==5)
    {
      purchases.push_back("Corn");
      cornCount++;
      total = total + 3;
    }



    cout<<"Would you like to continue shopping or checkout?"<<endl;
    cout<<"Enter y/Y to continue, any other key to checkout"<<endl;
    cin>>checkout;

  
  }while(checkout == 'y' || checkout == 'Y');
  
  cout<<"Here is your list of purchases:"<<endl;
  yourPurchases(purchases);
  cout<<"Your total is: "<<total<<endl;
  cout<<"Please enter payment."<<endl;
  cin>>payment;
     
  if(payment < total)
    { 
      cout<<"That is not enough. Please enter more money."<<endl;
      cin>>payment2;
      payment = payment + payment2;
      if(payment < total)
      {
        cout<<"That is still not enough money. Please enter more money."<<endl;
        cin>>payment3;
        payment = payment + payment2 + payment3;
        
        if(payment < total)
        {
          cout<<"Still not enough money. Please enter more."<<endl;
          cin>>payment4;
          payment = payment + payment2 + payment3 + payment4;
        }
      }
    }
  if(payment > total)
    {
      change = payment - total;
      cout<<"Your change is: "<<change<<endl;
    }
  cout<<"Thanks for shopping with 99cent.com. Please come again!"<<endl;
  return 0;
}

void readIn(vector<string>& inventory)
{
  ifstream inf("inventory.txt");
  string word;
  while (inf >> word)
  {
    inventory.push_back( word );
  }
  
}

void readOut(vector<string>& inventory)
{
  for( int i=0; i < inventory.size(); i++)
  {
    cout<<i+1<<":"<<inventory[i]<<endl;
  }

}

void yourPurchases(vector<string>& purchases)
{
  for( int i=0; i < purchases.size(); i++)
  {
    cout<<purchases[i]<<endl;
  }
}
Can someone tell me how to make a variable that adds to itself

I give you a coin. You have a coin in hand.
I give you second coin. Does it meld into the first, or do you now have two coins in hand?

Is the type of coin that I give you at any particular time the same thing as how much money you have got? Perhaps they are two different things? Two variables.

1
2
3
4
5
6
7
sum = 0;
while ( std::cin >> value ) {
  sum += value;
  if ( enough ) break;
  std::cout << "We want more!\n";
}
// How much we got? Be nice or take the money and run? 
Last edited on
Topic archived. No new replies allowed.