Fun Stuff > CLIKC

C++

(1/2) > >>

Dissy:
I'm guessing that since this is computer related......


Source code:
# include <iostream>
# include <iomanip>
# include <fstream>
using namespace std;

//istream& getData(fstream &is, string name, int rate, int hours, int taxBracket,
//                         double ytd, double ytdTax, int week1);

//void calcGross(double rate, double hrs, double &gross);
//void calcTax(double grossPay, int taxBracket, double&tax);
int main()
{
    ifstream fin("lab4.in");
    if(fin.fail())
    {
                  cerr << "Error opening input file. \n\n";
                  exit(1);
    }
    ofstream fout("lab4.out");
    if(fout.fail())
    {
              cerr << "Error opening output file.\n\n";
              exit(1);
    }   
    fout.setf(ios::showpoint);
    fout.setf(ios::fixed);
    fout.precision(2);             
 
    double hours, pay, dependents, grosspay, overtime;
    double tax, taxpay, percent, finalpay;
    int workers;
   
    fin >> workers >> hours >> pay >> dependents >> tax;
   
    fout << "Lab 4\t\t\t Kevin \n";
    fout << setw(10) << "Employee\t" << setw(15) << "Hours worked\t"
         << setw(10) << "Pay Rate\t" << setw(15) << "Tax Bracket\t"
         << setw(5) << "# Dep\t" << setw(10) << "Gross Pay\t"
         << setw(15) << "Federal Tax\t" << setw(10) << "Net Pay\n";
    cout << "I'm bored.  What about you?\n"
         << "Seriously, why are you here?\n"
         << "Press Ctrl & C.\n" << endl;

    while (!fin.fail())
    {         
         if (hours > 40)
         {
            overtime = hours - 40;
            hours = 40;
         }
         if (tax==1)
            percent = .13;
         else if (tax==2)
            percent = .27;
         else if (tax==3)
            percent = .35;
         else percent = .35;
             percent = percent - (dependents*0.01);
             grosspay = (hours * pay) + (overtime * pay * 1.5);
             taxpay = grosspay * percent;
             finalpay = grosspay - taxpay;
             hours += overtime;
         
          fout << workers << "\t"
               << setprecision(2) << hours << "\t"
               << setprecision(2) << pay << "\t"
               << setprecision(2) << tax << "\t"
               << setprecision(2) << dependents << "\t"
               << setprecision(2) << grosspay << "\t"
               << setprecision(2) << taxpay << "\t"
               << setprecision(2) << finalpay << endl;
    }
    fin.close();
    fout.close();
    system ("pause");
    return 0;
}

File In:
  1         50.00      20.00        2         3   
2         40.00      10.00        1         0   
3         10.00      40.00        3         5   
4         40.00      11.25        1         2   


I am getting an infinite loop off of the first line.  Anyideas how to fix this?

morbid79:
My hack:

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

//istream& getData(fstream &is, string name, int rate, int hours, int taxBracket,
//                         double ytd, double ytdTax, int week1);

//void calcGross(double rate, double hrs, double &gross);
//void calcTax(double grossPay, int taxBracket, double&tax);
int main()
{
   ifstream fin("lab4.in");
   if(fin.fail())
   {
      cerr << "Error opening input file. \n\n";
      exit(1);
   }
   ofstream fout("lab4.out");
   if(fout.fail())
   {
      cerr << "Error opening output file.\n\n";
      exit(1);
   }   
   fout.setf(ios::showpoint);
   fout.setf(ios::fixed);
   fout.precision(2);             

   // Dump the header
   fout << "Lab 4\t\t\t Kevin \n";
   fout << setw(10) << "Employee\t" << setw(15) << "Hours worked\t"
         << setw(10) << "Pay Rate\t" << setw(15) << "Tax Bracket\t"
         << setw(5) << "# Dep\t" << setw(10) << "Gross Pay\t"
         << setw(15) << "Federal Tax\t" << setw(10) << "Net Pay\n";

   double hours, pay, dependents, grosspay, overtime;
   double tax, taxpay, percent, finalpay;
   int workers;

   while ((fin >> workers >> hours >> pay >> dependents >> tax) && !fin.fail())
   {
      if (hours > 40)
      {
         overtime = hours - 40;
         hours = 40;
      }
      if (tax==1)
         percent = .13;
      else if (tax==2)
         percent = .27;
      else if (tax==3)
         percent = .35;
      else percent = .35;
      percent = percent - (dependents*0.01);
      grosspay = (hours * pay) + (overtime * pay * 1.5);
      taxpay = grosspay * percent;
      finalpay = grosspay - taxpay;
      hours += overtime;

      fout << workers << "\t"
         << setprecision(2) << hours << "\t"
         << setprecision(2) << pay << "\t"
         << setprecision(2) << tax << "\t"
         << setprecision(2) << dependents << "\t"
         << setprecision(2) << grosspay << "\t"
         << setprecision(2) << taxpay << "\t"
         << setprecision(2) << finalpay << endl;
   }
   fin.close();
   fout.close();
   system ("pause");
   return 0;
}

Melodic:

--- Quote from: morbid79 on 11 Jun 2008, 12:49 ---using namespace std;

--- End quote ---

What version of C++ uses this syntax?

morbid79:
Uh, any kind?  AFAIK it is part of the language.  Sure compiles under GCC and MSVC.

Dimmukane:
Yeah, it's a VERY common thing in VC++ projects....

Not to doubt your abilities as a programmer (I'm not), but how did you never come across that?

Navigation

[0] Message Index

[#] Next page

Go to full version