Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 13th, 2006, 7:36 PM   #1
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
having problem with data not saving to a file

Hey Everyone,

Please don't hate me, but i'm back, with ALMOST the same problem as that last huge thread, except now instead of trying to read in data, I need help with the program that lets the user input that data and save to the file. I originally did this exercise without classes, but I've redone it:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;

class Employee
{
 
  private: 
    int id;
    char sex;
	float wage;
	int years;    


  public:
    Employee(){dataStore(00000000, 'M', 00.00, 0);};
    Employee(int, char, float, int);
    void dataStore(int , char , float, int);
    int retrieveID() {return id;};
    char retrieveSex() {return sex;};
	double retrieveWage() {return wage;};
    int retrieveYears() {return years;};

};

 Employee::Employee(int in_id, char in_sex, float in_wage, int in_years)
 {
      dataStore(in_id, in_sex, in_wage, in_years);
 }

 void Employee::dataStore(int in_id, char in_sex, float in_wage, int in_years)
 {
      id = in_id;
      sex = in_sex;
      wage = in_wage;
      years = in_years;
 }

int main()                       
{
  const int TOTAL = 3;
  Employee holder;
  
  int i, j, k, id, years;
  float wage;
  char sex;
  Employee holder2;
  Employee Employees[TOTAL]; 
  
  string filename="save.txt";
  ofstream file(filename.c_str());  
  
    for(i=0; i<3; i++)
	{
		cout << "Please enter the following Employee information:" << endl;
		cout << "\nEmployee ID No: ";
		cin >> id;
		cout << "\nEmployee Sex (M or F): ";
		cin >> sex;

		while((sex != 'M') && (sex != 'm') && (sex != 'F') && (sex != 'f'))
		{
			cout << "Must be M or F. Please retry: ";
			cin >> sex;
		}

		cout << "\nEmployee Numeric Wage: ";
		cin >> wage;
		cout << "\nYears with Company: ";
		cin >> years;
		cout << endl;
		
		holder = Employee(id, sex, wage, years);
	    Employees[i] = holder;

	}
  
  for (i = (TOTAL - 1); i >= 0; i--)
  {
	  for(j = 1; j <= i ; j++)
	  {

			if(Employees[j-1].retrieveYears() > Employees[j].retrieveYears() )
			{
   			    holder2 = Employees[j-1];			 						   
				Employees[j-1] = Employees[j];
				Employees[j] = holder2;
			}
		  
	  }
  }


    cout << "You have entered:" <<endl;
	cout <<"Employee ID       " << "Employee Sex         " << "Numeric Wage        "  << "Years with Company" << endl;
	cout << "-----------       " << "------------         " << "------------        " << "------------------" << endl;

	for(k=0; k<3; k++)
	{
		cout << setw(11) << right << Employees[k].retrieveID() << "       "	<< setw(12) 
        << char(toupper(Employees[k].retrieveSex())) << "         " << "$  " << setw(9) << fixed 
        <<	setprecision(2) << right << Employees[k].retrieveWage() << "        " << setw(18) 
        <<	right << Employees[k].retrieveYears() << endl;
	}

  cout << "\nWould you like to write the information to a file? This will overwrite the file." << "Would you like to continue? (y/n) : " ;
	char response;
	cin >> response;

	while((response !='N') && (response!='n') && (response!='Y') && (response!='y'))
	{
		cout << "Must enter Y or N: ";
		cin >> response;
	}

	if( response == 'Y' || response == 'y')
	{
		
		file.open("save.txt");

		if(file.fail())
		{
			cout << "\nThe file was not succesfully saved." << endl;
		}

		else
		{
		    for(k=0; k < TOTAL; k++)
			{
				file << Employees[k].retrieveID() << " " 
                << char(toupper(Employees[k].retrieveSex())) << " " 
                << Employees[k].retrieveWage() << " " 
                << Employees[k].retrieveWage() << endl;
			}

			file.close();
		

		cout << "\nThe file has been written successfully." << endl;
	}
}

	else
	{
		cout << "\nThe file has not been written." << endl;
	}

  system("PAUSE");
  return 0;
}

Something goes bad when I try to save it to the file, it says it doesn't save. If you don't have the file save.txt already made in the directory, when you run the program, it makes it for you, i've noticed. BUT, i'm guessing when it tries to open it for saving, something goes bad. Does anyone notice a problem????
codylee270 is offline   Reply With Quote
Old Apr 13th, 2006, 7:42 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You're opening it after it's opened at the top of main. Bad boy.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 13th, 2006, 7:44 PM   #3
aulė
Newbie
 
aulė's Avatar
 
Join Date: Apr 2006
Location: Connecticut
Posts: 3
Rep Power: 0 aulė is on a distinguished road
Send a message via AIM to aulė
i promise this is the last childish thing i do on this forum I SWEAR. i just had to do it


[quote]retrieveSex() {return sex;}[quote]
aulė is offline   Reply With Quote
Old Apr 13th, 2006, 7:58 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Do it in the damned lounge, it has nothing to do with this topic. Incidentally, read the forum's rules. Animated avatars are not allowed. Would a moderator move or delete that post?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 13th, 2006, 8:31 PM   #5
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
OOPS!... I was trying to insert the class and build the new version from within the old one, and that slipped through the cracks. Seriously though, what would a program of mine be without me missing something stupid?? Once again, fantastic service as always, except for the guy who still giggles when he sees the letters s-e-x in close proximity of each other. Other than that, thank you very very much again Dawei. Aren't you glad I worked a class in? And bubble sort!! I'm glad someone reminded me of it.
codylee270 is offline   Reply With Quote
Old Apr 13th, 2006, 8:39 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Actually, I'm glad to see the class. There's sort of a good reason not to use arrays as you were. If you look at it, you'll see that it amounts to rotating a database or spreadsheet by 90 degrees. When the number of entries increase, you have to up the size of the array. That's like having to redefine your table columns in a DB every time you add a record, while the records remain fixed at what should have been the number of fields. Sort of a minor point, but has some validity.

To be quite honest about it, I was shocked the first time I discovered one couldn't call open on an opened file. I had presumed it would either ignore the action, or rewind the file. That's so long ago, I forgot I forgot.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:34 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC