Week 7 In-Class Examples

Program with global variables and a function with no parameters or returned value:

#include <iostream>
using namespace std;

int hours;
float payrate;
float grosspay;

void calcpay(void);

int main(void)
{
   calcpay();

  cout << "Total pay is " << grosspay << endl;
   return 0;
} // end main function

void calcpay(void)
{
  do {
     cout << "Hours ? ";
     cin >> hours;
     if (hours < 0 || hours > 100) {
        cout << "Out of range\n";
     }
  } while (hours < 0 || hours > 100);

  do {
     cout << "Pay Rate? ";
     cin >> payrate;
     if (payrate < 8 || payrate > 40) {
        cout << "Out of range\n";
     }
  } while (payrate < 8 || payrate > 40);

  grosspay = hours * payrate;


} // end of calcpay function

Program with local variables and a function with no parameters or returned value:

#include <iostream>
using namespace std;

void calcpay(void);

int main(void)
{
   calcpay();
   return 0;
} // end main function

void calcpay(void)
{
   int hours;
   float payrate;
   float grosspay;

  do {
     cout << "Hours ? ";
     cin >> hours;
     if (hours < 0 || hours > 100) {
        cout << "Out of range\n";
     }
  } while (hours < 0 || hours > 100);

  do {
     cout << "Pay Rate? ";
     cin >> payrate;
     if (payrate < 8 || payrate > 40) {
        cout << "Out of range\n";
     }
  } while (payrate < 8 || payrate > 40);

  grosspay = hours * payrate;

  cout << "Total pay is " << grosspay << endl;

} // end of calcpay function

Program with local variables and a function with no parameters with a returned value from the function:

#include <iostream>
using namespace std;

float calcpay(void);

int main(void)
{
   float grosspay;
   grosspay = calcpay();
   cout << "Total pay is " << grosspay << endl;
   return 0;
} // end main function

float calcpay(void)
{
   int hours;
   float payrate;
   float grosspay;

  do {
     cout << "Hours ? ";
     cin >> hours;
     if (hours < 0 || hours > 100) {
        cout << "Out of range\n";
     }
  } while (hours < 0 || hours > 100);

  do {
     cout << "Pay Rate? ";
     cin >> payrate;
     if (payrate < 8 || payrate > 40) {
        cout << "Out of range\n";
     }
  } while (payrate < 8 || payrate > 40);

  grosspay = hours * payrate;

  return grosspay;
} // end of calcpay function

Program with local variables and functions with parameters and returned values:

#include <iostream>
using namespace std;

float calcpay(int hours,float payrate);
int gethours(void);
float getpay(void);

int main(void)
{
   int hours;
   float payrate;
   float grosspay;
   hours = gethours();
   payrate = getpay();
   grosspay = calcpay(hours,payrate);
   cout << "Total pay is " << grosspay << endl;
   return 0;
} // end main function

int gethours(void)
{
  int hours;
  do {
     cout << "Hours ? ";
     cin >> hours;
     if (hours < 0 || hours > 100) {
        cout << "Out of range\n";
     }
  } while (hours < 0 || hours > 100);

  return hours;
}

float getpay(void)
{
  float payrate;
  do {
     cout << "Pay Rate? ";
     cin >> payrate;
     if (payrate < 8 || payrate > 40) {
        cout << "Out of range\n";
     }
  } while (payrate < 8 || payrate > 40);

  return payrate;
}

float calcpay(int hrs, float payr)
{
  float grosspay;
  grosspay = hrs * payr;
  return grosspay;

} // end of calcpay function