Author : =-=[ccoder]=-=-
Compiler : Microsoft Visual Studio 2008
Purpose : Display menu, Calculate sum of odd numbers, Calculate pay, Draw triangle
Limitations : No validation provided
----------------------------------------------------------------------------------------------------------------------*/
#include
#include
#include
using namespace std;
int main()
{
char choice;
char display_menu();
void pay_caculation();
void triangle_pattern();
void scaffold();
int odd_num_sum_cal();
do
{
choice = toupper(display_menu()); // call function to display menu
switch (choice)
{
case 'A': // select procedure to follow
scaffold();
break;
case 'B':
pay_caculation();
break;
case 'C':
triangle_pattern();
cout << endl << "C selected" << endl;
break;
case 'X':
break;
default:
cout << endl << "Invalid choice -- try again" << endl;
}
}while (choice != 'X'); // continue until exit selected
return 0;
}
char display_menu()
{
char choice;
// display menu
cout << endl << endl << " MAIN MENU" << endl << endl;
cout << "A. Sum of odd numbers " << endl;
cout << "B. Calculate pay " << endl;
cout << "C. Display Triangle Patterns " << endl;
cout << "X. Exit " << endl << endl;
cout << "Enter your choice : ";
cin >> choice;
return choice;
}
void scaffold()
{
char answer = 'y';
int number = 0;
int sum_of_odd = 0;
int odd_num_sum_cal(int number,int sum_of_odd);
do
{
int sum_of_odd = 0;
cout << "Please enter a number :";
cin >> number;
sum_of_odd = odd_num_sum_cal(number,sum_of_odd);
cout << "\n" << "The sum of the odd numbers is : " << sum_of_odd;
cout << "\n" << "Do you want to enter another number (Y/N) ?";
cin >> answer;
}while (toupper(answer)== 'Y');
}
int odd_num_sum_cal(int number,int sum_of_odd)
{
for (int x=1; x <= number ; x++)
{
if (x % 2 == 1)
{
sum_of_odd = sum_of_odd + x;
}
}
return sum_of_odd;
}
void pay_caculation()
{
int number_days = 0;
const int MAX = 20;
double day_salary = 0.01;
double total_pay = 0.01;
int x = 1;
cout << "Enter number of days: ";
cin >> number_days;
if (number_days <= MAX && number_days > 0)
{
cout << "DAY" << setw(19) << "PAY" << setw(25) << "TOTAL PAY" << endl;
do
{
cout << setprecision(2) << fixed;
cout << setw(3)<< x << setw(19) << day_salary << setw(25) << total_pay << endl;
day_salary = day_salary * 2;
total_pay = total_pay + day_salary;
x++;
}while(x <= number_days && x <= MAX);
cout << endl << "Number of days worked: " << number_days << endl;
}
else
{
cout << "Enter a number of worked days from 1 to 20 days max !" << endl;
}
system("pause");
}
void triangle_pattern()
{
char letter;
char input_letter;
cout << "Enter a letter: ";
cin >> input_letter;
input_letter = toupper(input_letter);
for (int row = 0; row <= (input_letter - 'A'); row++)
{
letter = input_letter;
for (int col = 0; col <= row; col++)
{
cout << letter;
letter = letter-1;
}
cout << endl;
}
system("pause");
}
No comments:
Post a Comment