calculator troubles

im making a calculator to which you should be able to input something like:
12+54 and get 66. im working only with addition and two numbers right now and am having trouble. right now its adding the digits of the numbers then adding them together. i dont understand how i can make it so that it makes the number first, and then converts it to an int value which is added to the total. please please please help :)
you dont really need to go through the code for this i just need ideas. here it is anyhow
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
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
char* eatspaces(char* str, int length, char* expression);
int addNumbers(char* str, int length, int num1, int num2);
int findLength(char* str, int length);

int main(void)
{
	const int max = 50;
	char equation[max];
	char* pequation = equation;
	cout << "Equation: " << endl;
	cin.getline(equation, 15, '\n');
	
	int length = 0;
	for (int i = 0; equation[i] != '\0'; i++)
		length++;
	char* expression = new char[length];
	expression = eatspaces(equation, length, expression);
	int num1Length = findLength(expression, length);
	int num2Length = num1Length + 2;
	int total = addNumbers(expression, length, num1Length, num2Length);

	cout << "total is: " << total << endl;

	delete[] expression;
	expression = NULL;
	pequation = NULL;
	return 0;
}

char* eatspaces(char* str, int length, char* expression)
{
	int spaceCounter = 0;
	for (int i = 0; i < length; i++)
	{
		if (str[i] == ' ')
			continue;
		else
			expression[spaceCounter] = str[i];
			spaceCounter++;
	}
	return expression;
}

int findLength(char* str, int length)
{
	int num1Length = 0;
	for (int i = 0; str[i] != '+'; i++)
		num1Length++;
	return num1Length;
}

int addNumbers(char* str, int length, int num1, int num2)
{
	int total = 0;		
	for (int i = 0; i < num1; i++)
		total += (str[i] - 48);
	num1++;
	for (; num1 < length; num1++)
		total += (str[num1] - 48);
	
	return total;
}
closed account (D80DSL3A)
One way is to use a stringstream object. Here is a sample code which works:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <sstream>
using namespace std;

int main()
{
	char exp[] = "12+54";// char array after spaces have been "eaten"
	stringstream ss(exp);// initialize stream with exp[]
	int x=0, y=0;// variables to stream exp into
	char oper='-';
	ss >> x >> oper >> y;// stream the values in

        // display contents of variables and find the total
	cout << "x = " << x << ", oper = " << oper << ", y = " << y << endl;
	cout << "Total = " << x+y << endl;
	return 0;
}
possibly but i feel like my prof would have taught us that before the assignment :o
I can't imagine your professor getting mad at you for using programming that he hasn't even taught yet unless he explicitly told you what you can and can not use.
Topic archived. No new replies allowed.