PLEASE HELP!!!

Can someone please help me figur out why is the program not displaying the contents of the input file in "Option 1" ?

#include <iostream>
#include <iomanip>
#include <cstring>
#include <fstream>
using namespace std;

///Declare struct
struct toyRecord
{
int toyID;
char description[50];
int quantity;
double priceUnit;
};

void viewToys (toyRecord[]);
void newRecord (toyRecord[]);
void purchaseToys (toyRecord[]);
void totalPurchases (toyRecord[]);
void updateInfo (toyRecord[]);

toyRecord available[50];

int main()
{
string filename;
int a = 0;
int i = 0;
int option = 0;

ofstream outfile;
ifstream infile;

cout << "Enter filename: ";
getline(cin, filename);

infile.open(filename.c_str());

if(!infile)
{
cout << "File not found!!!" << endl;
return 0;
}



while (infile.good())
{
infile >> available[a].toyID;
infile >> available[a].description;
infile >> available[a].quantity;
infile >> available[a].priceUnit;

if (infile.eof())
{
break;
}

a++;

}

infile.close();

///Prompt user to enter option
cout << "\nMENU: " << endl;
cout << "1. View available toys" << endl;
cout << "2. Add a new toy record" << endl;
cout << "3. Purchase toys" << endl;
cout << "4. Display overall total of all purchases" << endl;
cout << "5. Update toys information" << endl;
cout << "6. Quit" << endl;

cout << "\nPlease select an option: ";
cin >> option;

///Validate option
while (option < 1 || option > 6)
{
cout << "Invalid option! Try again.\n" << endl;
cout << "MENU: " << endl;
cout << "1. View available toys" << endl;
cout << "2. Add a new toy record" << endl;
cout << "3. Purchase toys" << endl;
cout << "4. Display overall total of all purchases" << endl;
cout << "5. Update toys information" << endl;
cout << "6. Quit" << endl;

cout << "\nPlease select an option: ";
cin >> option;
cout << "\n";
}

if (option == 1)
{
infile.open(filename.c_str());
cout << "\nAVAILABLE TOYS:-" << endl;
for (int i = 0; i < a; i++)
{
cout << "Toy #" << i+1 << endl;

if (infile.good())
{
string availableRecords;
getline(infile, availableRecords);
cout << "Available records: " << availableRecords << endl;
}

cout << "Toy ID: " << available[a].toyID << endl;
cout << "Description: " << available[a].description << endl;
cout << "Quantity available: " << available[a].quantity << endl;
cout << "Price per unit: RM" << available[a].priceUnit << endl;
}


}
/*
else if (option == 2)
{
char cont;
int newID;
string descriptionNew;
int quantityNew;
double priceNew;

outfile.open(filename, ios::app);

while(outfile.good());
{
cout << "Enter "
}

}

else if (option == 4)
{
outfile.open(filename, ios::out);

for (int i = 0; i < )
}

*/
}

*Code is still incomplete, here's the sample input file:

2
1001
Remote
Control
Car
50
35.80
1002
Barbie Doll
75
45.00

The first line in the file should represent the number of records available in the file. The following lines represent the details of each record starting with the toy id, description, quantity, and price per unit.

When the program starts, prompt the user to enter the filename and read all toys’ information to be stored in the array of struct. You need to keep track of the total records available by declaring and using suitable variable as counter. The program should then display a menu that allows the user to perform the following operations:
 View the different toys available in the store.
 Add a new toy record
o For this option, prompt the user to enter all the toys information and add to the array of struct. You do not need to update the file yet at this moment.
 Purchase toys
o The user should be allowed to select a toy from the list and determine the quantity to purchase.
o If there is sufficient quantity available for the purchase
 deduct the purchased quantity from the toy’s record
 Calculate and display the current total amount
o If the quantity is not sufficient, display an error message
o Then prompt the user whether another purchase is needed. If yes, repeat the process again
o If not, print the bill. This option should involve displaying the total due, prompt the user to enter the payment amount, and display the balance need to be paid back to the customer.
 Display overall total of all purchases (you may need to consider additional variables to keep this information)
 Update toy’s information
o Only description, quantity, and price per unit can be updated.
 Quit from the program.
Purchasing a toy should also involve calculating discounts based on the quantity purchased. This should be done automatically when the purchase option is selected after the user has done the selection. Any purchase that exceeds 10 units is entitled for a 10% discount. Remember to update the record’s quantity every time a purchase is made. Use functions to perform each option provided in the menu (except the Quit option). Your program should also consider data validations. Before the program terminates, you
Last edited on
closed account (48T7M4Gy)
While slightly off your question you should use a better control structure like a switch and some functions, including a display menu function. It will have tha advantage of debugging and isolating problems.
Topic archived. No new replies allowed.