Homework help - debugging needed

Hello,

I can't seem to figure out what's wrong with my code. I wrote a program for a vending machine that requires you to pull info from a product file and a machine file, both of which contain info on the products, etc.

Every time I compile and run, the application opens but says it has failed to open my two input files. I've looked through it a million times, and I'm not sure where I'm going wrong here.

Would someone look through my code and tell me why my two input files are failing to open? (It must be because they haven't been created, but how to I go about creating and opening files in this one program??)

All help is greatly appreciated!

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <iostream>
#include <cstring>
#include<fstream>
#include <string>
#include<windows.h>

using namespace std;

const char MACHINE_FILE[] = "machine.txt";                                      //names of the input and output files                                          
const char PRODUCT_FILE[] = "product.txt";
const char LOGBOOK_FILE[] = "logfile.txt";
const int MACHINE_INFO_LINES = 5;
const int MAX_NUM_OF_PRODUCTS = 20;

ifstream machine, product;
ofstream logfile;
string vm[MACHINE_INFO_LINES], product_id[MAX_NUM_OF_PRODUCTS], product_name[MAX_NUM_OF_PRODUCTS];
int initial_inventory[MAX_NUM_OF_PRODUCTS]={0,0,0,0,0,0,00,0,0,0,0,0,0,0,0,0,0,0,0,0}, low_inventory_threshold[MAX_NUM_OF_PRODUCTS]={0,0,0,0,0,0,00,0,0,0,0,0,0,0,0,0,0,0,0,0};
double product_cost[MAX_NUM_OF_PRODUCTS]={0,0,0,0,0,0,00,0,0,0,0,0,0,0,0,0,0,0,0,0};

int end_the_program()                                                           //function to end the program
{
    system("PAUSE");
    return EXIT_SUCCESS;
}

bool next()                                                                     //funtion to ask user to continue or not
{
 char answer;
 cout<<"Do you want to continue (Y/N)?  ";
 cin>>answer;
 return (answer=='Y' || answer == 'y');
}

bool keep_shopping()                                                            //function to ask user to continue of not
{
  char answer;
  cout<<"Do you want to purchase more items? [Y/N]";
  cin>>answer;
 return (answer=='Y' || answer == 'y');    
}

double get_more_money(double difference)                                        //function to get more money if the product's cost is higher than the originally entered money
{
   double money;
   cout<<"You need to $"<<difference<<" more to purchase the item."<<endl;
   cout<<"\n Enter the additional amount. $";
   cin>>money;
   logfile<<"Deposit additional of money : $"<<money<<endl;                     //logfile line for deposit of additional money
  return money;                      
}

void evaluate_choice(double& money, int choice)                                 //evaluate and check for the product price and the entered price
{
   int more_money_choice, check=0;
   double difference;
   while((money < product_cost[choice-1]) && (check==0))
   {
     cout<<"Insufficient funds."<<endl;
     logfile<<"Insufficient funds."<<endl;                                      //logfile line for warning of insufficient funds
     cout<<"Do you want to insert  more money or get your change?"<<endl;
     cout<<"Press 1 to insert more money or Press 2 to get your change back."<<endl;//giving the user option to either quit of add more money
     cin>>more_money_choice;
     if (more_money_choice !=1)
     {
       check=1;
       end_the_program();
     }
     else
     {
        difference = product_cost[choice-1] - money;
        money = money + get_more_money(difference);                             //adding more money
     }
   }
}

double get_money()                                                              //get money from the user
{
   double money;
   cout<<"\n Enter the amount. $";
   cin>>money;
   logfile<<"Deposit of money : $"<<money<<endl;                                //logfile line for deposit of the money
  return money;
}

int show_menu_and_get_choice ()
{
    int choice, check, temp; 
    cout<<"Select the product you want to buy."<<endl;
    do
    {
      check=1;
      for(int i=0; i<MAX_NUM_OF_PRODUCTS; i++)
       {
        if ((initial_inventory[i]<low_inventory_threshold[i]) && (initial_inventory[i]!=0))
           logfile<<"The inventory of "<<product_name[i]<<" (ID # "<<product_id[i]<<"), of "<<vm[0]<<" (ID# "<<vm[1]<<") is below the low inventory threshold."<<endl;
        while(initial_inventory[i]==0) //prevent item out of stock to get displayed
        {
          logfile<<"The product "<<product_name[i]<<" (ID # "<<product_id[i]<<"), of "<<vm[0]<<" (ID# "<<vm[1]<<") is out of stock."<<endl;
          i++;
        }
        cout<<"Enter "<<product_id[i]<<" for "<<product_name[i]<<" priced $"<<product_cost[i]<<"."<<endl; //give the product list
       }
      cin>>choice;
      logfile<<"Item selected "<<product_name[choice-1]<<"."<<endl;             //logfile line to recoed the selected item
      if (initial_inventory[choice-1]==0)
       {
          check=0;
          cout<<"Item not in the menu."<<endl;
          cout<<"Do you want to exit or buy something else?"<<endl;
          cout<<"Press 1 to exit. Press 0 to buy something else."<<endl;
          cin>>check;
          logfile<<"Invalid choice. Item not in the menu"<<endl;                //logfile line for invalid choice
       }
    }while(check!=1);
  return choice;
}

void shopping(double& balance)
{
    double money=0.0;
    int choice;
    do
    {
      money = balance + get_money();
      choice = show_menu_and_get_choice ();
      evaluate_choice(money, choice);
      if ((money>=product_cost[choice-1]) && (initial_inventory[choice-1]!=0))
      {
      cout<<"Vending...";
      cout<<vm[3];
      Sleep(2000);
      logfile<<"Dispensing "<<product_name[choice-1]<<"."<<endl;                //logfile line to recoed dispensing of an item
      balance = money - product_cost[choice-1];
      initial_inventory[choice-1]=initial_inventory[choice-1] - 1;
      cout<<"You have a balance of $"<<balance<<endl;
      logfile<<"Remaining balance: $"<<balance<<endl;                           //logfile line to show the remaining balance
      }
    }while (keep_shopping());
     
}

void get_product_data()                                                         //fucntion to get the product data from an input file
{
    char temp;
    for (int i = 0; (i<MAX_NUM_OF_PRODUCTS) && (!product.eof()); i++)
    {
    product>>initial_inventory[i];
    product>>temp;
    getline(product, product_id[i], ':');
    getline(product, product_name[i], ':');
    product>>product_cost[i];
    product>>temp;
    product>>low_inventory_threshold[i];
    }    
}

void start ()
{
    cout<< vm[0]<<endl;
    logfile<<vm[0]<<" -Business log !"<<endl;                                   //logfile line at the start of the program
}

void get_machine_data()                                                         //function to get vending machine info
{
    for (int i=0; i<MACHINE_INFO_LINES; i++)
    getline( machine, vm[i]);     
}

void open_files(ifstream& machine, ifstream& product, ofstream& logfile)        //function to open files
{
   machine.open( MACHINE_FILE );
   if ( machine.fail() )
   {
       cout << "\nFailed to open " << MACHINE_FILE << " \n";     
       end_the_program();
   }
   product.open(PRODUCT_FILE );
   if ( product.fail() )
   {
       cout << "\nFailed to open " << PRODUCT_FILE << " \n";        
       end_the_program();
   }
   logfile.open( LOGBOOK_FILE);
   if ( logfile.fail() )
   {
       cout << "\nFailed to open " << LOGBOOK_FILE << " \n";          
       end_the_program();
   }          
}

void close_files()                                                              //closing the files at the end of the program
{
  machine.close();
  product.close();
  logfile.close();   
}

int main()
{
   double balance = 0.0;
   open_files( machine, product, logfile ); 
   get_machine_data();
   start();
   get_product_data();
   shopping(balance);
   if (balance!=0.0)
       cout<<"Please take your change $"<<balance<<"."<<endl;
   logfile<<"Change given: $"<<balance<<"."<<endl;
   cout<<vm[4]<<endl;
   close_files();
 end_the_program();
}
I think ifstream assumes the file is already created, try using fstream instead (which has both read and write capabilities).
Ok, thank you! So basically replace all ifstreams with fstream?
Try it out, your computer won't blow up.
I just did, and that still has the files failing to open.
It seems ofstream creates a file, try using std::ios:: flags, gotta go, my boat arrived...
You need to create the input files yourself, before attempting to run the program. Use ifstream for input files, ofstream for output files.
Make sure the files are placed in the same location as the executable program.
Last edited on
Topic archived. No new replies allowed.