How to stop my calculator from being able to be bugged

Hello, I have a little problem. It is possible to bug my calculator by entering '1 - 1' which will cause it to loop uncontrollably. I've set up an array containing the characters I would like to restrict but i have not found a way to implement that, need some help with that.

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
110
111
112
// Bucky.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <string.h>
using namespace std;

// Global Variables



// Calling Functions

int addition();
int subtract();
int multiply();
int divide();


// Classes -------------------------

class Calc {
public:
	int addition() {
		int x;
		int y;
		int sum;
		cout << "Enter a number" << endl;
		cin >> x;
		cout << "Add a number" << endl;
		cin >> y;
		sum = x + y;
		cout << x << " plus " << y << " is equal to: ";
		cout << sum << endl;
		return sum;
	}
	int subtract() {
		int x;
		int y;
		int sum;
		cout << "Enter a number" << endl;
		cin >> x;
		cout << "Subtract a number" << endl;
		cin >> y;
		sum = x - y;
		cout << x << " minus " << y << " is equal to: ";
		cout << sum << endl;
		return sum;
	}
	double multiply() {
		double x;
		double y;
		double sum;
		cout << "Enter a number" << endl;
		cin >> x;
		cout << "Multiply by a number" << endl;
		cin >> y;
		sum = x * y;
		cout << x << " multiplied by " << y << " is equal to: ";
		cout << sum << endl;
		return sum;
	}
	double divide() {
		double x;
		double y;
		double sum;
		cout << "Enter a number" << endl;
		cin >> x;
		cout << "Divide by " << endl;
		cin >> y;
		sum = x / y;
		cout << x << " divided by " << y << " is equal to: ";
		cout << sum << endl << endl;
		return sum;
	}

};

// Main

int main() {
	//Creating Objects
	Calc CalcObj;
	//Global Variables
	int input;
	char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'å', 'ä', 'ö', '!', '@', '#', '¤', '%', '&', '/', '(', ')', '=', '?', '*' } ;
	//Loop point for correct execution
	retry:
	cout << "Welcome to Sexyass Calculator using Objects and Classes!!!" << endl;
	cout << "1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n";
	//Loop point for incorrect execution
	start:
	cin >> input;
	if (input > 10000)
		goto start;
	else if (input == 1)
		CalcObj.addition();
	else if (input == 2)
		CalcObj.subtract();
	else if (input == 3)
		CalcObj.multiply();
	else if (input == 4)
		CalcObj.divide();
	else
		goto start;
	goto retry;
	return 0;
}

I'm not sure why 1-1 would crash your program. Might be good to get rid of your gotos though. They really don't help for clarity and stability.

[edit]

Wait, do you mean actually inputting '1-1', or choosing 2 (substraction), then 1 (x) and 1 (y)? The first will obviously crash because it's expecting a number, not other stuff.
Last edited on
Topic archived. No new replies allowed.