Thursday, 28 July 2016

Chapter 4 Exercise // 7 - Principles & Practice Using C++

In all these exercises I am using Visual Studio Community 2015 and the header file "std_lib_facilities.h" which can be found here:


http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h


My version is spelt differently so adjust the code accordingly if copying and pasting.


Chapter 4 Exercise // 4.7

Modify the "mini calculator" from exercise 5 to accept (just) single-digit numbers written as either digits or spelled out.

#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;

vector<string> values1{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
vector<string> values2{ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

char op; //the operation

//this function gets our digits
int get_digit()
{
int d_1 = 10; //this will test for bad input

cout << "Please enter a single digit 0 - 9 in numeric form or written (lower case): \n";
string digit;
cin >> digit;

//this checks for bad input on digit and loops till it's correct
while (d_1 != 0 && d_1 != 1 && d_1 != 2 && d_1 != 3 && d_1 != 4 && d_1 != 5 && d_1 != 6 && d_1 != 7 && d_1 != 8 && d_1 != 9)
{
for (int i = 0; i < values1.size(); ++i)
{
//this is for numeric input
if (digit == values1[i])
{
d_1 = i;
return i;
}

//this is for written input
else if (digit == values2[i])
{
d_1 = i;
return i;
}
}

if (d_1 != 0 && d_1 != 1 && d_1 != 2 && d_1 != 3 && d_1 != 4 && d_1 != 5 && d_1 != 6 && d_1 != 7 && d_1 != 8 && d_1 != 9)
{
cout << "Sorry, incorrect input. Please try again: \n";
cin >> digit;
}
}

d_1 = 10; //reset back to ten for future use
}

//this function gets the operation
char get_op()
{
cout << "Please enter an operation from, +, -, *, /: \n";
cin >> op;

while (op != '+' && op != '-' && op != '*' && op != '/')
{
cout << "Sorry, that operation is not recognised. Please try again: \n";
cin >> op;
}

return op;
}

int main()
{
char loop = 'y';

cout << "This program is a calculator for single digits.\n";

while (loop == 'y')


d1 = get_digit(); //first number
op = get_op(); //this will get the operation
d2 = get_digit(); //second number

double add = d1 + d2;
double minus = d1 - d2;
double mult = d1*d2;
double div = d1 / d2;

switch (op)
{
case '+':
cout << "The sum of " << d1 << " and " << d2 << " is: " << add << endl;
break;
case '-':
cout << d1 << " minus " << d2 << " is: " << minus << endl;
break;
case '*':
cout << d1 << " multiplied by " << d2 << " is: " << mult << endl;
break;
case '/':
cout << d1 << " divided by " << d2 << " is: " << div << endl;
break;
}

cout << "\nWould you like to use the calculator again? y / n\n";
cin >> loop;

while (loop != 'y' && loop != 'n')
{
cout << "Sorry, not recognised. Try again. y / n: \n";
cin >> loop;
}
}

keep_window_open();

return 0;
}

This one took me a few hours. I originally started off with it all in main, using if-statements for bad input and converting the input into an actual number. There was also code to get a second digit and it worked but it ran into quite a few lines of code and looked quite ugly. So I sat there and pondered on how I could make it more streamline. 

By turning the process of getting a digit into a function outside main, I could then just call that whenever necessary and by turning the process of getting the number into a while loop, the function also checks itself for bad input so you don't have to do it in main.

I then turned getting the op into a function, just to take it out of main and make it more readable. The last bit was then just a copy and paste job from the exercise mentioned.

The hardest part of all of this was getting it to check itself for bad input, the upside of that is I now understand while loops a hell of a lot more and I'm starting to feel much more comfortable using them.

No comments:

Post a Comment