THESE FORUMS NOW CLOSED (read only)

Fun Stuff => CLIKC => Topic started by: Dissy on 11 Jun 2008, 11:43

Title: C++
Post by: Dissy on 11 Jun 2008, 11:43
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?
Title: Re: C++
Post by: morbid79 on 11 Jun 2008, 12:49
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;
}
Title: Re: C++
Post by: Melodic on 11 Jun 2008, 13:52
using namespace std;

What version of C++ uses this syntax?
Title: Re: C++
Post by: morbid79 on 11 Jun 2008, 14:09
Uh, any kind?  AFAIK it is part of the language.  Sure compiles under GCC and MSVC.
Title: Re: C++
Post by: Dimmukane on 11 Jun 2008, 19:15
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?
Title: Re: C++
Post by: Melodic on 11 Jun 2008, 21:43
I only spent a short time on early builds of C++, when they updated the base every three weeks. I don't remember much, but we could totally start a Dev thread and I'd be all over it like white on rice.
Title: Re: C++
Post by: GenericName on 12 Jun 2008, 11:38
From what I remember, some programs do away with it because they figure most people are going to type that anyways so why not do it automatically?

But that was a year ago and I've forgotten all my C++, or else I'd help you with this.
Title: Re: C++
Post by: Melodic on 12 Jun 2008, 14:50
I think it was VC++ 6 or so that they did away with namespace syntax. I never realized they had added it back in until now.
Title: Re: C++
Post by: Catfish_Man on 19 Jun 2008, 23:24
using is definitely a part of standard C++, as are namespaces.

<edit>
Why are you testing against ios::fail()? ios::eof() seems like it's what you want (although I admit I didn't read your code very carefully
</edit>
Title: Re: C++
Post by: Melodic on 19 Jun 2008, 23:30
I never realized they added it in after it was removed from ~VC6. That's handy.

Also, on a similarly-brief glance I agree with on on eof.