I am having a hard time understanding what the problem with my code is. The problem that occurs is coming from the I/O I believe. I am creating a bank handling program and I am storing the accounts in a <map> and then I am storing the accounts in a txt file.
The problem I have is when ever I am closing the application and restarting it and want to read the txt file I am getting nothing or I am getting really odd results. In the application it can be that it does not read anything or it could be that I get
Acc nbr: 0
Name: -47000300
Type: 12333993
balance: Å
it doesnt make sense. But in my txt file its perfectly normal. Sometimes when Ive been trying to play around with the code the txt file changes the text to kanji signs.
I've created an own implemented class that handles the account for example, deposition, overloading constructor of creating an account, withdraw, returning various information. The attributes are all public.
void write_file(){ //Writes to a file
ofstream outFile;
outFile.open("list.txt", ios::out | ios::app);
if(outFile.is_open()){
for(it = mymap.begin(); it != mymap.end(); it++){
outFile << it->second.getName()<<" " << it->second.getPnbr()<<" "
<< it->second.getBalance()<<" " << it->second.getAccountType() <<" "
<< it->second.getAccountNbr();
}
outFile.close();
}
else{
cout << "Could not open file" << endl;
}
}
I am going to post out the print function that is only internal for the map.
1 2 3 4 5 6 7 8 9 10 11
void print(){ //Prints out all information that is stored in the map
for(it = mymap.begin(); it != mymap.end(); it++){
if(mymap.size()==0){
cout << "There is no accounts stored" <<endl;
}
else{
cout<< it->second.returnInformation()<< endl;
}
}
}
How would your output file look after you write two entries in it? No need to actually run program, just compose file manually looking at what your program does.
Show yours returnInformation function.
And make a minimal case reproducing your problem: a one compiling file containing code just enough to reproduce the problem.