Help with my C++ Code

0 Noel Kibai · June 15, 2015
Hi All;

I need some help here.

I have to create a C++ application which will read a file of daily payments, calculate the total as well as the average 
payment, display the results to the screen and write the results to a file.   The input and output file names 
should be provided as command line arguments.

Here are my codes:

(a) main.cpp

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <stdexcept>


using namespace std;

string double2string(double);
double string2double(string);

string double2string(double d){
    ostringstream outstr; 
    outstr << setprecision(2) << fixed << setw(10) << d; 
    return outstr.str(); 
};


double string2double(string s){ 
    istringstream instr(s); 
    double n; 
    instr >> n; 
    return n; 
}

int main(int argc, char* argv[]){

fstream dfile;
fstream ofile;

dfile.open(argv[1]);
ofile.open(argv[2]);


int len;
int n = 0;
int counter = 0;
double plus = 0;
string s;
    

    fstream::pos_type flen;
    dfile.seekg(0, ios::end);
    flen = dfile.tellg();
while(n<=flen){
dfile.seekg(n, ios::beg);
getline(dfile, s);
len = s.size() + 2;
n+=len;
string expense = s.substr(40,10);
double payment;
payment = string2double(expense);
plus = plus + payment;
counter++;
}

(b) exception.cpp

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <fstream>
#include "exception.h"

using namespace std;

Argc_error::Argc_error(string reason):logic_error(reason){

bool more = true;
while(more){
try{
if(argc != 2){

throw Argc_error ("Usage: aap_name data_file_name output_file_name");

}
status = false;
}

catch (Argc_error& e){
cout << e.what() <<"\n";
throw;

}

}
}

(c) exception.h

#ifndef EXCEPTION_H
#define EXCEPTION_H

#include <string>

using namespace std;

class Argc_error : public logic_error{
public:
Argc_error (string reason);
};
class Readfile_error : public logic_error{
public:
Readfile_error (string reason);
};
class Digit_error : public logic_error{
public:
Digit_error (string reason);
};
class Writefile_error : public logic_error{
public:
Writefile_error (string reason);
};

#endif


Post a Reply

Replies

Oldest  Newest  Rating
+1 Devon Horton · June 18, 2015
What exactly is the question/issue here? 
  • 1

C++

106,944 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator