-1

I am trying to figure out how to allow my program to have another name input indefinitely. Right now, if you'd want to look up another name, you'd need to restart the entire program. This program calculates employee net pay value. All employees have a standard $45 deduction from their checks. If an employee does not earn enough to cover the deduction, an error is displayed.

#include <iostream>
#include <string>

using namespace std;

int
main()
{
  // Declare variables
  string name;
  int hours;
  int DEDUCTION = 45;
  int gross;
  int net;
  int rate;
  string EOFNAME = "quit";
  char again = 'Y';

  // Declare input items
  cout << "Enter first name or " << EOFNAME << " to quit ";
  cin >> name;
  if (name == EOFNAME) {
    cout << "End of program ";
    return 0;
  } else {
    cout << "Enter hours worked for " << name << endl;
    cin >> hours;
    cout << "Enter hourly rate for " << name << endl;
    cin >> rate;
    gross = hours * rate;
    net = gross - DEDUCTION;
  }
  do {
    cout << "Net pay for " << name << " is " << net << endl;
    break;
  } while (net > 0);

  {
    if (net < 0)
      cout << "Deductions not covered. Net is 0." << endl;
  }

  return 0;
} // end of main
rsjaffe
  • 5,600
  • 7
  • 27
  • 39
Fizzik
  • 21
  • 3
  • You already use `while`. You should know how to loop. – DimChtz Jan 24 '19 at 20:36
  • Use a `for`, `while` or `do` loop (or even a `label:` + a `goto` - *not* recommended) if you want a loop in your program. – Jesper Juhl Jan 24 '19 at 20:37
  • @DimChtz I really don't know how to do much. This is the first program I've ever written. – Fizzik Jan 24 '19 at 20:45
  • Please start with a tutorial for the language you are trying to learn, I don't think this forum is the right place for this. – joanis Jan 24 '19 at 20:58
  • [Recursion](https://en.wikipedia.org/wiki/Recursion_(computer_science)) may be an option as well. – user4581301 Jan 24 '19 at 21:11
  • 1
    @joanis Prefer to push people toward [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) unless you have a tutorial in mind. The number of indifferent and outright harmful tutorials dwarf the number of tutorials that will make a person a better programmer. Until that person knows enough about the language and its idioms to be able to tell good from bad, odds are too high they will find bad. – user4581301 Jan 24 '19 at 21:21
  • @user4581301 good point, that's a more reliable source of good language introductions. Noted. – joanis Jan 24 '19 at 21:25

1 Answers1

1

This is a case where you need to implement a main loop. Might I suggest the following:

First, take everything you have in main() and put it in another function, something like:

void processEmployeeDeductions()
{
  // All the code currently in main()
}

Then start with an infinite-loop in main() that just calls the function repeatedly:

int main()
{
  while(1) // Loop forever!
  {
    processEmployeeDeductions();
  }
  return 0;
}

When your code becomes more sophisticated, you will be able to change the conditional of the loop to trigger on specific events (such as a handler SIGINT setting a flag to end the loop).

jhill515
  • 894
  • 8
  • 26