Array not reading from File

Iam new to c++, iam writing this program with 3 files called header file, class implementation file and main cpp file.

Iam using array to read from 2 files file food Inventory get the data, and other file name rates has tax rates info, i got all the 4 cases working fine, except case 5 I got struck, I really appropriate if you can look at my code, need your help. This is main CPP file , if you want look at other 2 files let me know

Thanks in advance.




#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include "Food.h"
using namespace std;


void displayMenu();
int getChoice();
double getTaxes(int, double);
const int SIZE=20;
int main()
{
Food arr[SIZE];
string name;
string type;
string selCat;
double taxes,weight;
int qty, i=0,selection,choice,largeSub=0,largest,leastSub=0,least;
ifstream inFile;
inFile.open("FoodInventory.txt");
if(!inFile)
{
cout << "Error opening input file" << endl;
exit(99);
}

inFile.open("Rates.txt");
if(!inFile)
{
cout << "Error opening input file" << endl;
exit(99);
}

inFile >> name >> type >> qty;
while(!inFile.eof())
{
cout << "The input data was: " << endl;
cout << "\tThe name is " << name << endl
<< "\tThe type is " << type << endl
<< "\tThe quantity is " << qty << endl<<endl;
arr[i].setName(name);
arr[i].setCategory(type);
arr[i].setAmount(qty);
i++;
inFile >> name >> type >> qty;
}

int counter=i;
inFile.close();
int sub=0;
for(sub=0; sub<counter; sub++)
{
cout<<"Name="<<arr[sub].getName()
<<" Category="<<arr[sub].getCategory()
<<" Amount="<<arr[sub].getAmount()<<endl;
}

do
{

displayMenu();
choice=getChoice();
switch (choice)
{

case 1:

cout<<"What category do you want: ";
cin>>selCat;
for(sub=0; sub<counter; sub++)
{
if(selCat==arr[sub].getCategory())
{
cout<<"Name="<<arr[sub].getName()
<<" Category="<<arr[sub].getCategory()
<<" Amount="<<arr[sub].getAmount()<<endl;
}
}


break;

case 2:
//int largeSub=0;
largest=arr[0].getAmount();
for(int sub=0; sub<counter; sub++)
{
if(arr[sub].getAmount()>largest)
{
largest=arr[sub].getAmount();
largeSub=sub;
}
}
cout<<"Largest item is Name="<<arr[largeSub].getName()
<<" Category="<<arr[largeSub].getCategory()
<<" Amount="<<arr[largeSub].getAmount()<<endl;

break;
case 3:

//int leastSub=0;
least=arr[0].getAmount();
for(int sub=0; sub<counter; sub++)
{
if(arr[sub].getAmount()<least)
{
least=arr[sub].getAmount();
leastSub=sub;
}
}
cout<<"Least item is Name="<<arr[leastSub].getName()
<<" Category="<<arr[leastSub].getCategory()
<<" Amount="<<arr[leastSub].getAmount()<<endl;

break;

case 4:
double total;
for(int sub=0; sub<counter; sub++)
{
total+=arr[sub].getAmount();
}
cout<<"Total="<<total
<<" Average="<<total/counter
<<endl;
break;

case 5:

double tax_category,myFood,i=0,numInArray,numInArray2;
int food_category,file_error;
//int getTaxes(int,int);

getTaxes(arr, numInArray2);
//if (file_error == 99)

{
break;
}
double tax;
double tax_rate;
int outFile;
//string myFood;
for (int i=0; i<numInArray2; i++)
{
tax_category = arr[i].getTax();
tax_rate = arr[i].getTaxes();
for (int j=0; j<numInArray; j++)
{
food_category = arr[j].getCategory();
if (tax_category == food_category)
{
tax = arr[j].getWeight() * tax_rate;
outFile << fixed << showpoint << setprecision(2);
outFile << arr[j].getName()<< " " << tax << endl;
}
}
}
int outFile.close();


break;

}while (choice != 6);

return 0;

}

void displayMenu()
{

cout << "\n*************************************\n";
cout << "Make a selection from the list below\n";
cout << "*************************************\n";
cout << "1. List all items of a particular category\n";
cout << "2. Find which item has the largest inventory\n";
cout << "3. Find which item has the least inventory\n";
cout << "4. Display the total and average weights\n";
cout << "5. Calculate import taxes\n";
cout << "6. Exit the program:\n";

}

int getChoice()
{

int choice;
cout << "Enter your choice (between 1 and 6): ";
cin >> choice;
cin.ignore();
return choice;
}
Last edited on
Topic archived. No new replies allowed.