I think I am getting bad data because I have my pointer doing the wrong thing, I just don't know exactly how to fix it. Any help is greatly appreciated!

output:
Number: 4669840
Name:
Value: 3.76441e-039

Number: 4218312
Name:
Value: 6.52879e-039

Number: 2686408
Name:
Value: 6.52879e-039

Number: 128
Name:
Value: 7.55882e+033

Number: 8392488
Name:
Value: 1.12104e-044

Number: 8392360
Name:
Value: 1.17549e-038

Number: 2686392
Name:
Value: 1.12104e-044

    while(!inFile)
    {
        //Same junk output for this
        for(ptr = &company[0]; ptr < &company[SIZE]; ptr++)
        {
            cin >> (*ptr).budget_num;
            cin >> (*ptr).name;
            cin >> (*ptr).budget_value;
        }
    }


    for(int i = 0; i < SIZE; i++)
    {
    cout << "Number: " << company[i].budget_num << endl;
    cout << "Name:   " << company[i].name << endl;
    cout << "Value:  " << company[i].budget_value << endl;
    cout << endl;

    outFile << "Number: " << company[i].budget_num << endl;
    outFile << "Name:   " << company[i].name << endl;
    outFile << "Value:  " << company[i].budget_value << endl;
    outFile << endl;
    }

Recommended Answers

All 5 Replies

It's not a pointer problem, just output problem. Try adding fixed to cout

outFile << "Value: " << fixed << company[i].budget_value << endl;

Thanks for the advice, Ancient Dragon. I added << fixed << to my output and it is still wrong. The if I am only reading in the file to the array with a pointer, then it should come out looking the same since nothing was done to it. I have attached my input and output files for

Here is my code:

#include <iostream> //Provides cout and cin
#include <fstream>  //Provides external file access
#include <cstdlib>  //Provides Exit_Success
#include <iomanip>
#include <cmath>

using namespace std;  //Allows Standard Library items to be used

//**********Class Definitions/Function Prototypes**********
class budget
{
    public:
    int budget_num;
    string name;
    float budget_value;
};

//********End Class Definitions/Funtion Prototypes********

//Declare file streams
ifstream inFile;
ofstream outFile;

int main()
{
    //Declare variables
    int SIZE = 15;
    budget company[SIZE];
    budget *ptr = &company[0];
    //string MyString = "";

    //Open inFile
    inFile.open("ledger.dat");

    while(!inFile)
    {
        cout << "Error opening the inFile." << endl;
        return 1;
    }

    //Open outFile
    outFile.open("Results.txt");

    while(!outFile)
    {
        cout << "Error opening the outFile." << endl;
        return 1;
    }

    while(!inFile)
    {
        //Same junk output for this
        for(ptr = &company[0]; ptr < &company[SIZE]; ptr++)
        {
            cin >> (*ptr).budget_num;
            cin >> (*ptr).name;
            cin >> (*ptr).budget_value;
        }

  //      As this
//        for ( int i = 0; i < SIZE; ++i )
//        {
//            cin >> (company[i].budget_num);
//            cin.ignore();
//            getline(cin, company[i].name);
//            cin >> company[i].budget_value;
//            cin.ignore();
//        }

    }

    //See what is in the array
    for(int i = 0; i < SIZE; i++)
    {
    cout << "Number: " << fixed << company[i].budget_num << endl;
    cout << "Name:   " << fixed << company[i].name << endl;
    cout << "Value:  " << fixed << company[i].budget_value << endl;
    cout << endl;

    outFile << "Number: " << fixed <<company[i].budget_num << endl;
    outFile << "Name:   " << fixed <<company[i].name << endl;
    outFile << "Value:  " << fixed <<company[i].budget_value << endl;
    outFile << endl;
    }

    //Close the files
    inFile.close();
    outFile.close();

    //Exit program
    return EXIT_SUCCESS;
}

Well, crud! I didn't get my files attached before it posted! Anyway, The input file looks like this:

101  Checking account #1      8219.23
102  Checking account #2      3321.20
505  Advertising expense        25.00
510  Auto expense              501.12
515  Bank charges                5.00
520  Books and publications     87.40
525  Interest expense          380.27
530  Legal expense             100.00
535  Miscellaneous expense      27.87
540  Office expense            138.37
545  Postage and shipping       57.45
550  Rent                     1500.00
555  Supplies                  241.80
560  Travel and entertainment 1307.74
565  Utilities                 878.48

You can see from the output above that it doesn't match.

lines 35 and 48: don't use while statements, just simple if will do because all its doing is testing if the file is open, and if not exiting the progrm. while is unnecessary.

line 50: while not necessary here either. If the file was opened correctly then everything within that while statement will get skipped because inFile will not be NULL and your program isn't reading anything from the file.

The code you posted osn't reading the input file at all, just getting data from the keyboard.

Thanks, Ancient Dragon. I figured it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.