I decided to take up learning C++ (as I'm sure has been said an untold amount of times). For my first (but unoriginal) project, I decided to create a CLI Simple Arithmetic Calculator (1+1, 2-1, 3*1, 4/1). This is the logical description of my program:
* Display program title and available options to user
* Request input from user to select an option
- Return error if input is invalid and give another chance
* Perform arithmetic based on valid input
- Request input from user for calculation
- Return error if input is invalid and give another chance
- Return arithmetic calculation based on valid input
* Loop until user inputs exit option
Here is the extremely messy code I have come up with:
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
|
#include <iostream>
#include <string>
using namespace std;
//--- Global Variables
double a = 0;
double b = 0;
double c = 0;
int option = 0;
//--- End Global Variables
//--- Function Prototypes
void userOptions();
void optionLoop();
void validOptionCheck();
void validMathInputCheck();
double addition();
double subtraction();
double multiplication();
double division();
//--- End Function Prototypes
//--- Main
int main()
{
// Program Title
cout << "Simple Arithmetic Calculator \n" << endl;
// Display User Options, Ask for Input, and Validate
userOptions();
validOptionCheck();
optionLoop();
return 0;
}
//--- End Main
//--- Function Definitions
void userOptions()
{
// Ask For User Input
cout << "What would you like to do?: \n\n";
// User Options
cout << "1. Add \n"
<< "2. Subtract \n"
<< "3. Multiply \n"
<< "4. Divide \n"
<< "5. Exit Program \n\n";
// Get User Input
cin >> option;
cout << "\n";
}
void optionLoop()
{
// While-Loop for Continuity Until Exit
while(option != 5)
{
// Switch Case for Options
switch(option)
{
case 1:
if(option == 1)
{
cout << "What numbers would you like to add?\n";
cout << "\n";
cin >> a >> b;
cout << "\n";
validMathInputCheck();
cout << a << " + " << b << " = " << addition() << "\n\n";
// Show User Options and Get User Input
userOptions();
validOptionCheck();
}
break;
case 2:
if(option == 2)
{
cout << "What numbers would you like to subtract?\n";
cout << "\n";
cin >> a >> b;
cout << "\n";
validMathInputCheck();
cout << a << " - " << b << " = " << subtraction() << "\n\n";
// Show User Options and Get User Input
userOptions();
validOptionCheck();
}
break;
case 3:
if(option == 3)
{
cout << "What numbers would you like to multiply?\n";
cout << "\n";
cin >> a >> b;
cout << "\n";
validMathInputCheck();
cout << a << " * " << b << " = " << multiplication() << "\n\n";
// Show User Options and Get User Input
userOptions();
validOptionCheck();
}
break;
case 4:
if(option == 4)
{
cout << "What numbers would you like to divide?\n";
cout << "\n";
cin >> a >> b;
cout << "\n";
validMathInputCheck();
cout << a << " / " << b << " = " << division() << "\n\n";
// Show User Options and Get User Input
userOptions();
validOptionCheck();
}
break;
}
}
}
void validOptionCheck()
{
while(!option)
{
cout << "Invalid input. Please try again." << endl;
cin.clear();
cin.ignore(100,'\n');
cin >> option;
cout << "\n";
}
}
void validMathInputCheck()
{
while (!a || !b)
{
cout << "Error detected! Only numerical values are allowed." << endl;
cin.clear();
cin.ignore(100,'\n');
cin >> a >> b;
cout << "\n";
}
}
double addition()
{
c = (a + b);
return c;
}
double subtraction()
{
c = (a - b);
return c;
}
double multiplication()
{
c = (a * b);
return c;
}
double division()
{
c = (a / b);
return c;
}
//--- End Function Definitions
|
So far, the program works MOSTLY as expected. I used double for the variables for calculation so that users can return decimals as results. However, from the code I have now, there is an infinite loop somewhere that I just can't see. I've tried taking breaks and coming back, but I can't find it. The loop occurs when I enter invalid characters for calculation (ex. instead of entering two numbers, I do letters). On the first pass, I receive the correct error response and my code works as intended. However, when I try again, it loops infinitely (I don't know if what I'm saying is making sense, so forgive me).
Anyway, I'm opening myself up for some coding tips/insight. Also, is it normal to get so wrapped up in your code that it's hard to find mistakes? Does this change with experience?