So I need a little help on this, What this program is supposed to do is have the user input there checkbook.txt into the program which looks like this

deposit:July 7:-:300
416:July 8:Albertsons:15.85
417:7/9:Checker Auto:19.95
418:7/10:Super Target:47.50
419:Dec 5:Home Depot:47.89

Once the users enters there data into the program it outputs the text but removes the colons and adds spaces so it looks nice. I would like it to look like this.

--------------------------------------------------------------------
 deposit           July 7              -                   $ 300.00
 416               July 8              Albertsons          $ 15.85
 417               7/9                 Checker Auto        $ 19.95
 418               7/10                Super Target        $ 47.50
 419               Dec  5              Home Depot          $ 47.89
--------------------------------------------------------------------
Balance: $168.81

So how do I go about making the items alligned witheachother from reading the txt file?
Also how would I go about getting the balance? Any help would be appreciated thank you. Here is my code this far.

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main(){

    char    filename[1000];
    cout << "Please Enter Your Checkbook Text: ";
    cin.getline(filename, 1000);

    ifstream    input(filename);
    cout << "--------------------------------------------------------------------" << endl;
    if(! input.good()){

        cerr << "Unable to open " << filename << " for reading" << endl;
        exit(1);
    }
    while (! input.eof()){

        char    c;
        input.get(c);

        switch (c){

        case ':' : cout << left << setw(2) << "\t" <<  right << setw(6);
            break;

        default : cout << c;
            break;
        }

    }

    return 0;
}

Edited 4 Years Ago by Sasquadge

Instead of reading the file one character at a time, read an entire line at once and then parse it to break it up into its individual parts.

std::string line;
while( getline(infile,line) )
{
  // now break the line into its individual parts


}

How do I go about breaking the line individually I'm new to I/O and working with Files...just an example would be great and I'll go from there. Thanks for the help.

Another technique is use the fact that the colon separates the fields, using it as a delimiter in the getline. This separates the items for you.

std::string input;
while( getline( infile, input, ':' ) //reads first element of record
{
    //output the first item
    getline( infile, input, ':' ); //read second item
    //output second item
    getline( infile, input, ':' ); //third item
    //output
    getline( infile, input ); //last item, no delimiter, reads till endline
    //output
}

As to displaying the values in nice columns, just use setw( ). Don't use tabs. You may need to set the alignment to the left for some columns.

When I try the getline and infile I get intellisense telling me error no instance.

I took out what I had and inserted the code from above to see what it was going to give me

#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <string>
using namespace std;



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

    char    filename[1000];
    cout << "Please Enter Your Checkbook Text: ";
    cin.getline(filename, 1000);

    ifstream    input(filename);

    if(! input.good()){

        cerr << "Unable to open " << filename << " for reading" << endl;
        exit(1);
    }

    string input;
    while(getline (infile, input, ':')) //reads first element of record
    {
        //output the first item
        getline( infile, input, ':' ); //read second item
        //output second item
        getline( infile, input, ':' ); //third item
        //output
        getline( infile, input ); //last item, no delimiter, reads till endline
        //output
    }


    return 0;
}

You can't just take sample code given here and plug it in directly, you must make sure that names match up. Sample code is meant to demonstrate a usage, not necessarily directly solve your problem.

See getline information here: http://www.cplusplus.com/reference/string/getline/

You are using your file handle where the string name needs to be, and you didn't declare a string to take the input.

Okay so I got the format figured out, how would I go about getting the balance? Thanks for the help so far guys.

code now:

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



int main(){

    char    filename[1000];
    cout << "Please Enter Your Checkbook Text: ";
    cin.getline(filename, 1000);

    ifstream    input(filename);

    if(! input.good()){
            cerr << "Unable to open " << filename << " for reading" << endl;
        exit(1);
    }

    cout << "-------------------------------------------------------------------------------" << endl;
        while (!input.eof()){

        char    deposit[15];
        input.getline(deposit, 15, ':');
        cout << left << setw(15) << deposit;

        char    date[15];
        input.getline(date, 15, ':');
        cout << setw(15) << date;

        char    place[15];
        input.getline(place, 15, ':');
        cout << setw(15) << place;

        char money[15];
        input.getline(money,15);
        cout << right << setw(15) << "$   " << money << right << endl;

    }
    cout << "-------------------------------------------------------------------------------" << endl;
    return 0;
}

Edited 4 Years Ago by Sasquadge

You need to convert the number input into numeric values.

Two ways you could do that.

Read them into numeric variable directly, as in:

ifstream fin;
double money = 0.0;
fin.open( "data.txt" )
//test for success, of course

fin >> money; //input stored in the double

Since the number value is the last thing on the line, you will need to get rid of the remaining newline character.

Alternatively, you could use what you have, and convert the numbers stored as a string to a double value. I'll point you here: http://www.cplusplus.com/reference/cstdlib/strtod/

ummm, you're already doing that in line 38.

Just change the manner in which you do it, or what you do with that input.

I still am not getting anywhere with this add, subtracting the balance...any other help out there for this problem?

Well thats the thing I've tried some stuff and nothing has worked or giving me any out put so my code is looking exactly how it is up there with the display.

Are you reading the money value into a numeric type variable? Once you do that, you can then add deposits to a balance, subtract checks.

This article has been dead for over six months. Start a new discussion instead.