Output question

This is a discussion on Output question within the C++ Programming forums, part of the General Programming Boards category; This is just a programming example I am learning from. I can't get this to output to a text file. ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    Output question

    This is just a programming example I am learning from.
    I can't get this to output to a text file.
    Input file:
    1126 Andrew Jackson ILL 5000.00 3245.11

    Code:
    #include<iostream.h>
    #include<fstream.h>
    #include<iomanip.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    const int MaxNameLen = 31;
    const int MaxStCodeLen = 3;
    const int MaxNum = 500;
    
    struct CREDITREC {
         long acctno;   //account number = social security number;
         char custName[MaxNameLen];  //Customer name upto 30 cahracters.
         char stateCode[MaxStCodeLen]; //upper case state code.
         double creditlimit;  //credit limit of customers
         double balance;  //customers current balance due.
         };
    
    
    int LoadArray(CREDITREC crec[], int limit, char filename[]);
    void SortArray(CREDITREC crec[], int numRecs);
    void PrintReport(CREDITREC crec[], int numRecs, char filename[]);
    
    int main(){
         CREDITREC creditRec[MaxNum];  //array of customer credit records.
         int  numRecs;                 //current number of customers in array.
    
         //Obtain the input and report filenames from the user
         char inputFilename[_MAX_PATH];
         char reportFilename[_MAX_PATH];
         cout<<"Enter the name of the credit master file: ";
         cin>>inputFilename;
         cout<<"Enter the name of the output filename: ";
         cin>>reportFilename;
    
         //load the array of credit records
         numRecs = LoadArray (creditRec, MaxNum, inputFilename);
         cout<<"\nArray loaded: "<<numRecs<<"records input\n";
    
         //sort the arrray into a state code order
         cout<<"Sorting the array\n";
         SortArray(creditRec, numRecs);
    
         //Produce the report
         cout<<"Printing the report\n";
         PrintReport(creditRec, numRecs, reportFilename);
         cout<<"Finished\n";
         return 0;
    }
    
    //////////////////////////////////////////////////////////////////////
    //Load Array                                                        //
    //////////////////////////////////////////////////////////////////////
    
    int LoadArray(CREDITREC crec[], int limit, char filename[]){
         ifstream infile;
         
         infile.open(filename, ios::in | ios::nocreate );
         if (!infile){
              cerr<<"Error: cannot open"<<filename<<endl;
         exit (1);
         }
         int i = 0;
         char c;
         while(i < limit && infile >>crec[i].acctno >> c){
              //input the customer name " delimited
         infile.getline(crec[i].custName, MaxNameLen, '\"');
         //input the next byte which can be the " if thee name is length.
    
         infile.get (c);
         //if it was the " then skip ws
         infile>>ws;
         infile.get (crec[i].stateCode, MaxStCodeLen);
         infile>>crec[i].creditlimit>>crec[i].balance;
         i++;
         }
    
         //guard against input data
         if(infile.bad()){
              cerr<<"Error: bad data input file.\nLast good id was";
              if((i - 1)== 0)
                   cerr<<"the first line\n";
              else
                   cerr<<crec[i - 1].acctno<<endl;
              infile.close ();
              exit (3);
         }
         infile.close ();
         return i;//return the number of records
    }
    //////////////////////////////////////////////////////////////////////
    //SortArray: sorts thee credit records into state code order        //
    //////////////////////////////////////////////////////////////////////
    void SortArray (CREDITREC crec[], int numRecs){
         CREDITREC temp;
         int i, j;
         for(i=0; i<numRecs-1; i++){
              for(j=i+1; j<numRecs; j++){
                   if (strcmp (crec[j].stateCode, crec[i].stateCode) < 0){
                        temp = crec[i];
                        crec[i] = crec[j];
                        crec[j] = temp;
                   }
              }
         }
    }
    
    //////////////////////////////////////////////////////////////////////
    //PrintReport: summarize data wile printing the report              //
    //////////////////////////////////////////////////////////////////////
    void PrintReport(CREDITREC crec[], int numRecs, char filename[]){
         char prevState[MaxStCodeLen];  //the previous state code for breaks
         int numAcctInState = 0;  //number of accounts in this state
         double totalLimit = 0;   //total credit limit in this state
         double totalBalance = 0;  //total credit extended in this state
         int grandNumStates = 0;
         int grandNumAccts = 0;      //company total number of accounts.
         double grandTotalLimit = 0; //company total credit limit
         double grandTotalBalance = 0; //company total credit extended
    
         //open report file and print hedings and column headings
         ofstream outfile (filename);
         outfile<<"Acme Credit Company - Creedit Extended by State Report\n\n"
                 <<"State Number of Credit Totals Extended\n"
                 <<"Code Accounts         Limit           Balance\n";
    
         outfile.setf(ios::fixed, ios::floatfield);
         outfile.setf(ios::showpoint);
         outfile<<setprecision(2);
    
         //guard against no records in the array
         if(numRecs){
              //initialize the previous state code to the first record
              //and initialize the initial state totals
         strcpy(prevState, crec[0].stateCode);
         totalLimit = crec[0].creditlimit;
         totalBalance = crec[0].balance;
         numAcctInState;
    
         //Loop through all the credit records
         for(int i=1; i < numRecs; i++){
              //check for a change in state code
              if (strcmp (prevState, crec[i].stateCode) != 0){
                   //this stat is the next state so print totals of the previous state
         outfile<<setw(4)<<prevState
                 <<setw(9)<<numAcctInState
                 <<setw(7)<< "$" <<setw(9)<<totalLimit
                 <<setw(6)<< "$" <<setw(9)<<totalBalance<<endl;
    
         //roll previous state totals into grand company totals
         grandNumAccts += numAcctInState;
         grandNumStates++;
         grandTotalLimit += totalLimit;
         grandTotalBalance += totalBalance;
         //reset previous state code to the new one
         strcpy(prevState, crec[i].stateCode);
         //reset the state totals to zero.
         numAcctInState = 0;
         totalLimit = 0;
         totalBalance = 0;
              }
         //accumulate this state values
              numAcctInState++;
              totalLimit += crec[i].creditlimit;
              totalBalance += crec[i].balance;
         }
         //print last state in the set of data
         outfile<<setw(4)<<prevState
                 <<setw(9)<<numAcctInState
                 <<setw(7)<< "$" <<setw(9)<<totalLimit
                 <<setw(6)<< "$" <<setw(9)<<totalBalance<<endl;
         grandNumAccts += numAcctInState;
         grandNumStates++;
         grandTotalLimit += totalLimit;
         grandTotalBalance += totalBalance;
         }
         //print the grand company totals
         outfile<<" --       ---       --------          -----------\n"
                 <<setw(4)<<grandNumStates
                 <<setw(9)<<grandNumAccts
                 <<setw(7)<< "$" <<setw(9)<<grandTotalLimit
                 <<setw(6)<< "$" <<setw(9)<<grandTotalBalance<<endl;
         outfile.close();
    }

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    try to localize the problem... it's difficult to diagnose if you have a brief description of the problem and a large possible area of error... and use code tags... [i've edited your post to implement them for improved readability...]
    hasafraggin shizigishin oppashigger...

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    84
    I got it to work: Here is the nes code
    I didn't have MAX_PATH declared and I changed a value in my for loop.

    #include<iostream.h>
    #include<fstream.h>
    #include<iomanip.h>
    #include<stdlib.h>
    #include<string.h>

    const int MAX_PATH = 100;
    const int MaxNameLen = 31;
    const int MaxStCodeLen = 4;
    const int MaxNum = 500;

    struct CREDITREC {
    long acctno; //account number = social security number;
    char custName[MaxNameLen]; //Customer name upto 30 cahracters.
    char stateCode[MaxStCodeLen]; //upper case state code.
    double creditlimit; //credit limit of customers
    double balance; //customers current balance due.
    };


    int LoadArray(CREDITREC crec[], int limit, char filename[]);
    void SortArray(CREDITREC crec[], int numRecs);
    void PrintReport(CREDITREC crec[], int numRecs, char filename[]);

    int main(){
    CREDITREC creditRec[MaxNum]; //array of customer credit records.
    int numRecs; //current number of customers in array.

    //Obtain the input and report filenames from the user
    char inputFilename[MAX_PATH];
    char reportFilename[MAX_PATH];
    cout<<"Enter the name of the credit master file: ";
    cin>>inputFilename;
    cout<<"Enter the name of the output filename: ";
    cin>>reportFilename;

    //load the array of credit records
    numRecs = LoadArray (creditRec, MaxNum, inputFilename);
    cout<<"\nArray loaded: "<<numRecs<<"records input\n";

    //sort the arrray into a state code order
    cout<<"Sorting the array\n";
    SortArray(creditRec, numRecs);

    //Produce the report
    cout<<"Printing the report\n";
    PrintReport(creditRec, numRecs, reportFilename);
    cout<<"Finished\n";
    return 0;
    }

    //////////////////////////////////////////////////////////////////////
    //Load Array //
    //////////////////////////////////////////////////////////////////////

    int LoadArray(CREDITREC crec[], int limit, char filename[]){
    ifstream infile;

    infile.open(filename, ios::in | ios::nocreate );
    if (!infile){
    cerr<<"Error: cannot open"<<filename<<endl;
    exit (1);
    }
    int i = 0;
    char c;
    while(i < limit && infile >>crec[i].acctno >> c){
    //input the customer name " delimited
    infile.getline(crec[i].custName, MaxNameLen, '\"');
    //input the next byte which can be the " if thee name is length.

    infile.get (c);
    //if it was the " then skip ws
    infile>>ws;
    infile.get (crec[i].stateCode, MaxStCodeLen);
    infile>>crec[i].creditlimit>>crec[i].balance;
    i++;
    }

    //guard against input data
    if(infile.bad()){
    cerr<<"Error: bad data input file.\nLast good id was";
    if((i - 1)== 0)
    cerr<<"the first line\n";
    else
    cerr<<crec[i - 1].acctno<<endl;
    infile.close ();
    exit (3);
    }
    infile.close ();
    return i;//return the number of records
    }
    //////////////////////////////////////////////////////////////////////
    //SortArray: sorts thee credit records into state code order //
    //////////////////////////////////////////////////////////////////////
    void SortArray (CREDITREC crec[], int numRecs){
    CREDITREC temp;
    int i, j;
    for(i=0; i<numRecs-1; i++){
    for(j=i+1; j<numRecs; j++){
    if (strcmp (crec[j].stateCode, crec[i].stateCode) < 0){
    temp = crec[i];
    crec[i] = crec[j];
    crec[j] = temp;
    }
    }
    }
    }

    //////////////////////////////////////////////////////////////////////
    //PrintReport: summarize data wile printing the report //
    //////////////////////////////////////////////////////////////////////
    void PrintReport(CREDITREC crec[], int numRecs, char filename[]){
    char prevState[MaxStCodeLen]; //the previous state code for breaks
    int numAcctInState = 0; //number of accounts in this state
    double totalLimit = 0; //total credit limit in this state
    double totalBalance = 0; //total credit extended in this state
    int grandNumStates = 0;
    int grandNumAccts = 0; //company total number of accounts.
    double grandTotalLimit = 0; //company total credit limit
    double grandTotalBalance = 0; //company total credit extended

    //open report file and print hedings and column headings
    ofstream outfile (filename);
    outfile<<"Acme Credit Company - Creedit Extended by State Report\n\n"
    <<"State Number of Credit Totals Extended\n"
    <<"Code Accounts Limit Balance\n";

    outfile.setf(ios::fixed, ios::floatfield);
    outfile.setf(ios::showpoint);
    outfile<<setprecision(2);

    //guard against no records in the array
    if(numRecs){
    //initialize the previous state code to the first record
    //and initialize the initial state totals
    strcpy(prevState, crec[0].stateCode);
    totalLimit = crec[0].creditlimit;
    totalBalance = crec[0].balance;
    numAcctInState;

    //Loop through all the credit records
    for(int i=0; i < numRecs; i++){
    //check for a change in state code
    if (strcmp (prevState, crec[i].stateCode) != 0){
    //this stat is the next state so print totals of the previous state
    outfile<<setw(4)<<prevState
    <<setw(9)<<numAcctInState
    <<setw(7)<< "$" <<setw(9)<<totalLimit
    <<setw(6)<< "$" <<setw(9)<<totalBalance<<endl;

    //roll previous state totals into grand company totals
    grandNumAccts += numAcctInState;
    grandNumStates++;
    grandTotalLimit += totalLimit;
    grandTotalBalance += totalBalance;
    //reset previous state code to the new one
    strcpy(prevState, crec[i].stateCode);
    //reset the state totals to zero.
    numAcctInState = 0;
    totalLimit = 0;
    totalBalance = 0;
    }
    //accumulate this state values
    numAcctInState++;
    totalLimit += crec[i].creditlimit;
    totalBalance += crec[i].balance;
    }
    //print last state in the set of data
    outfile<<setw(4)<<prevState
    <<setw(9)<<numAcctInState
    <<setw(7)<< "$" <<setw(9)<<totalLimit
    <<setw(6)<< "$" <<setw(9)<<totalBalance<<endl;
    grandNumAccts += numAcctInState;
    grandNumStates++;
    grandTotalLimit += totalLimit;
    grandTotalBalance += totalBalance;
    }
    //print the grand company totals
    outfile<<" -- --- -------- -----------\n"
    <<setw(4)<<grandNumStates
    <<setw(9)<<grandNumAccts
    <<setw(7)<< "$" <<setw(9)<<grandTotalLimit
    <<setw(6)<< "$" <<setw(9)<<grandTotalBalance<<endl;
    outfile.close();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question: Output of data.txt from the following program
    By outlaw525 in forum C Programming
    Replies: 9
    Last Post: 06-23-2008, 04:33 PM
  2. strange virtual function output
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2008, 08:08 AM
  3. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 06:41 PM
  4. a question about file output
    By wang8442 in forum C Programming
    Replies: 3
    Last Post: 12-26-2004, 03:18 AM
  5. a newbie question about file output
    By maybe in forum C Programming
    Replies: 8
    Last Post: 10-12-2004, 09:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21