0

Hi my assignment is due in a couple of hours and I am trying to write my code to produce this output but it is not working. My program doesn't even run at all and always fails and I don't know what the issue is. I'm having issues with what to place in int main() and how to process the data from file to the functions! I have been trying forever.. In need of major help !!!!! thanks for your time

sample input file :

Miss Informed
125432  32560.0
Sweet Tooth
5432  9500
Bad Data
1255  -4500.0
John Smith
1225  3500.0
Nancy Brown
1555  154500.0

CODE:

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;



    int main()
    {
        float CalcIncomeTax(float );
        float CalcNetSalary(float, float );
        bool OpenFile(ifstream& fin);
        bool OpenFile(ofstream& fout);
        void Instruct(void);
        void ReadData(ifstream & fin, string& Name , int &Id, float& grossIncome);
        void Print(ofstream&, string, int, float, float, float);

        ifstream    fin;
        ofstream    fout;
        string      Name;
        int         Id = 0;
        float       grossIncome = 0;
        float       netSalary;
        float       incomeTax = 0;


       Instruct ();


            netSalary = CalcNetSalary(grossIncome,incomeTax);
            incomeTax = CalcIncomeTax(grossIncome);
            Print(fout, Name, Id, grossIncome, incomeTax, netSalary);
            ReadData(fin, Name, Id, grossIncome);

        OpenFile(fin);
        {
            getline(fin, Name);
            while (!fin.eof())
            {
                fin >> Id >> grossIncome;
                cout << setw(20) << left  << Name
                << setw(8)  << right << Id
                << setw(10) << grossIncome  << endl;
                fin.ignore(10,'\n');
                fin >> Id >> grossIncome;
            }
            getline(fin,Name);
        }

        OpenFile(fout);

        ReadData(fin, Name, Id, grossIncome);


        fin.close();
    }


            bool OpenFile(ifstream&fin)
    {
        cout <<"\nEnter the name and location of the input file:   ";
        string file_input;
        getline(cin, file_input);
        fin.open(file_input.c_str() ) ;
        if(fin.fail())
            return false;
        else
            return true;
    }
            bool OpenFile(ofstream &fout)
    {
        cout <<"Enter the name and location of the output file:   ";
        string file_output;
        getline(cin, file_output);
        fout.open( file_output.c_str() );
        if (fout.fail())
        return false;
        else
        return true;
    }


        void Instruct()
    {
        cout << "Programmer:"<< setw(25) << "//" << endl;
        cout << "Programming Assignment" << setw(5) << "4" << endl;
        cout << "This program will calculate and report tax liability" << endl;
    }

        float CalcIncomeTax(float grossIncome)
    {
        float incomeTax = 0;

        if (grossIncome <= 3500)
        {
            incomeTax = 0.00;
        }
        else if (grossIncome >= 3500 && grossIncome <= 8000)
        {
            incomeTax = 0 + 0.06 * (grossIncome - 3500);
        }
        else if (grossIncome >= 8000 && grossIncome <= 20000)
        {
            incomeTax = 270.00 + 0.11 * (grossIncome - 8000);
        }
        else if (grossIncome >= 20000 && grossIncome <= 34000)
        {
            incomeTax = 1590.00 + 0.17 * (grossIncome - 20000);
        }
        else if (grossIncome >= 34000 && grossIncome <= 54000)
        {
            incomeTax = 3970.00 + 0.24 * ( grossIncome - 34000);
        }
        else if (grossIncome >= 54000)
        {
            incomeTax = 8770.00 + 0.32 * ( grossIncome - 52000);
        }
        else if (grossIncome < 0)
        {
            cout << "****Invalid Income";
        }
        return(incomeTax);
    }


        float CalcNetSalary( float grossIncome, float incomeTax)
    {
        float netSalary;
        netSalary = grossIncome - incomeTax;
        return (netSalary);
    }

        void Print(ofstream& fout, string Name, int Id, float grossIncome, float incomeTax, float netSalary)
    {
        cout << setfill(' ') << left << setw(18) << "\tName";
        cout << setfill(' ') << left << setw(12) << "ID";
        cout << setfill(' ') << left << setw(17) << "Gross Income";
        cout << setfill(' ') << left << setw(12) << "Taxes";
        cout << setfill(' ') << left << setw(16) << "Net Income";
        cout << endl;

        cout << setfill('=') << setw(70)<<"\t";
        cout<<endl;

        cout << setprecision(2) << showpoint << fixed;
        cout << setfill(' ') << "\t" << setw(17)<< Name;
        cout << setfill(' ') << setw(12) << Id;
        cout << '$' << setfill(' ') << setw(16) << grossIncome;
        cout << '$' << setfill(' ') << setw(11) << incomeTax;
        cout << '$' << setfill(' ') << setw(16) << netSalary;
        cout << endl;
    }

HOW OUTPUT SHOULD BE

Name             ID           Gross Income        Taxes         Net Income 
Miss Informed    125432        $32560.00     **** Invalid ID
Sweet Tooth      5432          $9500.00          $435.00         $9065.00
Bad Data         1255          $-4500.00     **** Invalid Income
John Smith       1225          $3500.00          $0.00           $3500.00
Nancy Brown      1555          $154500.00        $40930.00       $113570.00
user2914173
  • 1
  • 1
  • 4

1 Answers1

1

The way to write a program is not to write everything and then try to run it. Start small and simple, add complexity a little at a time, test at every step and never add to code that doesn't work.

This will take a few iterations. We'll start with something that can read from a file:

#include <iostream>
#include <fstream>
#include <string>

using namespace std; // This is a good TEMPORARY SHORTCUT.

int main()
{
  ifstream fin("inputdata");

  string firstName, lastName;

  fin >> firstName >> lastName;

  cout << "First name is " << firstName << endl;
  cout << "Last name is " << lastName << endl;

  return(0);
}

Post a comment when you have this working, and we'll go the next step.

Beta
  • 96,650
  • 16
  • 149
  • 150
  • I can't code it that way because I have to prompt the user for filename – user2914173 Nov 07 '13 at 03:48
  • Start small and refactor to add features. If you are able to load a hardcoded file then try prompting the user and making sure that works, etc. – Retired Ninja Nov 07 '13 at 03:52
  • @user2914173: Read it again: **Start small and simple, add complexity a little at a time.** Prompting for a filename is the first thing the finished code must do, but that doesn't mean it's the first thing we must write. Conceptually, the file input itself is more important, so let's do that first. – Beta Nov 07 '13 at 03:53
  • @user2914173: where is your ReadData function defined? –  Nov 07 '13 at 04:03
  • just to add to Beta comments, you must have an algorithm in your head but more practically written down, describing step by step how your program is going to work. Then you implement it bit by bit. Any other approach and you end up where you are now. Sadly I don't think you'll pass. –  Nov 07 '13 at 04:18
  • 1
    @user2914173 programming is harder than it looks. In order to write a program that works, you have to first start with something simple. Then you run it, test it. Then you add another bit. Run it, test it. Each component runs, and is tested, and you know it works. Then you add another component. The problem of "reading from the file" can be solved separately from "calculating tax" which can be solved separately from "generating pretty output". Solve *one* of these problems, produce a test that demonstrates you got it right. If you try all parts at once, you will fail. Stop failing. – Yakk - Adam Nevraumont Nov 07 '13 at 04:32
  • @AndrewDouglas: I disagree on one small point. I think it is not always necessary to have the *entire* algorithm in mind before implementing anything. Sometimes it's better to start implementing the parts of the algorithm you're sure you'll need, while other parts are still a little vague. This can greatly clarify the remaining problem. – Beta Nov 07 '13 at 04:37
  • Algorithms are very helpful but sometimes I notice things while writing the code, which ends up adding to my algorithm – user2914173 Nov 07 '13 at 04:41
  • 1
    @Beta: A high level algorithm with placeholders will help to keep a bigger picture and ensure the approach is valid. I think this is precisely the problem here. –  Nov 07 '13 at 04:41
  • I understand I would have to try step by step to make sure Im doing everything correctly. I have just started understanding how functions work and I'm still trying to understand them. I just don't understand how it works exactly like where to ReadData or what to type inside int main or if I shouldn't – user2914173 Nov 07 '13 at 04:43
  • @user2914173: In your code you have function calls in the wrong order, this tells me you have no high level view of how the program solves the problem. –  Nov 07 '13 at 04:43
  • Yes exactly, I'm having problems with that and when to call, where to place everything – user2914173 Nov 07 '13 at 04:44
  • @user2914173 You need to draw/describe the algorithm in plain english. Then code it –  Nov 07 '13 at 04:45
  • and what different or more functions I need – user2914173 Nov 07 '13 at 04:45
  • @user2914173 Follow Beta advice and you may be able to salvage this. Start from scratch. –  Nov 07 '13 at 04:48
  • @AndrewDouglas: I completely agree about high-level algorithms. The highest level in this case is "read stuff from a file and print it to the screen a little differently". Once we have those two parts working *at all*, we can zoom in, and in, and in... – Beta Nov 07 '13 at 04:48
  • Is it a good idea, for my case, to write the code without using functions and then re-write it using functions – user2914173 Nov 07 '13 at 04:54
  • @user2914173: I wanted to keep the first step as simple as possible while still accomplishing something. Yes, I could have made the first step a function called `readItem` that did nothing. Maybe I should have. Let's discuss that *later*. **Do you have the above code working yet?** – Beta Nov 07 '13 at 04:58
  • @user2914173 No. Use functions to group functionality. Functions should do one thing: read a file, or print, or calculate or display something, etc, etc –  Nov 07 '13 at 04:58
  • No I don't have it working yet, Im working on it. Just one question , to read the names and numbers from the file is it correct the way I'm doing it like get line(fin,Name) and how does looping all the names etc. until eof work? I'm kind of confused with that too. I have tried numbers only before but I'm confused with string and numbers together – user2914173 Nov 07 '13 at 05:12
  • @user2914173: Yes, you can use `getline` to get the whole name. *Get that working* and then we'll tackle the problems of reading numbers and looping through the whole file. – Beta Nov 07 '13 at 05:15
  • Should probably move this to chat. –  Nov 07 '13 at 05:18
  • Why does my code output only the first line of the file continuously? – user2914173 Nov 07 '13 at 05:37
  • getline(fin,Name) while(!fin.eof()) { fin >> Id >> grossIncome; cout << setw(20) << left << Name << setw(8) << right << Id << setw(10) << grossIncome << endl; fin.ignore(10,'\n'); getline(fin,Name); } //is this incorrect? – user2914173 Nov 07 '13 at 05:37
  • @user2914173: You can edit your question; at the bottom, put a big clear "**EDIT**", then add your new code. And don't... try... the... loop... yet. – Beta Nov 07 '13 at 05:45
  • I was using a horrible text file and didn't realize that the first name was deleted until now. I am succeeding much better now. You guys helped me a lot thank you so much really for your time! – user2914173 Nov 07 '13 at 06:01