• 0

Need help using a file (C++)


Question

I decided to take an online C++ class, and at first I was doing well but now I seem to be behind. I have read the chapters a few times and think I may have this one but I am probably completely off base. Here is the problem:

Design an algorithm that will read a file of employee records and produce a weekly report of gross earnings for those employees. Fross earnings are earnings before tax and other deductions. Each input record contains the employee number, the hours worked, and the rate of pay. At the end of the report print the total gross earnings for that week.

He did not give us a file to input from so it is hard to check to see if it worked. Here is what I have so far:

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
    {float GrossEarnings, TotalGrossEarnings, rate;
    int hours;
    unsigned int EmployeeNumber;
    ifstream infile;  // Declare file stream named infile.

    infile.open ("grossearnings.txt",ios::in); //Oppem file for input

    TotalGrossEarnings = 0.0;
    GrossEarnings = 0.0;
    rate = 0;
    hours = 0;

    cout << "This is the Employees Gross Earnings:\n";

    if (infile) // If no error occurred while opening file
     {
                cout.setf(ios::fixed);
                cout << "The number in the data file are as follows:\n"
                << setprecision(1);

     do
       {
                infile >> EmployeeNumber >> hours >> rate;     // Get number from file.
                cout << EmployeeNumber << GrossEarnings <<  endl;
                GrossEarnings = rate * hours;

                } 
                while(EmployeeNumber != 0.0);

      TotalGrossEarnings = TotalGrossEarnings + GrossEarnings;
      cout << "Total Gross Earnings are:" << TotalGrossEarnings <<endl;


    system ("PAUSE");

    return 0;
       }}}

Am I completly off base? Thank You!

Shadow

Link to comment
https://www.neowin.net/forum/topic/878628-need-help-using-a-file-c/
Share on other sites

5 answers to this question

Recommended Posts

  • 0

I don't have time to proof-read your code at the moment, but:

- test it. Write a txt file containing what you think the records should look like, and make the program use it.

- use code tags when you post code in a forum, otherwise it's almost impossible to read.

  • 0

Unfortunately I'm not so good with file reading or the iomanip library, so that's going to be up to you.

I did you a little service and cleaned up your code. If someone like Dr.Asik comes back and is able to review your code for you (or is willing. Keep in mind, we don't do your homework for you, here) then at least now it will be readable and, assuming you properly use your functions and such, functional.

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

int main()
{
    float GrossEarnings, TotalGrossEarnings, rate;
	GrossEarnings = TotalGrossEarnings = rate = 0.0; //assign 0.0 to ALL float values listed. Much easier to read.
    int hours = 0;
    unsigned int EmployeeNumber = 0; // assigning values on instantiation improves organization as well.
    ifstream infile;  // Declare file stream named infile.

    infile.open("grossearnings.txt",ios::in); //Open file for input
    cout << "This is the Employees Gross Earnings:\n";

    if (infile) // If no error occurred while opening file
	{
		cout.setf(ios::fixed);
		cout << "The number in the data file are as follows:\n" << setprecision(1);
	} //You were missing a bracket here.

	while(EmployeeNumber != 0.0) // I find do/while loops to be sloppy so a while loop makes the code more clean.
	{
		infile >> EmployeeNumber >> hours >> rate; // Get number from file.
		cout << EmployeeNumber << GrossEarnings << "\n";
		GrossEarnings = rate * hours;
	}

	TotalGrossEarnings += GrossEarnings; //+= returns the sum of the right and left operand and returns value to left operand.
	cout << "Total Gross Earnings are: " << TotalGrossEarnings << "\n";

    cout << "Press any key to exit.\n";
    cin.get();  //A lot of people argue against using the system("pause") command, it is better in many cases to use cin.get();
    return 0;
}

I'm not trying to nitpick at your code or anything, don't get me wrong.

You should have a look at the changes I made. Most of what I did greatly improves your organization, and makes the code much easier for others to use.

I can't really test this myself since I really don't know what you expect to be done with the file to be read from, but you might be able to put something together and give this a whirl.

Otherwise, Dr.Asik may be able to help.

Good luck. :yes:

  • 0

Unfortunately I'm not so good with file reading or the iomanip library, so that's going to be up to you.

I did you a little service and cleaned up your code. If someone like Dr.Asik comes back and is able to review your code for you (or is willing. Keep in mind, we don't do your homework for you, here) then at least now it will be readable and, assuming you properly use your functions and such, functional.

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

int main()
{
    float GrossEarnings, TotalGrossEarnings, rate;
	GrossEarnings = TotalGrossEarnings = rate = 0.0; //assign 0.0 to ALL float values listed. Much easier to read.
    int hours = 0;
    unsigned int EmployeeNumber = 0; // assigning values on instantiation improves organization as well.
    ifstream infile;  // Declare file stream named infile.

    infile.open("grossearnings.txt",ios::in); //Open file for input
    cout << "This is the Employees Gross Earnings:\n";

    if (infile) // If no error occurred while opening file
	{
		cout.setf(ios::fixed);
		cout << "The number in the data file are as follows:\n" << setprecision(1);
	} //You were missing a bracket here.

	while(EmployeeNumber != 0.0) // I find do/while loops to be sloppy so a while loop makes the code more clean.
	{
		infile >> EmployeeNumber >> hours >> rate; // Get number from file.
		cout << EmployeeNumber << GrossEarnings << "\n";
		GrossEarnings = rate * hours;
	}

	TotalGrossEarnings += GrossEarnings; //+= returns the sum of the right and left operand and returns value to left operand.
	cout << "Total Gross Earnings are: " << TotalGrossEarnings << "\n";

    cout << "Press any key to exit.\n";
    cin.get();  //A lot of people argue against using the system("pause") command, it is better in many cases to use cin.get();
    return 0;
}

I'm not trying to nitpick at your code or anything, don't get me wrong.

You should have a look at the changes I made. Most of what I did greatly improves your organization, and makes the code much easier for others to use.

I can't really test this myself since I really don't know what you expect to be done with the file to be read from, but you might be able to put something together and give this a whirl.

Otherwise, Dr.Asik may be able to help.

Good luck. :yes:

This is great! I am definitely not looking for someone to do my work, just someone to explain (like you did) where I may have gone astray. I am going to spend the weekend rereading all the chapters to see if I can get a better grasp on this, but you certainly have helped me.

  • 0

This is great! I am definitely not looking for someone to do my work, just someone to explain (like you did) where I may have gone astray. I am going to spend the weekend rereading all the chapters to see if I can get a better grasp on this, but you certainly have helped me.

Well, you can start by using an IDE. The code you originally posted was full of formatting errors and wouldn't even compile. As for your assignment, consider organizing related data into structures or classes. And Employee structure might look something like this:

struct Employee
{
	unsigned int EmployeeNumber;
	float Rate;
	float GrossEarnings;
};

One benefit of this is that the Employee structure above is of fixed-length: is this case 12 bytes (both ints and floats are 4 bytes). This makes it easy to store records in a file and is similar to how a database stores records. Moving one record forward simply involves incrementing a pointer to the structure. Furthermore, if you had a record file that was 36 bytes long, you could infer that it contained 3 Employee structures.

  • 0

Well, you can start by using an IDE. The code you originally posted was full of formatting errors and wouldn't even compile. As for your assignment, consider organizing related data into structures or classes. And Employee structure might look something like this:

struct Employee
{
	unsigned int EmployeeNumber;
	float Rate;
	float GrossEarnings;
};

One benefit of this is that the Employee structure above is of fixed-length: is this case 12 bytes (both ints and floats are 4 bytes). This makes it easy to store records in a file and is similar to how a database stores records. Moving one record forward simply involves incrementing a pointer to the structure. Furthermore, if you had a record file that was 36 bytes long, you could infer that it contained 3 Employee structures.

Thank you again... I understand what you are saying and like the logic behind it. We actually have not gotten into pointers or structures, but that won't keep me from using it in the homework.

You guys are great

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • This got me thinking, would you rather a self driving car prioritise protecting its passengers or everyone else? I'd choose the one that keeps me and my kids safest. At some point, these cars have to make those choices already, don't they? Wonder if we have a way to find out what way they lean.
    • The proportion (or number of iterations) has nothing to with this aspect of Copyright I am describing. In short, it doesn't matter how many times the manager tells you to change something or how. Your work product is always YOURS until and unless you then assign that to the person representing the client/company, usually for financial compensation -- either in salary or as a subcontract work for hire payment. if iterations determined copyright, then businesses would have learned to just keep making changes until they could claim they owned the copyright, without having to compensate the artist for their work. And that would be BAD. The only place where the amount of changes does have a role is in how much does a human modify a previous public domain work (from any source) before it is considered fair use or their own work, etc. For example, if a human makes substantial changes to a public domain (re: AI, by definition) work, then they can then claim that derivative work as their own...but NEVER the original version, of course. That's why anyone can make a movie about Dracula, for example, as long as it is based on the public domain novel, but not if they take new ideas from copyrighted movies made afterwards. As one of the people who personally advised the US Copyright Office on their recent ruling on these very issues, be assured that I specifically used the terminology precisely -- though I made it simple enough for laymen to understand it. If I made this confusing by doing so, I apologize. But, to be clear regarding your assumption that I would agree to your second statement that I quoted above -- the answer is NO. If AI does the work, no matter how much "direction" you give it, it cannot be copyrighted. All AI generated content is in the Public Domain and therefore the copyright cannot be assigned to ANYONE, even you -- until and unless substantial modifications are made to it BY A HUMAN BEING (yourself or a contracted artist/writer/etc.) and then that copyright on the derivative work is legally (in writing) transferred to you. This is a critical distinction. And it is important that people, especially AI sloppers, understand this. For example, YouTube is not paying AI slop generators for the copyright, etc. of their AI slop. What YouTube is doing is sharing AD REVENUE for permission to publish your AI slop. Copyright/ownership/rights never come into it. Importantly, that means that anyone can copy any AI slopware on YouTube, etc. and rehost it anywhere they want, even back on YouTube, and there is nothing legal that YouTube can do about it with regards to copyright protections, ownership, DMCA, etc. Anyone is legally free to use any AI slopware in any way they want. When this ruling was pending, I warned Disney legal of all of this before they did their OpenAI deal -- that it would literally dilute their entire IP portfolio forever. They ignored that warning for the PR and stock bump. But that is why, when the ruling came down last year, Disney quickly extricated themselves from that OpenAI deal, even eating the initial upfront fees -- followed closely by OpenAI ending their entire AI video generating business model. They adjusted their PR release dates to make this less obvious to shareholders, of course. Phew. I hope that this clears up the key distinctions for you and anyone reading. If you have any additional questions or even hypotheticals about AI and Copyright, please feel free to ask.
    • Each of the devices displayed on this page now has a little volume meter next to it to show if there is audio actively playing. About time.
    • Owing to the nature of Windows feature enablement updates, it was distributed over Windows Update services as a complete system upgrade rather than as an ordinary cumulative update
  • Recent Achievements

    • Collaborator
      ryansurfer98 went up a rank
      Collaborator
    • Week One Done
      Eurosoft10 earned a badge
      Week One Done
    • One Month Later
      Eurosoft10 earned a badge
      One Month Later
    • One Year In
      Skeet Campbell earned a badge
      One Year In
    • One Month Later
      Sharbel earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      561
    2. 2
      +Edouard
      188
    3. 3
      Michael Scrip
      78
    4. 4
      PsYcHoKiLLa
      74
    5. 5
      neufuse
      71
  • Tell a friend

    Love Neowin? Tell a friend!