Program Compiles but does not read proper file....

This is a discussion on Program Compiles but does not read proper file.... within the C++ Programming forums, part of the General Programming Boards category; I have this code, and it compiles - it asks for the file to look at to get the information ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    16

    Question Program Compiles but does not read proper file....

    I have this code, and it compiles - it asks for the file to look at to get the information from and I type in the file which is account and i also tried account.txt and then it doesn't do anything else. It says press any button to continue and then exits out of everything. I'm not sure whats going on.
    Can anyone help me????? Thanks



    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    struct Entry
    {
    	string date, action;
    	float amount;
    	string descript;
    }Entry;
    
    int main()
    {
    	string name, date, action, descript;
    	float balance, amount;
    
    	ifstream fin;
    	ofstream fout;
    
    	cout << "enter the name of the Checkbook filename: " << endl;
    
    	cin >> name;
    
    	//string update(name);
    
    	//update.append(".update");
    
    	fin.open(name.c_str(), ios::in);
    	
    
    	fin >> balance;
    
    	while (!fin.eof())
    	{
    		fin >> date >> action;
    		//cout << date << action;
    		fout << date << action;
    
    		if (action == "credit")
    		{
    			fin >> amount;
    
    			fout << amount;
    
    			balance = balance + amount;
    
    		}
    
    		else if (action == "debit")
    		{
    			fin >> amount;
    
    			fout << amount;
    
    			balance = balance - amount;
    
    		}
    
    		getline(fin, descript);
    
    		fout << descript;
    	}
    
    	fout << balance;
    
    	fin.close();
    	//fout.close();
    
    }//end main

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    If you are running it from within the compiler that is your problem.
    The working directory for programs ran in the compiler is different than where the actual .cpp file is. Run the exe with the file in the same directory as the exe.

  3. #3
    www.gamingdl.com gamingdl'er's Avatar
    Join Date
    Nov 2005
    Posts
    27
    You can fix the exiting with a while loop:
    Code:
    bool usercontinue (void)
    {
        char DoneCharacter;
        cout <<
           endl <<
              "Continue?" <<
                 " - Press \"n\" and \"Enter\" to stop: ";
                 cin >> DoneCharacter;
                 return (DoneCharacter != 'n'); // true when not "n"
    }
    
    int main()
    {
            while(usercontinue()) {
    	string name, date, action, descript;
    	float balance, amount;
    
    	ifstream fin;
    	ofstream fout;
    
    	cout << "enter the name of the Checkbook filename: " << endl;
    
    	cin >> name;
    
    	//string update(name);
    
    	//update.append(".update");
    
    	fin.open(name.c_str(), ios::in);
    	
    
    	fin >> balance;
    
    	while (!fin.eof())
    	{
    		fin >> date >> action;
    		//cout << date << action;
    		fout << date << action;
    
    		if (action == "credit")
    		{
    			fin >> amount;
    
    			fout << amount;
    
    			balance = balance + amount;
    
    		}
    
    		else if (action == "debit")
    		{
    			fin >> amount;
    
    			fout << amount;
    
    			balance = balance - amount;
    
    		}
    
    		getline(fin, descript);
    
    		fout << descript;
    	}
    
    	fout << balance;
    
    	fin.close();
    	//fout.close();
     }
    
    }//end main
    Visit www.gamingdl.com - A gaming download database
    *Complete and ready for you!

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    try not to use your struct name as the variable itself

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,323
    You never open a file for the output stream fout, you just start writing to it. You need to specify a filename for the output file. Then, if the program is working, it will still say press any button to continue, but there will be an output file with the data you wrote to fout. If you change fout to cout it would output to the screen.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple File Read Program...
    By PowerHouse in forum C Programming
    Replies: 9
    Last Post: 10-07-2008, 11:30 PM
  2. Read data from file - C program ?
    By Pawan Sangal in forum C Programming
    Replies: 2
    Last Post: 08-22-2008, 04:28 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21