Finding the problem in my simple calculator

Hey everyone,

I'm quite new to C++ and recently I've written a simple calculator. It can do simple additions, substractions, divisions and multiplications. Uptill this point it worked fine, however, when I put in the function for finding the square of a number, it stoped working. When I excecute the program, I can enter the first number, then the operation, even the second number, however, it won't show the result.
Could someone please help and explain to me what I did wrong? And any improvements would also be appreciated.

Thanks in advance!

The code I've used is:

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
//
// This is a simple calculator, capable of computing sums with: +,-,/,* and squares
//

#include <iostream>
using namespace std;

int main()
{
    //declare all variables used
    int first_number;
    int second_number;
    char operation;
    int go_on = 5;
    do
    {
        //welcome the user
        cout << "****** Time to do some basic math!******" << endl;
        cout << "________________________________________" << endl;
        cout << endl;
        
        // ask for the first input
        cout << "Please input the first number: " <<endl;
        cin >> first_number;
        
        // ask what operation the user wants to utilise (+,-,*,/, square)
        cout << "Please enter an operation (+,-,*,/, square (press: 2)): " << endl;
        cin >> operation;
        
        // ask for the last number if the operation is not to square the first number
        if(cin, operation == '+')
        cout << "Please input the seocond number: " << endl;
        cin >> second_number;
        
        if(cin, operation == '-')
        cout << "Please input the seocond number: " << endl;
        cin >> second_number;
        
        if(cin, operation == '*')
        cout << "Please input the seocond number: " << endl;
        cin >> second_number;
        
        if(cin, operation == '/')
        cout << "Please input the seocond number: " << endl;
        cin >> second_number;
              
        // excecute when the + operation is requested
        if( cin, operation == '+')
        cout << first_number << " + " << second_number << " = " << first_number+second_number << endl;
       
       // excecute when the - operation is requested
        if( cin, operation == '-')
        cout << first_number << " - " << second_number << " = " << first_number-second_number << endl;
        
        // excecute when the * operation is requested
        if( cin, operation == '*')
        cout << first_number << " * " << second_number << " = " << first_number*second_number << endl;
        
        // excecute when the / operation is requested
        if( cin, operation == '/')
        cout << first_number << " / " << second_number << " = " << first_number/second_number << endl;
        
        // execute when the square operation is requested
        if( cin, operation == '2')
        cout << first_number << " square " << " = " << first_number*first_number << endl;
        
        system ("pause");
        system ("cls");
        } while (go_on == 5 );
        return 0;
}
In this part:
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
        if(cin, operation == '+')
        cout << "Please input the seocond number: " << endl;
        cin >> second_number;
        
        if(cin, operation == '-')
        cout << "Please input the seocond number: " << endl;
        cin >> second_number;
        
        if(cin, operation == '*')
        cout << "Please input the seocond number: " << endl;
        cin >> second_number;
        
        if(cin, operation == '/')
        cout << "Please input the seocond number: " << endl;
        cin >> second_number;

cin >> second_number; will always be executed 4 times -an if not followed by braces affects only one line-
The cin in the condition does nothing.
You can simplify the whole section in few lines:
1
2
3
4
5
if ( operation == '+' || operation == '-' || operation == '/' || operation == '*' ) //     || = logical or
{
    cout << "Please input the second number: " << endl;
    cin >> second_number;
}

Last edited on
Ok, thank you verry much!

And by the way, could anyone explain to me how to create a fallback. E.g: someone makes a mistake by entering '/' instead of '*'. How do you make the program display a sort of error message and automatically go back to entering your operation?
Last edited on
You have to use a do while/while loop just like you have put your program in. Also, you may want to change the condition of your do-while loop because at the moment it will always exit your loop so there is no point in it being there. What if they want to run the program again without having to exit the program?
I'm new too, here is how I set up my basic calculator:

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
#include <iostream>

using namespace std;

int main()
{
	int result, num1, num2, choice;
	cout<<"Enter the numbers: "<<endl;
	cin>>num1>>num2;
	cout<<"\nPress 1 to add, 2 to subtract, 3 to multiply, 4 to divide: "<<endl;
	cin>>choice;
	switch(choice)
	{
	case 1:
		result = num1 + num2;
		cout<<num1<<" + "<<num2<<" = "<<result<<endl;
		break;
	case 2:
		result = num1 - num2;
		cout<<num1<<" - "<<num2<<" = "<<result<<endl;
		break;
	case 3:
		result = num1 * num2;
		cout<<num1<<" * "<<num2<<" = "<<result<<endl;
		break;
	case 4:
		if(num2 == 0)
			cout<<"Division by zero not allowed."<<endl;
		else
		{
			result = num1 / num2;
			cout<<num1<<" / "<<num2<<" = "<<result<<endl;
		}
		break;
	default:
		cout<<"Invalid input."<<endl;
	}
	return 0;
}
Kaduuk,

There is also a pow(); function which requires you to use #include <cmath>.
In case you wish to expand your calculator or to make things easier here's an example.

1
2
3
4
5
6
double num1, square;

cout << "Please enter the number to be squared: ";
cin >> num1;
square = pow(num1, 2);
cout << num1 << " squared = " << square << ".\n";
Although in this case it is probably unnecessary to use the function and would be easier to just multiply the value by itself: (num1*num1)
Xyriene,

I have expanded my calculator so that it can now do x raised to the power 0-9.
However, when I execute the program it gives strange numbers. E.g: 2 raised to the power 3 = 1.6978e-313

Also, I've changed the code a bit, so it's 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
    
    #include <cmath>

    double pow1, pow3, pow4, pow5, pow6, pow7, pow8, pow9;
    char exponent;

        // ask to what power the user would like to raise the first number
        if( cin, operation == '1');
        cout <<"Please enter to what power (0-9) " << first_number << " should be raised: " << endl;
        cin >> exponent;
        
        // execute when the exponent requested is 0
        if( cin, exponent == '0')
        cout << first_number << " to the power 0 = 1" << ". " << endl;
        
        // excecute when the exponent requested is 1
        if (cin, exponent == '1')
        pow1 = pow(first_number, 1);
        cout << first_number << " to the power 1 = "<< first_number << ". " << endl;
        
        // execute when the square operation is requested
        if( cin, exponent == '2')
        cout << first_number << " square " << " = " << first_number*first_number << ". " << endl;
        
        // execute when the exponent requested is 3
        if( cin, exponent == '3')
        pow3 = pow(first_number, 3);
        cout << first_number << " raised to the power 3 = " << pow3 << ". " << endl;
        
        // execute when the exponent requested is 4
        if( cin, exponent == '4')
        pow4 = pow(first_number, 4);
        cout << first_number << " raised to the power 4 = " << pow4 << ". " << endl;
        
        // execute when the exponent requested is 5
        if( cin, exponent == '5')
        pow5 = pow(first_number, 5);
        cout << first_number << " raised to the power 5 = " << pow5 << "." << endl;
        
        // execute when the exponent requested is 6
        if( cin, exponent == '6')
        pow6 = pow(first_number, 6);
        cout << first_number << " raised to the power 6 = " << pow6 << ". " << endl;
        
        // execute when the exponent requested is 7
        if( cin, exponent == '7')
        pow7 = pow(first_number, 7 );
        cout << first_number << " raised to the power 7 = " << pow7<< ". " << endl;
        
        // execute when the exponent requested is 8
        if( cin, exponent == '8')
        pow8 = pow(first_number, 8);
        cout << first_number << " raised to the power 8 = " <<pow8<< ". " << endl;    
        
        // execute when the exponent requested is 9
        if( cin, exponent == '9')
        pow9 = pow(first_number, 9);
        cout << first_number << " raised to the power 9 = " << pow9 << ". " << endl;


The squares are given the correct way (as are x to the power 0 and 1), however, the rest are wrong.
Also, for some reason it shows all the possible awnsers for each exponent, and even when you use an other operation, after giving the awnser, it asks to which power the number should be raised.
Last edited on
Please listen to the more experienced users when they give you advice: http://www.cplusplus.com/forum/beginner/18477/#msg94376
in otherwords ignore the less experienced until they get post count xxxx
in otherwords ignore the less experienced until they get post count xxxx

Not at all. My point is that the more experienced users don't just post for the sake of it; they post because they have advice to give. Even more so when that advice comes with a helpful code segment, and the post gets ignored, it is plain rude and ignorant.

Bazzy has explained to you why the cin statements are wrong and gave replacement code.
Sorry, I didn't realize that. Thanks for poniting it out!
Topic archived. No new replies allowed.