-7

I am using orwell dev| c++

C++ is a new language to, i am coming from c# which is like i would say 70% the same.

here is my code

#include<iostream>
#include<fstream>

using namespace std;
///loading libraries

float const taxnet = .9;
float const taxwh = .1;
int employeenumber;
float payrate, gross, net, manhours, overtime, taxes;

///declaring variablies and costants
char more;
char more2;

///user controls

///payroll calculations function
void payrollcalc () {

    if (manhours>40) {

        overtime= manhours-40;
        gross= ((manhours - overtime) * payrate)+((payrate * 1.5)* overtime);
        //overtime claculation
    }
    else {

        gross =manhours * payrate;

        //no overtime calculation
    }

    taxes= gross * taxwh;
    net = gross * taxnet;

    //taxesand net pay calculation

    cout<< " Your ID is                " << employeenumber <<endl;
    cout<< " # of hours worked         " << manhours << endl;
    cout<< " Your Hourly rate is       " << payrate << endl;
    cout<< " Your Gross pay is         " << gross << endl;
    cout<< " Your tax rate is          " << taxwh << endl;
    cout<< " Amount of taxes           " << taxes << endl;
    cout<< " Your net pay is           " << net << endl;
    ///writing to file

    std::string empnum = std::to_string(employeenumber);

    ofstream payroll;
    payroll.open (empnum+".txt");
    payroll<< " Your ID is                " << employeenumber <<endl;
    payroll<< " # of hours worked         " << manhours << endl;
    payroll<< " Your Hourly rate is       " << payrate << endl;
    payroll<< " Your Gross pay is         " << gross << endl;
    payroll<< " Your tax rate is          " << taxwh << endl;
    payroll<< " Amount of taxes           " << taxes << endl;
    payroll<< " Your net pay is           " << net << endl;
    payroll.close();

}

main(){

    while (more != 27){

        //main

        cout<< "Hit 1 to enter data hit 2 to recall dat hit esc to exit";
        ///instructions

        newdata:
        ///call back see line 115

        if (more == 49) {

            cout<< "Enter Employee ID:";
            cin>> employeenumber;
            cout<<"Enter Number of Hours Worked:";
            cin>> manhours;
            cout<<"Enter Pay rate:";
            cin>> payrate;
            cin>> payrollcalc;
        }
        else (more == 50) {

            olddata:
            ///call back see line 111
            errorreset:
            cout<< "Enter employee number";
            cin>> employeenumber;
            ///reading in data
            ifstream payroll = employeenumber;
            payroll.open(employeenumber".txt");
            if (!payroll){

                cout>> "Check employeenumber and try agian" endl;
                goto errorreset:
                ///error check

            }
            cout>> payroll.eof endl;
            cout>> endl;
            cout>> endl;
            cout>> "Press Enter to see another employee number; Press space to enter new employee information; press escape to exit the program" endl;
            if (more2 == 13 ){
                goto olddata;
            }
            else (more2 == 32){

                goto newdata;

            }
    ///sending back to the loop

        }

//entering data
        return 0;
    }
}

I think my issues is in this segment

std::string empnum = std::to_string(employeenumber);

ofstream payroll;
payroll.open (empnum+".txt");
payroll<< " Your ID is                " << employeenumber <<endl;
payroll<< " # of hours worked         " << manhours << endl;
payroll<< " Your Hourly rate is       " << payrate << endl;
payroll<< " Your Gross pay is         " << gross << endl;
payroll<< " Your tax rate is          " << taxwh << endl;
payroll<< " Amount of taxes           " << taxes << endl;
payroll<< " Your net pay is           " << net << endl;
payroll.close();

If some can step me through where i am going off the rails i would be grateful because i am out of ideas.

AliciaBytes
  • 7,300
  • 6
  • 36
  • 47
  • 3
    I think you should look over a c++ book. In the simplest term, where is your variables in `main`. Besides, I don't recommend you to not use of `goto` [Source](http://stackoverflow.com/questions/46586/goto-still-considered-harmful) – Soner from The Ottoman Empire Jun 12 '15 at 00:03
  • 2
    What issues? What's the actual problem? – Jonathan Potter Jun 12 '15 at 00:07
  • 1
    How are you learning C++? I'd recommend reviewing your materials, also since there are lots of legacy tutorials around for C++ I'd recommend [one of these books](http://stackoverflow.com/q/388242/1942027) if you got problems with your current material. I can spot at least 4 errors and multiple bad practices in your code. It'd probably be best to clean up the formatting as it's inconsistent and turn up the warning level of your compiler it should already give you a lot of information (for gcc use `-Wall -Wextra` for sensible warnings. – AliciaBytes Jun 12 '15 at 00:17
  • 1
    «C++ 70% like C#»: guess it's time to grab a book. This little sentence makes it very clear you don't get C++... – Macmade Jun 12 '15 at 00:27
  • 1
    **Post the error messages!** – CinchBlue Jun 12 '15 at 00:30
  • I read through my text book but i found it to be of little help. i end up scrap the code and rewriting it http://pastebin.com/vT7yemrA – torrey garcia Jun 13 '15 at 00:40

1 Answers1

2

First off consider turning up the error/waring level of your compiler, for gcc/clang a sensible level would be -Wall -Wextra for a start.

Let me go through some of the problems I see in your code.

main(){

We got a first problem here already. The only 2 signatures allowed for the main function in C++ are int main() or int main(int argc, char *argv[]). Yours might be accepted due to legacy reasons (implicit return type of int in C if you don't specify any) but shouldn't be used.

cout>> payroll.eof endl; // this line absolutely makes no sens and you forgot some `<<` in here probably.
cout>> endl;
cout>> endl;
cout>> "Press Enter to see another employee number; Press space to enter new employee information; press escape to exit the program" endl;

The 'arrows' point into the wrong direction. It should be cout << endl. To remember it see them as arrows that signify the data flow. cin >> variable the data is read from cin and gets put into the variable, cout << variable the variable gets output into cout.

Then you got a read that doesn't make sense:

cin>> payrollcalc;

payrollcalc is a function, I don't know what you wanted to do here, but you should probably call it like payrollcalc();, trying to read from cin into it doesn't make sense.

You're also using std::string without #include <string>. Also note that you should probably put a space in the include line like I did. I don't know if it's a problem without space of the top of my head but it's certainly more readable with a space.


Now for some bad practices and other stuff that I should probably point out. Your indentation and formatting were very messy and made it hard to spot anything you should probably see into cleaning it up a bit.

As for goto it's considered bad practice/harmful. This was started by Dijkstra. Now to say it got it's uses still in some places and is often over exaggerated but for the simple code you're using I don't think it's necessary and could probably be done in a better more structured way that makes the logic easier to understand, remember code is read far more often than you write it and readability is very important. If you're curious about the problems, @itsnotmyrealname posted a link on your question already for you to look through.

Also you got inconsistencies that make it harder to read/confusing.

gross =manhours * payrate; // inconsistent assignments
taxes= gross * taxwh;
net = gross * taxnet;

std::to_string(employeenumber) // inconsistent function calls.
payroll.open (empnum+".txt");

As for the global variables if you've already got some experience in programming (in C# like you said) then you probably know that they are considered bad practice too and should be avoided, you should look into making them local variables and passing them around as function arguments/returning them from functions.

AliciaBytes
  • 7,300
  • 6
  • 36
  • 47
  • I would also add that the use of the `goto`s are not really appropriate in the way OP is trying to use them – Alejandro Jun 12 '15 at 00:46
  • wow, that got longer than I initially thought... Also I don't think I'll look into the actual program logic as the other problems already took quite some time. – AliciaBytes Jun 12 '15 at 00:57
  • Actually update my code and fixed a lot of that stuff. http://pastebin.com/vT7yemrA is my most current code/ – torrey garcia Jun 13 '15 at 00:38
  • @torreygarcia late reply since you commented when I was about to go to bed yesterday. I rewrote it in the way I would write it [here](http://pastebin.com/nRARGv0m). I left a few small comments and a big one at the start. The file compiles, see 2nd paragraph of the big comment for a note on it. I decided that it might be better to rewrite it as an example and hope it's quite readable/easy to understand. I didn't change too much (I hope). – AliciaBytes Jun 13 '15 at 13:49
  • @RaphaelMiedl thank you you explained a lot. and you are correct we haven't gotten to class struct. I appreciate seeing working code, i am comparing it to my code now. so ican how to improve my technique an better under stand c++. – torrey garcia Jun 13 '15 at 14:32