Sunday, 24 July 2016

Chapter 4 Exercise // 5 - 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.5

Write a program that performs as a very simple calculator. Your calculator should be able to handle the four basic math operations - add, subtract, multiply, and divide - on two input values. Your program should prompt the user to enter three arguments: two double values and a character to represent an operation. If the entry arguments are 35.6, 24.1 and '+', the program should be "The sum of 35.6 and 24.1 is 59.7. In Chapter 6 we look at a much more sophisticated simple calculator.


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


int main()
{
char loop = 'y';
double val1;
double val2;
char operation;

cout << "This is a simple calculator.\n";

while (loop == 'y')
{
cout << "Please enter your first numeric value: \n";
cin >> val1;
cout << "Please enter your second numeric value: \n";
cin >> val2;
cout << "Please enter an operation, '+', '-', '*' or '/': \n";
cin >> operation;

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

double add = val1 + val2;
double minus = val1 - val2;
double mult = val1*val2;
double div = val1 / val2;

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

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

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

val1 = 0;
val2 = 0;
operation = 0;
}

keep_window_open();

return 0;
}

This was quite easy but also made me smile because it was the first time I had just sat down, wrote the code I saw in my head and when I pressed build, there were no errors. It's the little things in life...

So, we pretty much built a calculator in Exercise 3.10 but I wanted this one to be a bit more sophisticated. I wanted it to loop if you wanted to continue using it or exit if you didn't. That's why I created the char 'loop'. At the end of the loop it will ask if you want to continue and reads a char into 'loop'. If it's 'y' you go round again until you enter 'n' and it will take you out of the loop. 

I also created variables to store the operations mainly because I didn't want the lines becoming too long and it just leaves less room for error.  Also, a switch statement is much cleaner in this instance than using if and actually makes the code easier to read.

No comments:

Post a Comment