Here's a simple school assignment I did:
Problem 1: Write a program that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character “X”. The number entered by the user will be the length of the side of the square. For example, if the user enters 5, the program should display the following:
XXXXX XXXXX XXXXX XXXXX XXXXX
Problem 2: Imagine that you and a number of friends go to a restaurant and when you ask for the bill you want to split the amount and the tip between all. Write a function
double CalculateAmountPerPerson(double TotalBill, double TipPercentage, int NumFriends)
that takes the total bill amount, tip percentage (e.g., 15.0 for a 15% tip), and the number of friends as inputs and returns the total bill amount as its output.
Write a main function that asks the user for the total amount of the bill and the size of his/her party (i.e., number of friends) and prints out the total amount that each person should pay for tip percentages of 10%, 12.5%, 15%, 17.5%, 20%, 22.5%, 25%, 27.5%, and 30%. Your main function should use a loop and invoke the CalculateAmountPerPerson function at each iteration.
My code:
#include <iostream>
#include <iomanip>
void menuPrompt();
short getMenuSelection();
void program1();
void squareLengthPrompt();
void displaySquare(int, char);
void program2();
void billPrompt();
void numPeoplePrompt();
double CalculateAmountPerPerson(double, double, int);
const short PROGRAM_1 = 1;
const short PROGRAM_2 = 2;
const short EXIT = 0;
int main(int argc, char *arv[]) {
while(true) {
char menu = getMenuSelection();
switch (menu) {
case EXIT:
exit(EXIT_SUCCESS);
case PROGRAM_1:
program1();
break;
case PROGRAM_2:
program2();
break;
default:
std::cout << "That program doesn\'t exist." << std::endl;
break;
}
}
return 0;
}
void menuPrompt() {
std::cout
<< "Menu:\n"
<< "\t1. Program 1\n"
<< "\t2. Program 2\n"
<< "\t0. Exit\n"
<< "Select your program: "
<< std::flush;
}
short getMenuSelection() {
short selection = 0;
while (selection != PROGRAM_1 && selection != PROGRAM_2) {
menuPrompt();
std::cin >> selection;
}
return selection;
}
void program1(){
const char SQUARE_CHARATER = 'X';
short squareLength = 0;
while (squareLength > 15 || squareLength < 1) {
squareLengthPrompt();
std::cin >> squareLength;
}
displaySquare(squareLength,SQUARE_CHARATER);
}
void squareLengthPrompt() {
std::cout
<< "Enter the length of the side of the square (Between 1 and 15): "
<< std::flush;
}
void displaySquare(int side, char character) {
for(int i = 0; i < side ; ++i) {
for (int j = 0; j < side; ++j) {
std::cout << character;
}
std::cout << std::endl;
}
}
void program2() {
const float TIP_PERCENTAGES[] = {.10, .125, .15, .175, .20, .225, .25, .275, .30};
double totalBill = 0;
int totalPeople = 0;
double amountPerPerson;
while (totalBill <= 0) {
billPrompt();
std::cin >> totalBill;
}
while (totalPeople <= 0) {
numPeoplePrompt();
std::cin >> totalPeople;
}
for(auto tipPercent: TIP_PERCENTAGES){
amountPerPerson = CalculateAmountPerPerson(totalBill,tipPercent, totalPeople);
std::cout << "With the tip percentage of " << std::fixed << std::setprecision(2)
<< tipPercent*100 << "%, each person pays " << amountPerPerson
<< " from a $" << totalBill << " bill. " << std::endl;
}
}
double CalculateAmountPerPerson(double TotalBill, double TipPercentage, int
NumFriends) {
return (TotalBill*(1+TipPercentage))/NumFriends;
}
void billPrompt() {
std::cout
<< "Enter the total of your bill (must be greater than 0): "
<< std::flush;
}
void numPeoplePrompt() {
std::cout
<< "Enter the number people that are splitting the bill (must be greater than 0): "
<< std::flush;
}
I primarily just want to know if the code is self documenting and if I should include comments.