Simple calculator problem

Hi guys.

So, i'm trying to make a simple calculator. Right now i'm only making the addition(+) version. The program runs, but some how it gives me the wrong answer. I have try to find it, but still can't figure it out. Can you please take a look, help me find where the problem is?

Thanx.

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
#include <iostream>
#include <string>
#include <math.h>
using namespace std;

void refresh()            // to clear the console
{
	int a;
	for(a=0;a<50;a++)
	{
		cout << endl;
	}
}


class calculator1            // where i store the result and add,get,set function
{
public:
	calculator1(){
		total = 0;
	}
	
	void add(double temp){
		total += temp;
	}

	int getTotal(){
		return total;
	}

	void setTotal(double temp){
		total = temp;
	}	

protected:
	double total;
};

void mode1()                              // calculator
{
	calculator1 object;
	string formula1;
	char formula2[1000];
	int stringLength;
	int a=0;
	double temp;
	
	cout << "Normal Calculator\n";
	cout << "-----------------\n\n\n";
	cout << "Input your formula : \n\n";
	cout << "Example : 1+2+3+4+5+6+7+8+9= \n\n\n";
	cout << "Input : ";

	cin >> formula1;
	stringLength = formula1.size();
	strcpy(formula2,formula1.c_str());

	while(a<stringLength)
	{
		if(a=0)
		{
			object.setTotal(formula2[a]);
			a++;
		}
		else if(formula2[a]=='+')
		{
			a++;
			object.add(formula2[a]);
			a++;

		}
		else if(formula2[a]=='=')
		{
			break;
		}
	}
	cout << "\n\n\nResult : " << object.getTotal();

	system("pause");
}


int main()          // the main only for menu
{
	int mode,flag;

	do{
		do{
			cout << " Menu\n";
			cout << " ----\n\n";
			cout << " 1. Normal Calculator[*,/,+,-]\n";
			cout << " 2. Exit\n\n";
			cout << " choose[1,2,3]:  ";
			cin >> mode;
		}while(mode<1 || mode>2);

		switch(mode)
		{
		case 1:
			mode1();
			cin.get();
			break;
		case 2:
			break;
		}

	}while(mode!=2);


	return 0;
}




Note : i use the class to make add function, and store the first number and the total every time i add numbers. The main function is only for menu.
Last edited on
There are lots of mistakes and problems , I don't think you are able to run your program with the code you posted.
here are some fixes that you should take into consideration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class calculator1{
public:
    calculator1(){ 
        total = 0; // i would use initailization list here.
    }	       // EX: "calculator1():total() or total(0){}"
	
    void add(double temp){
        total += temp;
    }

    int getTotal(){   // your return type is an int , but you actually 
	return total; //need to return a double since total is a double.
    }

    void setTotal(double temp){
	total = temp;
    }	

protected: // I would definatly make total a "private:".
	double total;
};



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
void mode1()
{
    calculator1 object;
    string formula1;
    char formula2[1000]; // you dont need this you could use index on string type object
    // unless you need to keep copy of the original input which you could use the assignment operator
    // (=) ... make another string like "string formula2;" then say "formula2 = formula1;"
    int stringLength; // always initialize data to get rid of garbage     
    int a=0;
    double temp = 0; //always initialize (get rid of garbage #) but
                     //  you do not need this object/data because you are not using it

    cout << "Normal Calculator\n";
    cout << "-----------------\n\n\n";
    cout << "Input your formula : \n\n";
    cout << "Example : 1+2+3+4+5+6+7+8+9= \n\n\n";
    cout << "Input : ";
    cin >> formula1;
    stringLength = formula1.size(); 
    strcpy(formula2,formula1); // don't need this since you could use assignment operator (=)
    
    while(a < stringLength){
	if(a=0){ // here you are assigning a to 0 , where i believe you meant to 
		 // compare a with 0 (a == 0)
	    object.setTotal(formula2[a]);  // here you just change "formula2[a]" to "formula1[a]"
	    a++; // you could just do this in the above statement "formula[a++];"
	}
	else if(formula2[a]=='+'){ // same here change "formula2[a]" to "formula1[a]"
            a++;
	    object.add(formula2[a]);
	    a++;
	}
	else if(formula2[a]=='='){ // you should mention to the user that if they want to quit, they
          //should end their statement with (=) sign
	    break;
	}
     }

    cout << "\n\n\nResult : " << object.getTotal();

    system("pause"); // do this in main not here since main is the last function running.
}
Last edited on
It seems that I need to learn more about string and initialization. Thank you for your help, I appreciate it. It helps a lot.
Last edited on
Here is the fix version. But, it still give me the wrong result. I can't find where the problem is. I make it simpler, and this time i use integer instead of double. But the total result still wrong. Please help me find the problem. Thank you.

Note :
when i input 1+2= , it gives me 99.
And the reason i use protected instead of private in the class, because i inherit the calculator class. The actual code is longer. But right now I'm trying to fix this problem first.

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
114
115
#include <iostream>
#include <string>
using namespace std;

void refresh()
{
	int a;
	for(a=0;a<50;a++)
	{
		cout << endl;
	}
}

class calculator
{
public:
	calculator()
	{
		total = 0;
	}
	
	void add(int temp)
	{
		total += temp;
	}

	int getTotal()
	{
		return total;
	}

	void setTotal(int temp)
	{
		total = temp;
	}	

protected:
	int total;
};

void mode()
{
	calculator object;
	string formula;
	int stringLength = 0;
	int a = 0;

	refresh();
	
	cout << "Normal Calculator\n";
	cout << "-----------------\n\n\n";
	cout << "Input your formula : \n\n";
	cout << "Example : 1+2+3+4+5+6+7+8+9= \n\n\n";
	cout << "Input : ";

	getline(cin,formula);
	cout << formula;
	stringLength = formula.size();

	while(a<stringLength)
	{
		if(a==0)
		{
			object.setTotal(formula[a]);
			a++;
		}
		else if(formula[a]=='+')
		{
			a++;
			object.add(formula[a]);
			a++;
		}
		else if(formula[a]=='=')
		{
			cout << "\nThe total is : " << object.getTotal() << endl;
			cin.get();
			break;
		}
	}

	refresh();
}

int main()
{
	int choose;

	do{
		do{
			cout << " Menu\n";
			cout << " ----\n\n";
			cout << " 1. Calculator[*,/,+,-]\n";
			cout << " 2. Exit\n\n";
			cout << " choose[1,2]:  ";
			cin >> choose;
			cin.clear();
			cin.sync();
		}while(choose<1 || choose>2);

		switch(choose)
		{
		case 1:
			mode();
			cin.get();
			break;
		case 2:
			break;
		}

	}while(choose!=2);

	

	return 0;
}




Last edited on
Here is your problem - you're reading in characters with the line:

getline(cin,formula);

so in your string variable "formula", you are saving the character representation of '1', '2', etc, not the actual values 1 and 2. In other words, if you type "1" into your computer, your computer doesn't actually save it as the integer value 1 - but as something else entirely!

Take a look at the ASCII table : http://www.asciitable.com/

This table explains how the computer internally stores all the characters we see on screen - the alphabet, numbers, punctuation marks, etc. The decimal representation ("dec") is on the far left of each column, and the character ("chr") is on the far right.

So if you take a look at the table, you'll notice the character "1" has a decimal value of 49, and the character "2" has a decimal value of 50. Therefore, it makes sense that when you pass the characters 1 and 2 into your "add" function, you are getting 99 (49+50).

What you need to do is convert the character values into integer values. Fortunately, this is a common thing in programming so C/C++ has a function that can do this for you - it's called "atoi." Google it to see how it works, what you need to include, etc.

I haven't tested this myself, but you need to change the line

object.add(formula[a]);

to something like (again, haven't tested it for syntax):

object.add( (atoi(formula[a]) );

I hope this helps!
Last edited on
@ ReedTompkins
it's called "atoi." Google it to see how it works, what you need to include, etc.


just a correction its "itoa" ... and cplusplus does mention in its reference area here is a link:
http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/

but unfortunately, C++ doesn't have such a function or at least i haven't seen it work.

Edit: you could use static_cast<data type> instead telling the compiler that is an int instead of char.
Last edited on
ReedTompkins, You are right. Thank you.
atoi and itoa are not the same thing.

atoi converts a character to integer, and is standard.

itoa converts an integer to a string, and is non-standard.

He wants to convert a character to an integer, so he should use atoi.

That being said, my code won't work,, since I forgot that atoi expects as input a const char *, while formula[a] is a char *.

One way to correct this is to cast from a char * to a const - casting refers to converting one data type to another.

1
2
3
4
5
6
        else if(formula[a]=='+')
        {
            a++;
            object.add( atoi( (const char*) formula[a]));
            a++;
        }


It's ugly, but it should work and I have to run :(
Topic archived. No new replies allowed.