This is what I have of my code so far
Where can I put these overloaded display functions?
// void display(const string &msg, char ch1, char ch2); // display letters and letter distance
// void display(const string &msg, double d1, double d2); // display numbers and number distance
// void display(const string &msg, double x1, double y1, double x2, double y2); // display points and point distance.
I need 3 overloaded input functions, 3 overloaded distance functions, and 3 overloaded display functions. Can you help provide the correct parameter type?:
//char input(parameter_type prompt_text, parameter_type error_message_text); // for getting a letter
//double input(parameter_type min_number, parameter_type max_number, parameter_type prompt_text, parameter_type error_message_text); // for getting a number
// void input(parameter_type x, parameter_type y, parameter_type prompt_text); // for getting a point (x, y)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <math.h>
using namespace std;
void printMenu(){
cout << "Options: l)etter; n)umber; p)oint; q)uit;" << endl;
}
void errorMessage(){
cout << "This is not an option, try again!" << endl;
}
//void printMenu() {cout << "Options: l)etter; n)umber; p)oint; q)uit; “;}
int main() {
//used for Letter option
char firstLetter, secondLetter;
//used for Point option
//distance used for all 3 options
double distance, x1, x2, y1, y2;
// used for Number option
double firstValue, secondValue;
// menu for options
enum menu { letter = 'l', number = 'n', point = 'p', quit = 'q' };
menu userChoice {};
//print menu options to user
printMenu();
while (userChoice != quit) {
char choice {};
std::cin >> choice;
userChoice = static_cast<menu>(choice);
switch (userChoice) {
case letter:
cout <<"Enter first letter (a to z): "<<endl;
cin >> firstLetter;
cout <<"Enter second letter (a to z): "<<endl;
cin >> secondLetter;
distance = secondLetter - firstLetter;
cout << "The distance between letters "<< firstLetter
<< " & " << secondLetter << " is: "<< distance <<endl;
break;
case number:
cout <<"Enter first value (-100, 100): "<<endl;
cin >> firstValue;
cout <<"Enter second value (-200, 200): "<<endl;
cin >> secondValue;
distance = secondValue - firstValue ;
cout << "The units between " << firstValue
<< " and " << secondValue << " = " << distance <<endl;
break;
case point:
cout <<"Enter the first point (x1): "<<endl;
cin >> x1;
cout <<"Enter the first point (y1): "<<endl;
cin >> y1;
cout << "Enter the second point (x2): "<<endl;
cin >> x2;
cout << "Enter the second point (y2): "<<endl;
cin >> y2;
distance = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
cout <<"The straight line distance between points ("
<< x1 <<","<< y1 << ")" << " and ("
<< x2 << ","<<y2<<") is: "
<< distance << " units." <<endl;
break;
case quit:
cout <<"Goodbye!"<<endl;
break;
// message for user if they input an invalid option from menu
default:
errorMessage();
break;
}
if (userChoice != quit) {
//print the menu options to user again
printMenu();
}
}
}
|
Overall my code must demostrate 6 skills:
1) Use functions. Functions sub-divide programming problems into independent parts.
2) Use function overloading, where several functions have the same name but different parameters.
3) Use pass-by-value and pass-by-reference parameters to functions.
4) Use static local variables, which work like global variables (values persist) and local variables (local scope). Their value persists between function calls, yet they can only be used within the function.
5) Use default parameters, which take on a default value if a parameter is not specified.
6) Use const parameters, which specify that the parameter cannot change. const reference parameters make good sense for strings.