So what I've got here is a program that reads from a file and assigns the category and number associated to an object array. I then have a menu where one can choose to read from a separate file and, again, assign those values to an object array. Then the program calculates the number from the first array with the second array and writes to an output file. Everything works fine except that the file created is empty when I open it.
so here's the snippet of code from that specific menu option that is not working out for me (and below that will be the whole app program.
How come nothing is being written to the file?
I've already tried to put a single statement outside the for loop to write a "test" to file and it worked...but I need the for loop for comparison
case 5:{
ifstream in_ratesFile;
inFile.open("Rates.txt");
if(!in_ratesFile){ //test if file there
cout << "Error opening input file: Rates" << endl;
exit (99);
}
cout<<"Tax rates fle opened"<<endl;
cout<<"Reading from file..."<<endl;
int index =0;
in_ratesFile >> ctgry;
while(!inFile.eof()) {
ratesArray[index].setRatectgry(ctgry); // assign ctgry value to [index] object private member
in_ratesFile>>rate;
ratesArray[index].setRate(rate);
in_ratesFile>>ctgry ;
index++;
}
inFile.close();
////////////////////////////////////////////////////////
ofstream out_ratesFile;
out_ratesFile.open("FoodTaxes.txt");
if(!out_ratesFile){ //test if file there
cout << "Error creating output file" << endl;
exit (99);
}
cout<<"Output file created"<<endl;
cout<<"Writing to output file..."<<endl;
double tax =0;
for( int i=0; i<arrayNum; i++){
for (int x=0; x<4; x++){
if ( itemArray[i].getCategory() == ratesArray[x].getRatectgry() ){
tax= itemArray[i].getPounds() * ratesArray[x].getRate();
out_ratesFile<<itemArray[i].getName()<<" ";
out_ratesFile<<tax<<endl; //the " " and endl will hopefully format file so it's readible later
}
}
}
cout<<"Finished writing..."<<endl;
out_ratesFile.close();
break;
}
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include "food.h"
#include "tax.h"
using namespace std;
const int ARRAY_SIZE=20;
//////////////////////////PROTOTYPES//////////////////////
//Display Menu
void displayMenu();
//User input validation on menu choice:
void validateInput(int&);
/////////////////////////////////MAIN///////////////////////////
int main(){
Item itemArray[ARRAY_SIZE]; //initializes array of 20 Item objects.
Tax ratesArray[4]; // declare Tax object in .h file!
int choice=0; //used later to get user's choice on menu
int count = 0;
int arrayNum=0; //used to count up to last array element initialized
string ctgry;
double rate; //used to read tax rates from rates file
string userCtgry;
string nm;
double lbs;
double highest; //keeps track of highest lb. item in inventory
double lowest; //keeps track of lowest lb. item in inventory
int temp; //to keep track which # is highest
double total=0;
double average;
ifstream inFile; //initializes inFile object
inFile.open("FoodInventory.txt"); //opens,associates FoodInventory.txt with inFile
if(!inFile){ //test if file there
cout << "Error opening input file: Food Inventory" << endl;
exit (99);
}
inFile >> nm;
while(!inFile.eof()) {
itemArray[count].setName(nm); // assign nm value to [i]object private member
inFile>>ctgry ;
itemArray[count].setCategory(ctgry);
inFile>>lbs ;
itemArray[count].setPounds(lbs);
inFile>>nm ;
count++;
arrayNum++;
}
inFile.close();
//*******MENU LOOP******//
do{
displayMenu();
cout<< "Please enter your choice:"<<endl;
cin>>choice;
cin.ignore();
cout<<endl;
validateInput(choice);
switch (choice){
case 1: {
cout<<" Please specify which category to list: "<<endl;
cin>>userCtgry;
cin.ignore();
cout<<endl;
for( int i=0; i<arrayNum; i++){
if ( userCtgry == itemArray[i].getCategory() ){
cout<<setw(20)<<itemArray[i].getName();
cout<<setw(20)<<itemArray[i].getCategory();
cout<<setw(20)<<itemArray[i].getPounds();
cout<<endl;
}
}
break;
}
case 2:{
highest = itemArray[0].getPounds();
for ( int count=0; count< arrayNum; count++){
if ( itemArray[count].getPounds() > highest){
highest = itemArray[count].getPounds();
temp= count;
//count only assigned to temp if new highest value detected, keeps track of which object#
// has lowest lbs. in inventory
}
}
cout<< itemArray[temp].getName()<< " has the largest inventory."<<endl;
break;
}
case 3:{
lowest = itemArray[0].getPounds();
for ( int count=0; count< arrayNum; count++){
if ( itemArray[count].getPounds() < lowest){
lowest = itemArray[count].getPounds();
temp= count;
}
}
cout<< itemArray[temp].getName()<< " has the lowest inventory."<<endl;
break;
}
case 4:{
for ( int count=0; count< arrayNum; count++){
total = itemArray[count].getPounds() + total;
}
cout <<"The total amount of lbs. in inventory: "<< total<<endl;
average = total/arrayNum;
cout <<"The average amount of lbs. in inventory: "<< average<<endl;
cout<<endl;
cout<<endl;
break;
}
case 5:{
ifstream in_ratesFile; //declares in_ratesFile object
inFile.open("Rates.txt");
if(!in_ratesFile){ //test if file there
cout << "Error opening input file: Rates" << endl;
exit (99);
}
cout<<"Tax rates fle opened"<<endl;
cout<<"Reading from file..."<<endl;
int index =0;
in_ratesFile >> ctgry;
while(!inFile.eof()) {
ratesArray[index].setRatectgry(ctgry); // assign ctgry value to [index] object private member
in_ratesFile>>rate;
ratesArray[index].setRate(rate);
in_ratesFile>>ctgry ;
index++;
}
inFile.close();
////////////////////////////////////////////////////////
ofstream out_ratesFile;
out_ratesFile.open("FoodTaxes.txt");
if(!out_ratesFile){ //test if file there
cout << "Error creating output file" << endl;
exit (99);
}
cout<<"Output file created"<<endl;
cout<<"Writing to output file..."<<endl;
double tax =0;
for( int i=0; i<arrayNum; i++){
for (int x=0; x<4; x++){
if ( itemArray[i].getCategory() == ratesArray[x].getRatectgry() ){
tax= itemArray[i].getPounds() * ratesArray[x].getRate();
out_ratesFile<<itemArray[i].getName()<<" ";
out_ratesFile<<tax<<endl; //the " " and endl will hopefully format file so it's readible later
}
}
}
cout<<"Finished writing..."<<endl;
out_ratesFile.close();
break;
}
case 6:{
cout<<"Program now terminating..."<<endl<<endl;
break;
}
} // end switch
}while (choice !=6); //end do while
return 0;
} //end main
///////////////////////////Definitions////////////////////////
void displayMenu(){
cout<<"Make a selection from the list below"<<endl;
cout<<"1. List all items of a particular category" <<endl;
cout<<"2. Find which item has the largest inventory"<<endl;
cout<<"3. Find which item has the least inventory "<<endl;
cout<<"4. Display the total and average weight"<<endl;
cout<<"5. Get tax rates"<<endl;
cout<<"6. Exit the program"<<endl;
}
//******************************************************************************
void validateInput(int &input){
if (input<1 || input>6){
do{
cout<<"You have entered invalid data. Please enter a choice from 1 to 5 :"<<endl;
cin>>input;
cin.ignore();
cout<<endl;
}while (input<1 || input>5);
}
}
MOD EDIT: When posting code...USE CODE TAGS!!!
This post has been edited by JackOfAllTrades: 25 October 2010 - 09:17 AM

New Topic/Question
Reply




MultiQuote





|