What is wrong with my calculator?

Hi there. I've just completed (I think) my first program, a calculator! :D
But it's not really completed because there is some errors in it I can't seem to fix so I don't even know if it works.
Could someone please check for errors in it because I can't find 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
110
111
112
113
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
	// Variables.
	string input;
	int result = 0;
	int tempResult = 0;
	bool firstRound = true;
	int arithOp = 0;
	vector<int> numbers;
	vector<int> tempNumbers;
	vector<char> arithmeticOperators;

	// The endless loop.
	while (true)
	{
		cout << "The incredible calculator!";
		cout << "\nInput: ";
		cin >> input;

		// Quit if input is "quit".
		if (input == "quit")
			break;

		// Add every arithmetic operator to the arithmeticOperators vector.
		for (int i = 0; i < input.size(); i++)
		{
			if ((input[i] == '+') || 
				(input[i] == '-') || 
				(input[i] == '*') || 
				(input[i] == '/'))
				arithmeticOperators.push_back(input[i]);
		}

		// Repeat for every character.
		for (int j = 0; j < input.size(); j++)
		{
			// Add number to tempNumbers if it isn't an arithmetic operator.
			if ((input[j] != '+') || 
				(input[j] != '-') || 
				(input[j] != '*') || 
				(input[j] != '/'))
			{
				tempNumbers.push_back(input[j]);
			}

			// If current character is an arithmetic operator.
			if ((input[j] == '+') || 
				(input[j] == '-') || 
				(input[j] == '*') || 
				(input[j] == '/'))
			{
				// This will make all numbers in tempNumber into one number.
				for (int k = 0; k < (tempNumbers.size() -1); k++)
				{
					tempResult += (tempNumbers[k] * 10);
					if (k == tempNumbers.end())
					{
						tempResult += tempNumbers.end();
					}
				}

				// This will happen the first round.
				if (firstRound == true)
				{
					result += tempResult;
					firstRound = false;
					tempNumbers.clear();
					continue;
				}

				// This will +, -, * or / tempNumbers to the result.
				while (firstRound == false)
				{
					if (arithmeticOperators[arithOp] == '+')
						result = result + tempNumbers;
					
					else if (arithmeticOperators[arithOp] == '-')
						result = result - tempNumbers;

					else if (arithmeticOperators[arithOp] == '*')
						result = result * tempNumbers;

					else if (arithmeticOperators[arithOp] == '/')
						result = result / tempNumbers;
				}
				
				// Clear stuff for the next numbers in input.
				tempResult = 0;
				tempNumbers.clear();
				arithOp++;
			}	
		}

		cout << "\nResult: " << result << endl;

		// Clear everything for a new round of awesome calculating.
		input = "";
		result = 0;
		tempResult = 0;
		firstRound = true;
		numbers.clear();
		tempNumbers.clear();
		arithmeticOperators.clear();
	}

	return 0;
}



I tried to make it so that it is possible to just input 34+542-3*43 for example and not just "input first number", "input arithmetic operator", "input second number". :)
Could someone please check for errors in it because I can't find them.


A handy thing about compilers. They not only tell you what the errors are, but they also tell you what file they're in and what line they're on. Most IDE's even let you double-click on the error and it will jump you right to the line in the code that has the problem.

The only catch is you have to actually read the error mesages.
I don't understand them :(
We do.

Post them for us so we can explain what they mean and how to correct them.
Line 62 you are trying to compare an int to an iterator.
Line 64 you are trying to add an int to an iterator and then store the result in an int.
Lines 81, 84, 87 and 90 you are trying to add an int to a vector.


Most of your problems seem to stem from not understanding what returns what.
tempNumbers.end() returns an iterator to a position that is 1 past the last element in your vector.

tempNumbers itself is a container with no real numerical value. You can't perform arithmetic on a container, just on its elements.

Last edited on
Thanks for the answers, I've fixed the errors so that it can start. But when i input something I get an error saying "Expression: vector iterator not dereferenceable".

What does that mean?

This is how the code looks like now:
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
113
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
	// Variables.
	string input;
	int result = 0;
	int tempResult = 0;
	bool firstRound = true;
	int arithOp = 0;
	vector<int> numbers;
	vector<int> tempNumbers;
	vector<char> arithmeticOperators;

	// The endless loop.
	while (true)
	{
		cout << "The incredible calculator!";
		cout << "\nInput: ";
		cin >> input;

		// Quit if input is "quit".
		if (input == "quit")
			break;

		// Add every arithmetic operator to the arithmeticOperators vector.
		for (int i = 0; i < input.size(); i++)
		{
			if ((input[i] == '+') || 
				(input[i] == '-') || 
				(input[i] == '*') || 
				(input[i] == '/'))
				arithmeticOperators.push_back(input[i]);
		}

		// Repeat for every character.
		for (int j = 0; j < input.size(); j++)
		{
			// Add number to tempNumbers if it isn't an arithmetic operator.
			if ((input[j] != '+') || 
				(input[j] != '-') || 
				(input[j] != '*') || 
				(input[j] != '/'))
			{
				tempNumbers.push_back(input[j]);
			}

			// If current character is an arithmetic operator.
			if ((input[j] == '+') || 
				(input[j] == '-') || 
				(input[j] == '*') || 
				(input[j] == '/'))
			{
				// This will make all numbers in tempNumber into one number.
				for (int k = 0; k < (tempNumbers.size() -1); k++)
				{
					tempResult += (tempNumbers[k] * 10);
					if (k == (*tempNumbers.end() - 1))
					{
						tempResult += (*tempNumbers.end() - 1);
					}
				}

				// This will happen the first round.
				if (firstRound == true)
				{
					result += tempResult;
					firstRound = false;
					tempNumbers.clear();
					continue;
				}

				// This will +, -, * or / tempNumbers to the result.
				while (firstRound == false)
				{
					if (arithmeticOperators[arithOp] == '+')
						result = result + tempNumbers[arithOp + 1];
					
					else if (arithmeticOperators[arithOp] == '-')
						result = result - tempNumbers[arithOp + 1];

					else if (arithmeticOperators[arithOp] == '*')
						result = result * tempNumbers[arithOp + 1];

					else if (arithmeticOperators[arithOp] == '/')
						result = result / tempNumbers[arithOp + 1];
				}
				
				// Clear stuff for the next numbers in input.
				tempResult = 0;
				tempNumbers.clear();
				arithOp++;
			}	
		}

		cout << "\nResult: " << result << endl;

		// Clear everything for a new round of awesome calculating.
		input = "";
		result = 0;
		tempResult = 0;
		firstRound = true;
		numbers.clear();
		tempNumbers.clear();
		arithmeticOperators.clear();
	}

	return 0;
}
 
if (k == (*tempNumbers.end() - 1))


This is equivalent to (parentheses added for emphasis):

if( k == ( (*tempNumbers.end() ) - 1 ) )

You are dereferencing end() which is not allowed (it results in undefined behavior).

You meant
 
if (k == *(tempNumbers.end() - 1))


which is the same thing as
 
if( k == tempNumbers.back() )

Thank you jsmith :)
It don't have any compile errors anymore, but it's not calculating right, 1+1 is aparently 490.

Can someone check what is wrong with my calculating part?
All the for loops and if stuff really confuses me even though I wrote it. I guess I'll just need some practice with it. :)

This is how the code is now:
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
113
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
	// Variables.
	string input;
	int result = 0;
	int tempResult = 0;
	bool firstRound = true;
	int arithOp = 0;
	vector<int> numbers;
	vector<int> tempNumbers;
	vector<char> arithmeticOperators;

	// The endless loop.
	while (true)
	{
		cout << "\nThe incredible calculator!";
		cout << "\nInput: ";
		cin >> input;

		// Quit if input is "quit".
		if (input == "quit")
			break;

		// Add every arithmetic operator to the arithmeticOperators vector.
		for (int i = 0; i < input.size(); i++)
		{
			if ((input[i] == '+') || 
				(input[i] == '-') || 
				(input[i] == '*') || 
				(input[i] == '/'))
				arithmeticOperators.push_back(input[i]);
		}

		// Repeat for every character.
		for (int j = 0; j < input.size(); j++)
		{
			// Add number to tempNumbers if it isn't an arithmetic operator.
			if ((input[j] != '+') || 
				(input[j] != '-') || 
				(input[j] != '*') || 
				(input[j] != '/'))
			{
				tempNumbers.push_back(input[j]);
			}

			// If current character is an arithmetic operator.
			if ((input[j] == '+') || 
				(input[j] == '-') || 
				(input[j] == '*') || 
				(input[j] == '/'))
			{
				// This will make all numbers in tempNumber into one number.
				for (int k = 0; k < (tempNumbers.size() -1); k++)
				{
					tempResult += (tempNumbers[k] * 10);
					if (k == tempNumbers.back())
					{
						tempResult += (*tempNumbers.end() - 1);
					}
				}

				// This will happen the first round.
				if (firstRound == true)
				{
					result += tempResult;
					firstRound = false;
					tempNumbers.clear();
					continue;
				}

				// This will +, -, * or / tempNumbers to the result.
				while (firstRound == false)
				{
					if (arithmeticOperators[arithOp] == '+')
						result = result + tempNumbers[arithOp + 1];
					
					else if (arithmeticOperators[arithOp] == '-')
						result = result - tempNumbers[arithOp + 1];

					else if (arithmeticOperators[arithOp] == '*')
						result = result * tempNumbers[arithOp + 1];

					else if (arithmeticOperators[arithOp] == '/')
						result = result / tempNumbers[arithOp + 1];
				}
				
				// Clear stuff for the next numbers in input.
				tempResult = 0;
				tempNumbers.clear();
				arithOp++;
			}	
		}

		cout << "Result: " << result << endl;

		// Clear everything for a new round of awesome calculating.
		input = "";
		result = 0;
		tempResult = 0;
		firstRound = true;
		numbers.clear();
		tempNumbers.clear();
		arithmeticOperators.clear();
	}

	return 0;
}
Er, ok, there are a lot of problems.

I found some of them, and it still doesn't work, so I'll let you to debug the rest.

First,

1
2
3
4
if ((input[j] != '+') || 
(input[j] != '-') || 
(input[j] != '*') || 
(input[j] != '/'))


is always true. You meant &&

second,
 
tempNumbers.push_back(input[j]);


input[j] has the value, for example, '0' which is decimal 48, hexadecimal 0x30.
You want the value you push back to be zero. You have to convert ASCII to decimal
by subtracting '0', or 0x30, or 48, whichever you like (I prefer the first).

third,
 
for (int k = 0; k < (tempNumbers.size() -1); k++)


this loop ignores the last character. you want k < tempNumbers.size(), not that - 1.


fourth,
 
tempResult += (tempNumbers[k] * 10);


that isn't mathematically right. you want tempResult = tempResult * 10;

fifth,
1
2
3
4
if (k == tempNumbers.back())
					{
						tempResult += (*tempNumbers.end() - 1);
					}


you can't compare k to back(). k is an index. back() is a value in the vector. it doesn't make sense to compare them.

further, the += line has the same problem as in my original reply.

Sorry, this is as far as I got.
Thanks for wasting time on me, jsmith :)
I started all over again and now I test that everything works before I continue :)

I need help with one thing.

This code is supposed to add every +, -, * and / to the arithmeticOperators vector, but I'm not sure if it does.
1
2
3
4
5
6
7
8
9
10
for (unsigned int i = 0; input.size() < i; i++)
{
	if (input[i] == '+' || 
		input[i] == '-' || 
		input[i] == '*' || 
		input[i] == '/')
	{
		arithmeticOperators.push_back(input[i]);
	}
}


When I check if there is something in the vector with this code, nothing shows up.
1
2
for (int h = 0; h < arithmeticOperators.size(); h++)
	cout << arithmeticOperators[h] << endl;


It works with numbers. Do I have to add a '0' to char's aswell?
You are testing the wrong condition in your for loop, it should be
 
for( vector<char>::size_type i = 0; i < input.size(); ++i )


You have the i variable and the size reversed.
Thanks alot, Tevsky :)
It works now, but maybe someone could explain me what vector<char>::size_type does? :)
It is essentially this:

1
2
3
4
5
template< ... >
class vector {
  public:
      typedef size_t size_type;
};


If you ever want to store a vector index or size in a variable, the type of the variable
should be vector<>::size_type to be the most correct. Most people just use int out
of laziness because on most platforms it happens to work for containers of less than
2 billion+ elements.

Make sure you use a good debugger and desk-check ("Step through") the code as you use live data. This will help pin point the error. A good debugger in invaluable. In this problem I would put several watches on the variables being used and watch how they change as you step through the program. A quick way to see where things are going wrong.

return 0;
Topic archived. No new replies allowed.