/*******************************************************/
//R.A. Hillayrd
//fun2.cpp
//Last Modified 10/08/2001
//program to use demonstrate basics of user defined functions
/*******************************************************/
#include<iostream>
#include<cctype> //for toupper
using namespace std;
//function prototypes
char printMenu(); //print menu of choices - return choice
int min(int a, int b); //return the smallest of two integers
void printChar(char ch, int num); //print a line of n characters
void doChoice(char ch); //carry out user request
int main()
{
char choice; //char variables for user input
while(true)
{
choice = printMenu(); //print menu
if(choice == 'Q') //exit loop if quitting
break;
else
doChoice(choice);
}//end while flag
return 0;
}//end main
/***********************************************************/
char printMenu()
{
char choice;
do
{
cout << "\nWelcome to the function demo program\n";
cout << "------------------------------------\n";
cout << "Choose one of the following\n";
cout << " S find the smallest of two integers\n";
cout << " P print a line of characters\n";
cout << " Q To quit\n\n";
cout << "Enter your choice: ";
cin >> choice;
choice = toupper(choice);
}while(choice != 'S' && choice != 'P' && choice != 'Q');
return choice;
}
/***********************************************************/
//carry out user request
/***********************************************************/
void doChoice(char ch)
{
int a,b; //integer variables for user input
char choice;
switch(ch)
{
case 'S' : cout << "Enter two integers: ";
cin >> a >> b;
cout << "The Smallest of " << a << " and " << b
<< " is " << min(a,b) <<endl;
break;
case 'P' : cout << "Enter a character and number of times: ";
cin >> choice >> a;
printChar(choice,a);
break;
default : cout << choice << " is undefined\n";
}//end switch
}
/***********************************************************/
/***********************************************************/
//return the smallest of two integers
/***********************************************************/
int min(int var1, int var2)
{
if(var1 <= var2)
return var1;
return var2;
}
/***********************************************************/
//prints a line of characters
/***********************************************************/
void printChar(char ch, int num)
{
for(int j = 0; j < num; j++)
{
cout << ch << " ";
}
cout << endl;
}
/***********************************************************/
/*********************Program Output************************
Welcome to the function demo program
------------------------------------
Choose one of the following
S find the smallest of two integers
P print a line of characters
Q To quit
Enter your choice: S
Enter two integers: 88 -88
The Smallest of 88 and -88 is -88
Welcome to the function demo program
------------------------------------
Choose one of the following
S find the smallest of two integers
P print a line of characters
Q To quit
Enter your choice: P
Enter a character and number of times: * 12
* * * * * * * * * * * *
Welcome to the function demo program
------------------------------------
Choose one of the following
S find the smallest of two integers
P print a line of characters
Q To quit
Enter your choice: q
*/