Nov 20, 2009 at 1:03am UTC
I have this program with a vector group of class objects. It does not seem to want to output for some reason so here are the 2 pages of my code that the problem lies in:
application.h
#include<fstream>
#include<iostream>
#include<string>
#include<cstdlib>
#include<vector>
#include"account.h"
#include"accountInfo.h"
using namespace std;
using namespace account_info;
using namespace exception_handling;
using account_class::account;
#ifndef _APPLICATION_CLASS_
#define _APPLICATION_CLASS_
namespace application_class{
class application{
private:
vector<account> accounts;
string first;
string last;
string accountCode;
long double balance;
public:
void accountInfo(istream& s, vector<account>& t){
try{
int count = 1;
while(s.peek()==EOF){
accountCode = get_info(s, 10);
first = get_info(s, 15);
last = get_info(s, 25);
s >> balance;
if(s.peek()!='\n'){
throw exception_three(count);
}
s.ignore();
t.push_back(account(accountCode, first, last, balance));
++count;
}
}catch(exception_three& three){ cout<< "Error 2203: "; three.errorStart();}
}
void outputReport(ostream& report){
for(vector<account>::iterator i=accounts.begin(); i!=accounts.end(); ++i){
cout << *i <<'\n';
}
}
void run(){
try{
ifstream accountRead("account.dat");
if(!accountRead){
throw exception_one();
}
ofstream report("report.txt");
if(!report){
throw exception_two();
}
accountInfo(accountRead, accounts);
outputReport(report);
}catch(exception_one& one){cout<<"Error 2201: "<<one.what();
}catch(exception_two& two){cout<<"Error 2202: "<<two.what();
}
}
};
}
#endif _APPLICATION_CLASS_
account.h
#include<string>
#include<iostream>
#include<cstdlib>
#include<vector>
#include"exceptionHandling.h"
using namespace std;
using namespace exception_handling;
#ifndef _ACCOUNT_CLASS_
#define _ACCOUNT_CLASS_
namespace account_class{
class account{
friend ostream & operator<<(ostream &output, const account &outputAccount){
return output << outputAccount.account_code <<", "<< outputAccount.first_name <<", "
<< outputAccount.last_name;
}
private:
string account_code;
string first_name;
string last_name;
double balance;
public:
account(string, string, string, double);
};
}
#endif _ACCOUNT_CLASS_
I am not sure if I did the vector iterator right. I am sure that is where the problem lies, but I am not sure what the problem is?
Last edited on Nov 20, 2009 at 1:08am UTC
Nov 20, 2009 at 2:16pm UTC
Please use [co de][/code] tags
Which function isn't giving the right output?
Nov 20, 2009 at 6:11pm UTC
code doesn't look to be complete. paste the full code.
Nov 21, 2009 at 3:25am UTC
your iterator and overloaded operator look ok.
you may want to direct your output to report instead of cout.
change:
cout << *i <<'\n';
to:
report << *i <<'\n';
man, overloaded operators always have a crazy look in their eyes. you never know what they are going to do.