Code Allows Something Strange

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.

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
 #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.

More information: http://www.cplusplus.com/forum/general/69685/#msg372532
Topic archived. No new replies allowed.