Skip to content

Commit ff07f40

Browse files
committed
Adding comments
1 parent cdccad5 commit ff07f40

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

Calculator/CalculatorMain.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,29 @@ int main()
77
{
88
while (1)
99
{
10+
//Ask the user what they want
1011
std::cout << "1) Basic Math\n"
1112
<< "2) Average\n"
1213
<< "3) Max and Min\n"
1314
<< "4) Resistor Operations\n"
1415
<< "Enter your choice (-1) to quit:" << std::endl;
1516
int choice = GetIntFromUser(std::cout,std::cin);
17+
//If this is null at the end we do not delete it
1618
int* array = 0;
1719
int array_length;
20+
21+
//Places to hold the answers
1822
float answer;
1923
int max,min;
24+
25+
//If the error is still zero at the end then no error occured
2026
int error = 0;
2127

2228
switch (choice)
2329
{
2430
case 1:
2531
{
32+
//Ask the user what they want
2633
std::cout << "1) Addition\n"
2734
<< "2) Subtraction\n"
2835
<< "3) Multiplication\n"
@@ -31,6 +38,8 @@ int main()
3138
<< "Enter your choice: " << std::endl;
3239

3340
int op = GetIntFromUser(std::cout,std::cin);
41+
42+
//Get the operands
3443
std::cout << "Enter the operands: " << std::endl;
3544
int op1 = GetIntFromUser(std::cout,std::cin);
3645
int op2 = GetIntFromUser(std::cout,std::cin);
@@ -58,9 +67,13 @@ int main()
5867
default:
5968
return 0;
6069
}
70+
//If we got an array delete it
6171
if (array != 0) delete[] array;
72+
73+
//If there is no error print the answers
6274
if (!error)
6375
{
76+
//For a max and min we have two answers to display
6477
if (choice == 3)
6578
{
6679
std::cout << "Max: " << max << " Min: " << min << std::endl;
@@ -70,6 +83,7 @@ int main()
7083
std::cout << "The answer is: " << answer << std::endl;
7184
}
7285
}
86+
//An error occured
7387
else
7488
{
7589
std::cout << "An error occured, please try again...." << std::endl;

Utility/UserInput.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,43 @@
22
#include <cstdlib>
33

44

5+
//If the string is an integer then return true (else false)
56
bool isInteger(const std::string& str)
67
{
8+
//Blank strings are not integers
79
if ("" == str) return false;
10+
811
char* lastDigit;
12+
//strtol will set "lastDigit" to the char it stopped parsing at
913
std::strtol(str.c_str(), &lastDigit, 10);
14+
15+
16+
//If the last parsed char it parsed the whole string
1017
return *lastDigit == 0;
1118
}
1219
int GetIntFromUser(std::ostream& ostream, std::istream& stream)
1320
{
1421
std::string str;
1522
std::getline(stream,str);
23+
//Yell at the user until they enter the correct data
1624
while (!isInteger(str))
1725
{
1826
ostream << "Try again" << std::endl;
1927
std::getline(stream,str);
2028
}
29+
//Convert the string to an int
2130
return std::strtol(str.c_str(),0,10);
2231
}
2332
void GetIntArrayFromUser(std::ostream &ostream, std::istream &stream,
2433
int **array, int *length)
2534
{
2635
ostream << "Enter array length:" << std::endl;
36+
37+
//Allocate array the length asked for
2738
*length = GetIntFromUser(ostream,stream);
2839
*array = new int[*length];
2940

41+
//Populate the array
3042
ostream << std::string("Enter array:") << std::endl;
3143

3244
for (int x = 0; x < *length; x++)

Utility/UserInput.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
#ifndef UTILITY_USERINPUT_HPP
2+
#define UTILITY_USERINPUT_HPP
13
#include <iostream>
24
#include <sstream>
35
#include <string>
46

7+
//Gets a single integer from the user
58
int GetIntFromUser(std::ostream& ostream,std::istream &stream);
9+
10+
//Returns whether or not the string is an integer
611
bool isInteger(const std::string &str);
12+
13+
//Get an array from the user
714
void GetIntArrayFromUser(std::ostream &ostream, std::istream &stream,
815
int **array, int *length);
16+
#endif

0 commit comments

Comments
 (0)