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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
#include <iostream>
// outside functions
int prompt ();
int prompt2 ();
int noobs ();
void values ();
void addition ();
void subtraction ();
void multiplication ();
void division ();
int repeat ();
int main ()
{
int again { 1 };
while(again) {
// prompt the user
int choice {};
if (choice == 0) {
choice = prompt ();
} else {
choice = prompt2 ();
}
switch(choice) {
case 1:
addition ();
break;
case 2:
subtraction ();
break;
case 3:
multiplication ();
break;
case 4:
division ();
break;
default:
break;
}
again = repeat (); // can return only 0 or 1
}
}
// Display initial menu
int prompt ()
{
std::cout << "CCCC A L CCCCC + \n"
"C A A L C + \n"
"C AAAAA L C +++++ \n"
"C A A L C + \n"
"CCCC A A LLLLL CCCCC + \n"
"______________________________\n"
"Welcome to Calc+, your friendly neighborhood calculator.\n"
"We are able to calculate the basic 4 functions add (1), "
"subtract (2), multiply (3), and divide (4).\n"
"What would you like to do? "
"(please enter a number equal to your chosen equation): ";
int choice;
std::cin >> choice;
while (choice < 1 || 4 < choice) {
choice = noobs ();
}
return choice;
}
// Display second menu
int prompt2 ()
{
std::cout << "What would you like to do?\n"
"Add (1), Subtract (2), Multiply (3), or Divide (4)\n"
"Please enter a number equal to your chosen equation: ";
int choice;
std::cin >> choice;
while (choice < 1 || choice > 4) {
choice = noobs ();
}
return choice;
}
// noobs function
int noobs ()
{
std::cout << "ERROR: Please enter a value between 1 and 4, "
"for our functions options;\n"
"Add is 1\n"
"Subtract is 2\n"
"Multiply is 3\n"
"Divide is 4\n"
"Please pick one of the above options: ";
int choice;
std::cin >> choice;
return choice;
}
// values function
void values (double & first, double & second)
{
std::cout << "What is the first value you would like to use in your equation? ";
std::cin >> first;
while (first == 0.0) {
std::cout << "Sorry, your first value needs to be a number: ";
std::cin >> first;
}
std::cout << "What is the second value you would like to use in your equation? ";
std::cin >> second;
while (second == 0.0) {
std::cout << "Sorry, your second value needs to be a number: ";
std::cin >> second;
}
}
// addition function
void addition ()
{
double first {};
double second {};
int choice {};
while(choice != 1) {
values(first, second);
std::cout << "So what I have is " << first << " plus " << second
<< ".\nIs that correct? (1 if yes, 2 if no): ";
std::cin >> choice;
if(choice == 2) {
std::cout << "Sorry about that, let's try again.\n";
continue;
}
std::cout << first << " + " << second << " = " << first + second << '\n';
}
}
// subtraction function
void subtraction ()
{
double first {};
double second {};
int choice {};
while ( choice != 1) {
values (first, second);
std::cout << "So what I have is " << first << " minus " << second
<< ".\nIs that correct? (1 if yes, 0 if no): ";
std::cin >> choice;
while (choice < 0 || choice > 1) {
std::cout << "Please enter an appropriate choice, 1 (yes) or 0 (no): ";
std::cin >> choice;
}
if(choice == 0) { continue; }
std::cout << first << " - " << second << " = " << first - second << '\n';
}
}
// multiplication function
void multiplication ()
{
double factor1;
double factor2;
values (factor1, factor2);
int choice {};
while ( choice != 1) {
std::cout << "So what I have is " << factor1 << " multiplied by " << factor2
<< ".\nIs that correct? (1 if yes, 0 if no): ";
std::cin >> choice;
while (choice < 0 || choice > 1) {
std::cout << "Please enter an appropriate choice, 1 (yes) or 0 (no): ";
std::cin >> choice;
}
if(choice == 0) { continue; }
std::cout << factor1 << " * " << factor2 << " = " << factor1 * factor2 << '\n';
}
}
// division function
void division ()
{
double dividend;
double divisor;
values (dividend, divisor);
int choice {};
while ( choice != 1) {
std::cout << "So what I have is " << dividend << " divided by " << divisor
<< ".\nIs that correct? (1 if yes, 0 if no): ";
std::cin >> choice;
while (choice < 0 || choice > 1) {
std::cout << "Please enter an appropriate choice, 1 (yes) or 0 (no): ";
std::cin >> choice;
}
if(choice == 0) { continue; }
std::cout << dividend << " / " << divisor << " = " << dividend / divisor << '\n';
}
}
// Ask the user if they want another calculation
int repeat ()
{
std::cout << "Would you like to do another math problem?\n"
"Press 1 for yes, or 0 for no: ";
int choice;
std::cin >> choice;
while (choice < 0 || choice > 1) {
std::cout << "Please enter an appropriate value:";
std::cin >> choice;
}
return choice;
}
|