read to file creates empty .txt

read to file creates empty .txt

Page 1 of 1

6 Replies - 853 Views - Last Post: 25 October 2010 - 10:24 AM Rate Topic: -----

#1 titova87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-September 10

read to file creates empty .txt

Posted 25 October 2010 - 09:05 AM

Hello

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!!!

:code:

This post has been edited by JackOfAllTrades: 25 October 2010 - 09:17 AM


Is This A Good Question/Topic? 0
  • +

Replies To: read to file creates empty .txt

#2 titova87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-September 10

Re: read to file creates empty .txt

Posted 25 October 2010 - 09:16 AM

p.s. I think the problem is that I am not comparing the two strings correctly (item category and rate category)
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: read to file creates empty .txt

Posted 25 October 2010 - 09:24 AM

So, why not check your hypothesis? Print out the value of each to the screen before you compare them.
Was This Post Helpful? 0
  • +
  • -

#4 titova87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-September 10

Re: read to file creates empty .txt

Posted 25 October 2010 - 09:29 AM

View PostJackOfAllTrades, on 25 October 2010 - 08:24 AM, said:

So, why not check your hypothesis? Print out the value of each to the screen before you compare them.



I've done that- I found the right code to be able to compare two strings together so that now works- but I've figured out that there is a yet another problem:

in_ratesFile >> ctgry;
			cout<<ctgry; //=              //This is not reading from file and then assigning it to the ratesArray!
			while(!inFile.eof()) {
				ratesArray[index].setRatectgry(ctgry);                            // assign ctgry value to [index] object private member
				in_ratesFile>>rate;
				cout<<rate;  //=
				ratesArray[index].setRate(rate);
				in_ratesFile>>ctgry ;
				cout<<ctgry ;   //=
				index++;
			}
			inFile.close();


so the second statement cout<<ctgry will print out and actual category, but the other two cout statements in the while loop never print.

MOD EDIT: Again, POST YOUR CODE IN CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 25 October 2010 - 09:37 AM

Was This Post Helpful? 0
  • +
  • -

#5 titova87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-September 10

Re: read to file creates empty .txt

Posted 25 October 2010 - 09:51 AM

Alright so now I've got the file reading and writing fixed , but now I'm back to those strings.

How do I compare two strings and see if they equal one another?

It seems that the code that I'm using is not working

string item; //=
string rate; //=
for( int i=0; i<arrayNum; i++){
for (int x=0; x<4; x++){
item = itemArray[i].getCategory();
rate = ratesArray[x].getRatectgry();

std::string item,rate; //compare two strings
if ( item == rate ){
tax= itemArray[i].getPounds() * ratesArray[x].getRate();
cout<<"Food Item";
cout<<itemArray[i].getName()<<" ";


cout<<tax<<" in rates to import."<<endl;
//tax= itemArray[i].getPounds() * ratesArray[x].getRate();
//out_ratesFile<<itemArray[i].getName()<<" ";
//out_ratesFile<<tax<<endl;

}
}
}

View Posttitova87, on 25 October 2010 - 08:29 AM, said:

View PostJackOfAllTrades, on 25 October 2010 - 08:24 AM, said:

So, why not check your hypothesis? Print out the value of each to the screen before you compare them.



I've done that- I found the right code to be able to compare two strings together so that now works- but I've figured out that there is a yet another problem:

in_ratesFile >> ctgry;
			cout<<ctgry; //=              //This is not reading from file and then assigning it to the ratesArray!
			while(!inFile.eof()) {
				ratesArray[index].setRatectgry(ctgry);                            // assign ctgry value to [index] object private member
				in_ratesFile>>rate;
				cout<<rate;  //=
				ratesArray[index].setRate(rate);
				in_ratesFile>>ctgry ;
				cout<<ctgry ;   //=
				index++;
			}
			inFile.close();


so the second statement cout<<ctgry will print out and actual category, but the other two cout statements in the while loop never print.

MOD EDIT: Again, POST YOUR CODE IN CODE TAGS!!!

:code:

Was This Post Helpful? 0
  • +
  • -

#6 titova87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 20-September 10

Re: read to file creates empty .txt

Posted 25 October 2010 - 10:10 AM

This code always evaluates to true for the if statement- how can I fix this?
Thanks!

		 double tax =0;

			 string item; //=
			 string rate;  //=
			 for( int i=0; i<arrayNum; i++){
				  for (int x=0; x<4; x++){
					  item = itemArray[i].getCategory();
					  cout<<item<<endl;
					  rate = ratesArray[x].getRatectgry();
					  cout<<rate<<endl;

					  std::string item,rate;    //compare two strings
					  if ( item == rate ){
						tax= itemArray[i].getPounds() * ratesArray[x].getRate();         
						cout<<"Food Item ";													
						cout<<itemArray[i].getName()<<" ";									
						cout<<"requires ";													
						cout<<tax<<" in rates to import."<<endl;							

						 
						 
					  }
				 }
			 }


MOD EDIT: Added code tags...AGAIN!!!
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: read to file creates empty .txt

Posted 25 October 2010 - 10:24 AM

Topics merged...please do not create duplicate topics.

And how many times must you be told to USE CODE TAGS?!?!?!?

:code:

std::string item,rate;    //compare two strings
		  if ( item == rate ){


So...what are the values contained in item and rate when you try to compare them here?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1