Hey. My program wont write the input data to file. It seems like the userdata variabels doesnt get updated after input from the user. Can anyone see what i have done wrong? This is what i have done so far:
// userdata.cpp
// class userdata store user information
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include "userdata.h"
using namespace std;
// default userdata konstruktør
userdata::userdata(int userIDvalue, string nameValue,
string genderValue, int yearOfBirthValue,
string occupationValue, string emailValue)
{
setUserID(userIDvalue);
setName(nameValue);
setGender(genderValue);
setYearOfBirth(yearOfBirthValue);
setOccupation(occupationValue);
setEmail(emailValue);
}// END userdata konstruktør
// get brukerID sin verdi
int userdata::getUserID() const
{
return userID;
}// END getUserID
// set verdi til userID
void userdata::setUserID(int userIDvalue)
{
userID = userIDvalue;
}// END setUserID
// get navn sin verdi
string userdata::getName() const
{
return name;
}// END getName
// set verdi til navn
void userdata::setName(string n)
{
int length = n.size();
length = (length < 20 ? length : 19 );
n.copy( name, length);
name[length] = '\0';
}// END setName
// get kjønn sin verdi
string userdata::getGender() const
{
return gender;
}// END getGender
void userdata::setGender(string g)
{
int length = g.size();
length = ( length < 6 ? length : 5 );
g.copy( gender, length);
gender[length] = '\0';
}// END setGender
int userdata::getYearOfBirth() const
{
return yearOfBirth;
}// END getYearOfBirth
void userdata::setYearOfBirth(int y)
{
yearOfBirth = y;
}// END setYearOfBirth
string userdata::getOccupation() const
{
return occupation;
}// END getOccupation
void userdata::setOccupation(string o)
{
int length = o.size();
length = ( length < 20 ? length : 19 );
o.copy( occupation, length);
occupation[length] = '\0';
}// END setOccupation
string userdata::getEmail() const
{
return email;
}// END getEmail
void userdata::setEmail(string e)
{
int length = e.size();
length = ( length < 30 ? length : 29 );
e.copy( email, length);
email[length] = '\0';
}// END setEmail
int enterChoice(); // Menyen brukeren av programmet interakterer med.
void newUser(fstream&);
void updateUser(fstream&);
void deleteUser(fstream&);
void outputLine(ostream&, const userdata & );
void createTextFile(fstream&);
int getUser(const char * const);
enum Choices{NEW = 1, UPDATE, DELETE, PRINT, END};
int main()
{
// Åpner fil for lesing og skriving.
fstream inOutUser("userdata.dat", ios::out);
inOutUser.close();
inOutUser.open("userdata.dat", ios::in | ios::out | ios::binary ); //| ios::binary
// Avslutte programmet hvis filen ikke kan åpnes.
if(!inOutUser)
{
cerr << "File could not be opened." << endl;
exit( 1 );
}// END if
int choice; // Variabel til å lagre brukerens valg.
// Gjør at brukeren får valgmulighetene på skjermen
while((choice = enterChoice()) != END)
{
switch(choice)
{
case NEW:
inOutUser << "TEEEEEEEEEEEEEEEEEEEEEST\n";
newUser(inOutUser);
break;
case UPDATE:
updateUser(inOutUser);
break;
case DELETE:
deleteUser(inOutUser);
break;
case PRINT:
createTextFile(inOutUser);
break;
default:
cerr << "Incorrect choice" << endl;
break;
}// END switch
inOutUser.clear();
}// END while
}// END main
// Gjør at bruker kan velge menyvalg.
int enterChoice()
{
// Viser tilgjengelige valg.
cout << "\nEnter you choice" << endl
<< "1 - Add a new user" << endl
<< "2 - Update an user" << endl
<< "3 - Delete an user" << endl
<< "4 - Store a formatted text file of users" << endl
<< "5 - End program\n?";
int menuChoice;
cin >> menuChoice; // Valgt menyvalg av brukeren
return menuChoice;
}// END enterChoice
// Opprett og sett inn en ny bruker
void newUser(fstream &insertInFile)
{
int userID = getUser("Enter new user ID");
insertInFile.seekg((userID - 1) * sizeof(userdata));
userdata user;
insertInFile.read(reinterpret_cast< char * >(&user),
sizeof(userdata));
// if record does not previously exist
if(user.getUserID() == 0)
{
string name;
string gender;
int yearOfBirth;
string occupation;
string email;
cout << "Enter name:\n?";
cin >> setw(20) >> name;
cout << "Enter gender:\n";
cin >> setw(6) >> gender;
cout << "Enter year of birth:\n";
cin >> yearOfBirth;
cout << "occupation:\n";
cin >> setw(20) >> occupation;
cout << "Email:\n";
cin >> setw(30) >> email;
user.setName(name);
user.setGender(gender);
user.setYearOfBirth(yearOfBirth);
user.setOccupation(occupation);
user.setEmail(email);
user.setUserID(userID);
insertInFile.seekp( (userID - 1) * sizeof(userdata));
insertInFile.write(reinterpret_cast< const char * >(&user),
sizeof(userdata));
cout << "New user added!" << endl;
}// END if
else
{
cerr << "User #" << userID << " already exist." << endl;
}// END else
}// END newUser
void updateUser(fstream &updateFile)
{
int userID = getUser("Enter user ID to update");
updateFile.seekg((userID - 1) * sizeof(userdata));
userdata user;
updateFile.read(reinterpret_cast< char * >(&user),
sizeof(userdata));
if (user.getUserID() != 0)
{
outputLine(cout, user); // Viser brukeren.
cout << "\nEnter gender (MALE or FEMALE)";
string newGender;
cin >> newGender;
user.setGender(newGender);
outputLine(cout, user); // Viser oppdatert bruker.
updateFile.seekp((userID - 1) * sizeof(userdata));
updateFile.write(reinterpret_cast<const char *>(&user),
sizeof(userdata));
}// END if
else
{
cerr << "User #" << userID<< " does not exist." << endl;
}// END else
}// END updateUser
void deleteUser(fstream &deleteFromFile)
{
int userID = getUser("Enter user ID to delete");
deleteFromFile.seekg((userID - 1) * sizeof(userdata));
userdata user;
deleteFromFile.read(reinterpret_cast<char *>(&user),
sizeof(userdata));
if(user.getUserID() != 0)
{
userdata blankUser;
deleteFromFile.seekp((userID - 1)*sizeof(userdata));
deleteFromFile.write(reinterpret_cast<const char *>(&blankUser),
sizeof(userdata));
cout << "User #" << userID << " deleted!\n";
}// END if
else
{
cerr << "User #" << userID << " is empty.\n";
}// END else
}// END deleteUser
// Lager en formatert tekstfil for printing
void createTextFile(fstream &readFromFile)
{
ofstream outPrintFile( "userdata.txt", ios::out);
if( !outPrintFile)
{
cerr << "File could not be created." << endl;
exit(1);
}// END if
outPrintFile << left << setw(10) << "UserID" << setw(21)
<< "Name" << setw(7) << "Gender" << setw(5) << "Year of birth"
<< setw(21) << "Occupation" << setw(31) << "Email" << endl;
readFromFile.seekg(0);
userdata user;
readFromFile.read( reinterpret_cast< char * >(&user),
sizeof(userdata));
while(!readFromFile.eof())
{
if(user.getUserID() != 0)
outputLine(outPrintFile, user);
readFromFile.read( reinterpret_cast< char * >(&user),
sizeof(userdata));
}// END while
}// END createTextFile
int getUser(const char * const prompt)
{
int userID;
do
{
cout << prompt << "(1-10): ";
cin >> userID;
}while ( userID < 1 || userID > 10);
return userID;
}// END getUser
// Viser en enkelt bruker.
void outputLine( ostream &output, const userdata &record)
{
output << left << setw(10) << record.getUserID()
<< setw(21) << record.getName()
<< setw(10) << record.getGender()
<< setw(10) << record.getYearOfBirth()
<< setw(21) << record.getOccupation()
<< setw(31) << setprecision(2) << fixed << showpoint << record.getEmail() << endl;
}// END outputLine
// userdata.h
#ifndef USERDATA_H
#define USERDATA_H
#include <string>
using namespace std;
class userdata
{
public:
// Standard konstruktør
userdata(int = 0, string = "", string = "", int = 0, string = "", string = "");
// Accessor funksjoner for BrukerID
void setUserID(int);
int getUserID() const;
// Accessor funksjoner for Navn
void setName(string);
string getName() const;
// Accessor funksjoner for Kjønn
void setGender(string);
string getGender() const;
// Accessor funksjoner for Fødselsår
void setYearOfBirth(int);
int getYearOfBirth() const;
// Accessor funksjoner for Yrke
void setOccupation(string);
string getOccupation() const;
// Accessor funksjoner for Email
void setEmail(string);
string getEmail() const;
private:
int userID;
char name[20];
char gender[6];
int yearOfBirth;
char occupation[20];
char email[30];
}; // END class userdata
#endif