C++/C++

Advertisement


Question
Hi Amit!!! I did that program and my instructor said that I missing Loop. And I don't know what Loop to put on. Can u help me?

/*
  Written by          Antonio Tando
  Course          COMP-122
  Class          2DDA
  Due Date          05/xx/07
  Date Submitted          05/10/07
  Filename          SalesCommission.cpp

  This program will calculate the Weekly Gross pay  for a Salesperson
  given the Base Pay and the Weekly Sales Amount.  It will not allow values
  outside the stipulated ranges:
    name <= 20 characters
    0 <= base pay <= $300
    0 <= weekly sales <= $10,000

  Additionally, the program will also accept yes or no from the user for
  response to 'another employee?'.

*/

#include <iostream>
#include <iomanip>

using namespace std;

void main(void)
{
 char   employeeName[21],
        SSN[12],
        answer;
 double weeklyGrossPay,
        weeklyBasePay,
        weeklySales,
        commission,
        commissionAmount,
        totalPay;
 int    shortCommission;
 bool   firstTime;

 firstTime = true;
 cout << "This program will calculate the Weekly Gross pay for a Salesperson "
      << "given the\nBase Pay and the Weekly Sales Amount." << endl << endl;
 do
 {
   if (!firstTime) system("cls");
   firstTime = false;
   cout << "Enter Employee Name (first last <= 20 chars) .....  ";
   cin.get(employeeName,21);  cin.ignore(100,'\n');
   
   cout << "\nEnter Employee Social Security # (ddd-dd-dddd) ...  ";
   cin.get(SSN,12);  cin.ignore(100,'\n');

   do
   {
     cout << "\nEnter Employee Weekly Base Pay (up to $300.00) ...  ";
     cin >> weeklyBasePay;
     if (weeklyBasePay > 300 || weeklyBasePay < 0)
       cout << "Please enter a Weekly Base Pay up to $300.00 \x07" << endl;
   } while (weeklyBasePay > 300 || weeklyBasePay < 0);

   do
   {
     cout << "\nEnter Employee Weekly Sales Amount ...............  ";
     cin >> weeklySales;
     if (weeklySales > 10000 || weeklySales < 0)
       cout << "Please enter a Weekly Sales Amount up to $10,000.00 \x07" << endl;
   } while (weeklySales > 10000 || weeklySales < 0);

   if (weeklySales >= 5000.01)
     commission = 0.15;
   else
       if (weeklySales >= 3000.01)
         commission = 0.10;
       else
         if (weeklySales >= 1000.01)
         commission = 0.05;
         else
         commission = 0.0;

   //system("cls");
   commissionAmount = commission * weeklySales;
   totalPay = weeklyBasePay + commissionAmount;
   shortCommission = commission * 100;
   cout << "\n\n\nEmployee"
        << "\t\t\tPay Category"
        << "\t\t  Pay" << endl;
   cout << "=====================================================================";
   cout << "\nName: " << left << setw(20) << employeeName
        << "\tWeekly Base Pay\t\t$"
        << right << fixed << showpoint << setprecision(2)
        << setw(8) << weeklyBasePay;

   cout << "\n\nSSN:  " << left << setw(11) << SSN
        << "\t\t" << fixed << noshowpoint << left
        << shortCommission << "% of Sales\t\t$"
        << right << fixed << showpoint << setprecision(2)
        << setw(8) << commissionAmount;

   cout << "\n\nSales: $" << left << setw(20)
        << showpoint << weeklySales
        << "\tTotal Gross Pay\t\t$"
        << right << fixed << showpoint << setprecision(2)
        << setw(8) << totalPay;
   do
   {
     cout << "\n\nDo you wish to Calculate Gross for Another Employee Y/N ? ";
     cin  >> answer; cin.ignore();
     if (answer!='y' && answer!='Y' && answer != 'n' && answer != 'N')
       cout << "\nPlease answer (Y)es or (N)o.";
   } while (answer != 'y' && answer != 'Y' && answer != 'n' && answer != 'N');

 } while (answer == 'y' || answer == 'Y');

 cout << endl << endl;
}


Answer
I dont know what loop your instructor is talking about. To me your program looks like a good program and one you should congratulate yourself for. A few suggested improvements (maybe done /after/ you've shown the assignment to your instructor):
1. try to use string in place of char array. This would also mean you can take arbitrary length strings from user for name etc. char is too Cish, not C++. I think then you wont need so many cin.ignore calls too.
2. instead of writing one function, divide your program into functions, for example, one for taking input, one for computation rel to one employee and one for output. Pay attention to separation of concerns.
If you get to know about the "loop" do let me know.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Amit Kumar

Expertise

I can answer Cplusplus language and library questions, including STL, ACE, Boost. I have a good background in Mathematics.

Experience

Programming in CPP for about 8 years. Industry experience of 4 years.

Education/Credentials
Bachelors and Masters in Computer Science from Indian Institute of Technology Delhi.

©2016 About.com. All rights reserved.