Hi. I've coded a calculator that I continuously add onto as I learn new things. However, I recently noticed (After trying to see how the calculator would handle unexpected inputs), that by putting a space between numbers, it would take the second number as an input for another variable.
To fully understand this I'll explain how the calculator works. It asks for a number, then once you enter it, it asks for another number, then it asks for an operation and solves. It looks like this -
Pick Your First Number:
Pick Your Second Number:
Choose Your Operation:
However, if in that very first input request (Pick Your First Number) - I can type it "12 12 +" and it'll speed through the other two texts and give me the answer.
Now, my question is why does it do this? It's not really a bad thing, can even speed up the process of calculating ( I might end up switching the sin::cin for operation and y value so instead of having to type in "12 12 +" one can type in "12 + 12". But it's still puzzling me why it accepts the information as input for other values when given a space between them.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
double a;
double x;
double y;
char z;
int subtract()
{
a = x - y;
return a;
}
int add()
{
a = x + y;
return a;
}
int divide()
{
a = x / y;
return a;
}
int multiply()
{
a = x * y;
return a;
}
int power()
{
a = pow(x, y);
return a;
}
int main()
{
std::cout << "Pick Your First Number: ";
std::cin >> x;
if (std::cin.fail())
{
std::cout << "Not A Number" << std::endl;
return (0);
}
std::cout << "Pick Your Second Number: ";
std::cin >> y;
if (std::cin.fail())
{
std::cout << "Not A Number" << std::endl;
return (0);
}
std::cout << "Choose Your Operation: ";
std::cin >> z;
switch (z)
{
case'-':
subtract();
std::cout << "The Sum Is: " << std::setprecision(7) << a << std::endl;
break;
case'+':
add();
std::cout << "The Sum Is: " << std::setprecision(7) << a << std::endl;
break;
case'/':
divide();
if (x == 0)
{
std::cout << "Not Possible" << std::endl;
break;
}
std::cout << "The Sum Is: " << std::setprecision(7) << a << std::endl;
break;
case'*':
multiply();
std::cout << "The Sum Is: " << std::setprecision(7) << a << std::endl;
break;
case'^':
power();
std::cout << "The Sum Is: " << std::setprecision(7) << a << std::endl;
break;
case'%':
if (x < y)
{
std::cout << "Not Applicable" << std::endl;
}
else
{ // Won't Work If Not Placed Here. Can't Perform % With Anything But Integers.
int b = x;
int c = y;
a = b % c;
std::cout << "The Remainder is: " << a << std::endl;
}
break;
default:
std::cout << "Unknown Operation: '" << z << "'\n";
}
system("pause");
return 0;
}
I figured out the std::cin doesn't handle white space - So I suppose that's why it would jump. I edited the code to make the calculator more user friendly by taking advantage of this.
There are two different kinds of input operations: formatted input and unformatted input.
Formatted input discards leading white space, and stops reading when it encounters a white space (or invalid characters in input). Unformatted input on the other hand, does not discard leading whitespace.