Thread: Sequential file problem(s)

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    11

    Sequential file problem(s)

    This is my last lab assignment for the semester. It was supposed to be due next week, but my instructor will be out next week so he decided today that it would be due on Thursday. He did this because this assignment is harder than the one originally due on Thursday (which I already have completed, just my luck).

    I have the assignment pretty much done, but have one glitch and one lack of knowledge problem.

    1. I have to display, after reading and manipulating the file, the number of records in the file. I can't find that operation in my book anywhere. If you could point me in the right direction I would appreciate it.

    2. I will try to attach an output to show what I am talking about here, as well as the .txt file that I am reading from. Some of my records (the three with the shortest last names) have the info tabbed wrong. I have tried to correct this by modifying the .txt file, but no luck.

    And before I get too much grief, my instructor specified all of the names, except the first one, which is the name of the student doing the assignment.

    Code follows:

    Code:
    //Travis Bryant
    //CPT-168-A01
    //Payroll Sequential File
    
    #include<iostream>
    #include<string>
    #include<iomanip>
    #include<fstream>
    
    using namespace std;
    
    int main()
    {
        //declarations
        string fnam = "",lnam = "", ssn = "";
        double hrswrkd = 0.0, parat = 0.0, grpay = 0.0, ntpay = 0.0, d_duk = 0.0;
        
        system("color f0");
    
        //header
        cout<<"\t\t\t***********************************\n";
        cout<<"\t\t\t*          Travis Bryant          *\n";
        cout<<"\t\t\t*           CPT-168-A0            *\n";
        cout<<"\t\t\t*     Payroll Seqeuntial File     *\n";
        cout<<"\t\t\t***********************************\n\n";
    
        ifstream infile;
    
        // Open payroll file
        infile.open("payroll.txt");
    
        cout<<fixed<<setprecision(2);
        cout<<"   SSN\t\tName\t\tHours\tRate\tGross\tDeductions\tNetpay\n";
        cout<<"   ____\t\t____________\t_____\t____\t_____\t__________\t______\n";
    
        // priming read
        infile>>fnam>>lnam>>ssn>>hrswrkd>>parat;
    
        //begin while loop (priming read instruction)
        while(infile.eof() != true)
        {
            // calculate gross pay
            if (hrswrkd > 40)
                grpay = (40 * parat) + (hrswrkd - 40) * (1.5 * parat);
            else
                grpay = hrswrkd * parat;
    
            //calculate deductions
            d_duk = grpay * .10;
    
            //calculate net pay
            ntpay = grpay - d_duk;
    
            // Display output
            cout << "   " << ssn.substr(7) << "\t\t" << fnam.substr(0,1) << ". " << lnam << "\t" << hrswrkd << "\t" << parat << "\t"
                 << grpay << "\t" << d_duk << "\t\t"<< ntpay<< "\n";
            
            
    
            // read next record
            infile>>fnam>>lnam>>ssn>>hrswrkd>>parat;
        }
    
        infile.close();
    
        //display appreciation
        cout<<"\n\t\t\t\tTHANK YOU!!!\n\n";
        system("pause");
        return 0;
    }
    Attached Images Attached Images Sequential file problem(s)-sequential-file-output-error-png Sequential file problem(s)-sequential-file-read-source-png 

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    setw - C++ Reference
    If you want exact widths, don't use a simple hard tab.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    2. I will try to attach an output to show what I am talking about here, as well as the .txt file that I am reading from. Some of my records (the three with the shortest last names) have the info tabbed wrong. I have tried to correct this by modifying the .txt file, but no luck
    In your output I recommend you use the setw() manipulator instead of tabs, you will have better control of what goes where with the manipulators.

    1. I have to display, after reading and manipulating the file, the number of records in the file. I can't find that operation in my book anywhere. If you could point me in the right direction I would appreciate it.
    You will need a counter to count the number of times you read a line of text.

    Jim

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    Quote Originally Posted by jimblumberg View Post
    In your output I recommend you use the setw() manipulator instead of tabs, you will have better control of what goes where with the manipulators.



    You will need a counter to count the number of times you read a line of text.

    Jim
    Thank you. Now that you mention counters I feel like a moron. It never occurred to me to use a counter that way.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    // priming read
    infile>>fnam>>lnam>>ssn>>hrswrkd>>parat;
     
    //begin while loop (priming read instruction)
    while(infile.eof() != true)
    {
    
        ...
     
        // read next record
        infile>>fnam>>lnam>>ssn>>hrswrkd>>parat;
    }
    While your above loop does manage to avoid the pitfalls of using an end-of-file test to control access to the loop's body that we so often see here on the board, there are typically better ways of doing this:

    Code:
    //begin while loop
    while(infile>>fnam>>lnam>>ssn>>hrswrkd>>parat)
    {
        ...
    }
    This method avoids a priming read and the read operation at the end of the loop body in favor of a single read operation in the while-loop's conditional to control further processing. The return value of the read operation can be tested in a true/false context here and prevent entry into the loop body if no data can be read (or bad data has been read).





    NIT:
    Code:
    cout<<"\t\t\t*     Payroll Seqeuntial File     *\n";
    Sequential is misspelled... just sayin'.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    Quote Originally Posted by hk_mp5kpdw View Post
    Code:
    // priming read
    infile>>fnam>>lnam>>ssn>>hrswrkd>>parat;
     
    //begin while loop (priming read instruction)
    while(infile.eof() != true)
    {
    
        ...
     
        // read next record
        infile>>fnam>>lnam>>ssn>>hrswrkd>>parat;
    }
    While your above loop does manage to avoid the pitfalls of using an end-of-file test to control access to the loop's body that we so often see here on the board, there are typically better ways of doing this:

    Code:
    //begin while loop
    while(infile>>fnam>>lnam>>ssn>>hrswrkd>>parat)
    {
        ...
    }
    This method avoids a priming read and the read operation at the end of the loop body in favor of a single read operation in the while-loop's conditional to control further processing. The return value of the read operation can be tested in a true/false context here and prevent entry into the loop body if no data can be read (or bad data has been read).





    NIT:
    Code:
    cout<<"\t\t\t*     Payroll Seqeuntial File     *\n";
    Sequential is misspelled... just sayin'.
    That would be a much better way of accomplishing it, but we are limited to only the commands/functions in that particular chapter of the book. I'm glad you caught that spelling error. I had already finished the project and got it ready to turn in tomorrow. I will go change that right now. Thanks so much for the help and suggestions!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sequential file question
    By Bparm in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2010, 05:15 PM
  2. Updating in a sequential file?
    By Ronnyv1 in forum C Programming
    Replies: 1
    Last Post: 03-24-2009, 04:41 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. sequential file help plz
    By dutrachr in forum C Programming
    Replies: 4
    Last Post: 04-18-2006, 11:41 AM
  5. Adding \n to sequential file
    By stimpyzu in forum C++ Programming
    Replies: 13
    Last Post: 04-09-2002, 01:05 PM