• 0

C++ Homework, What am I doing wrong?


Question

Here is the code I have come up with for this week, when I run it it reads the same record over and over, and never stops. I am sure I just did some stupid mistake, but I have been working on this so long today, my eyes are going to bleed.

//Jason Watson
//SPD_6_5

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

using namespace std;

int main ()
{
    float TotalTax, TotalGrossEarnings, TotalMedicalLevy, TotalEarnings;   
    TotalTax = 0;     //Set TotalTax to 0
    TotalGrossEarnings = 0;  //Set TotalGrossEarnings to 0
    TotalMedicalLevy = 0;   //Set TotalMedicalLevy  to 0
    TotalEarnings = 0;    //Set TotalEarnings to 0
    short hours, rate, employeenumber; 
    cout <<"Employeee earnings and medical levy report: "<< endl << endl;          //Display Heading
    cout <<"Emp #"<<setw(11)<<"Gross"<<setw(14)<<"Tax"<<setw(16)<<"Levy"<<setw(13)<<"Net"<<endl<<endl;    //Read Employee Record
    ifstream empfile;                            
    empfile.open("empfile.txt",ios::in);         //Open File  
    if(empfile)                                 //Check For Error
    {
                empfile >> employeenumber >> hours >> rate;                                    
                do
                {
                        TotalGrossEarnings += hours*rate;                   //Calluclations
                        TotalTax += hours*rate*0.15;                                   
                        TotalMedicalLevy += hours*rate*0.01;
                        TotalEarnings += (hours*rate)-((hours*rate)*0.15)-((hours*rate)*0.01);
                        cout.setf(ios::fixed);
                        cout<<employeenumber<<setw(15)<<setprecision(2)<<hours*rate*1.00;
                        cout<<setw(15)<<hours*rate*0.15<<setw(15)<< hours*rate*0.01;
                        cout<<setw(15)<<(hours*rate)-((hours*rate)*0.15)-((hours*rate)*0.01)<<endl<<endl;
                        empfile >> employeenumber >> hours >> rate;                  
                }
                while (!empfile.eof());
                empfile.close();                                      //Close Employee file
                cout << endl;
                cout << "Total Groass Earnings: " << TotalGrossEarnings << endl;
                cout << "Total Tax: " << TotalTax << endl;
                cout << "Total Medical Levy: " << TotalMedicalLevy << endl;
                cout << "Total Earnings: " << TotalEarnings << endl << endl;
    }
    else {cout << "Error opening data file" << endl;} 

    cin.get();
    return 0;
}

Thank you for any help you can give

Shadow

Link to comment
https://www.neowin.net/forum/topic/881050-c-homework-what-am-i-doing-wrong/
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Where are you increasing the file line? In my experience, when you read a file, you MUST tell it to go to the next line somehow......not too familiar with C++ but I would assume it works in a similar way. Currently, you are not telling it to read a line, just a file, so it is never reaching the end-of-file, because it is stuck reading the first line.

Here we go:

  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

C++ may be different, as it doesn't appear to need to increase the line, but use a while loop instead of a do loop and it looks like it should work (just pulled this off the internet though).

  • 0

Try:

char c;
do
                {
                       //stuff;           
                }
                while (empfile >> noskipws >> c);

maybe the myfile.eof() returns true when it finds the '/n' char, if it only reads one line?

noskipws flag

edit

yep getline() might do the trick too.

I don't know much about C++ but does this have anything to do with the fact that you're closing the file stream inside the while loop?

He is not.

do
                {
                        TotalGrossEarnings += hours*rate;                   //Calluclations
                        TotalTax += hours*rate*0.15;                                   
                        TotalMedicalLevy += hours*rate*0.01;
                        TotalEarnings += (hours*rate)-((hours*rate)*0.15)-((hours*rate)*0.01);
                        cout.setf(ios::fixed);
                        cout<<employeenumber<<setw(15)<<setprecision(2)<<hours*rate*1.00;
                        cout<<setw(15)<<hours*rate*0.15<<setw(15)<< hours*rate*0.01;
                        cout<<setw(15)<<(hours*rate)-((hours*rate)*0.15)-((hours*rate)*0.01)<<endl<<endl;
                        empfile >> employeenumber >> hours >> rate;                  
                } //end do
                while (!empfile.eof());
                //out of do-while loop here
                empfile.close();  //safe

Edited by Chester0
  • 0

The problem is you're using the stream operators to read consecutive values; the stream has no idea of the length of each value. So if you do the following:

int a = 23;
int b = 46;
int c = 0;
int d = 0;
fstream << a << b;
fstream >> c >> d;

You'll end up with c = 2346 and d = 0 since the stream doesn't know where to stop reading the integer. Furthermore, we have no idea what your "test.txt" file looks like. Upload it.

Use the ofstream.write and ifstream.read functions instead.

Or better yet, put all of the employee hours, rate, etc into a structure and read/write the structure from/to the file in one call. I think I already told you to do this in another thread; I'm telling you to do it again. It'll make this a lot easier.

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

    • No registered users viewing this page.
  • Posts

    • It saddens me when cars are such dull colours now. Mine is bright metallic blue and I absolutely adore it for standing out in contrast to that depressing backdrop of traffic.
    • Sparkle 2.20.0 by Razvan Serea Sparkle is a free, open-source Windows optimization tool designed to make your PC faster, cleaner, and more private. With Sparkle, you can easily debloat Windows by removing unnecessary apps and services, disable Microsoft tracking to enhance privacy, and apply performance tweaks to boost speed. Its cleaner removes junk and temporary files, while every change is safe and fully reversible. Sparkle also features a modern, user-friendly interface with automatic updates, making system maintenance simple. Explore over 39 tweaks, from disabling telemetry and hibernation to optimizing network and game settings, all aimed at customizing and enhancing your Windows experience. Sparkle supports Windows 10 and 11. Sparkle 2.20.0 changelog: Debloat Tweak has animated border New homepage loading UI New Tweak Modal (Markdown Supported) Refactored GPU Detection Added Tests with vitest Added foobar2000 to apps Added Localsend to apps Updated Modal Styles Added styles for disabled inputs Added Animated Border to debloat-windows tweak Bumped dependencies Refactor System info logic for speed Tweak info modals now support Markdown Added Clear System info cache to settings Redesigned Home Page Loading UI Changed Some Icons around the app Download: Sparkle 2.20.0 | Portable | ~100.0 MB (Open Source) Links: Sparkle Website | Github | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • lol it was a typo, fixed! haha imagine an actual 4TB Gen4 NVMe for $40 in 2026
    • Get over it, has nothing to do with my point.
    • I really don't like these rounded corners and huge borders in the viewport. They just waste space without anything in return.
  • Recent Achievements

    • Reacting Well
      Dys Topia earned a badge
      Reacting Well
    • Conversation Starter
      NovaEdgeX earned a badge
      Conversation Starter
    • One Year In
      Console General earned a badge
      One Year In
    • Week One Done
      Twozo Technologies earned a badge
      Week One Done
    • One Month Later
      Twozo Technologies earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      517
    2. 2
      +Edouard
      184
    3. 3
      PsYcHoKiLLa
      106
    4. 4
      Steven P.
      88
    5. 5
      ATLien_0
      68
  • Tell a friend

    Love Neowin? Tell a friend!