Calculator for decimals

I made a calculator but need help making it use decimals instead of just whole numbers. What do I need to add to my code?

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

using namespace std;

int number1;
int number2;
char symbol;

int main()
{
	

	cout << "Welcome to my calculator\n"
		<< "Please enter a symbol to indicate which type of problem we will be solving\n"
		<< "The choices are +, -, /, *, or % to find the modulus of two numbers\n";
	cin >> symbol;


	if (symbol == '+')
	{
		cout << "Please enter two numbers\n";
		cin >> number1 >> number2;

		cout << number1 << " plus " << number2 << " is " << number1 + number2 << endl;
	}
		
		
	else if (symbol == '-')
	{
		cout << "Please enter two numbers\n";
		cin >> number1 >> number2;


		cout << number1 << " minus " << number2 << " is " << number1 - number2 << endl
			<< number2 << " minus " << number1 << " is " << number2 - number1 << endl;
	}

	else if (symbol == '/')
	
	{
		cout << "Please enter two numbers\n";
		cin >> number1 >> number2;

		cout << number1 << " divided by " << number2 << " is " << number1 / number2 << endl
			<< number2 << " divided by " << number1 << " is " << number2 / number1 << endl;
	}


	else if (symbol == '*')
	{
		cout << "Please enter two numbers\n";
		cin >> number1 >> number2;


		cout << number1 << " times " << number2 << " is " << number1 * number2 << endl;
	}

	else if (symbol == '%')
	{
		cout << "Please enter two numbers\n";
		cin >> number1 >> number2;

		cout << number1 << " modulus " << number2 << " is " << number1 % number2 << endl
			<< number2 << " modulus " << number1 << " is " << number2 % number1 << endl;
	}


	else
	{
		cout << "\nInvalid operator entered\n"
			 << "\nPlease enter a symbol to indicate which type of problem we will be solving\n";
		cin >> number1, number2;

	}

		system("pause");
		return 0;

}
double number1 ;
double number2 ;


But beware that modulus operator (%) don't work in decimals..!!
I've tried that, but it gives me a ton of errors. Am I getting these errors after making it double because the % is there?
Last edited on
We can't know* unless you post the errors...

* We could copy the code and compile it but I'm too lazy for that. ;-)
Last edited on
ton of errors
A two errors is ton now? And it should say what is wrong:
1
2
\Test\main.cpp|63|error: invalid operands of types 'double' and 'double' to binary 'operator%'|
\Test\main.cpp|64|error: invalid operands of types 'double' and 'double' to binary 'operator%'|
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
#include <iostream>

int main()
{
    std::cout << "Welcome to my calculator\n"
              << "Please enter a symbol to indicate which type of problem we will be solving\n"
              << "The choices are +, -, /, *, or % to find the modulus of two numbers\n";

    char symbol ;
    std::cin >> symbol;

    if (symbol == '+')
    {
        double number1, number2 ;
        std::cout << "Please enter two numbers\n";
        std::cin >> number1 >> number2;

        std::cout << number1 << " plus " << number2 << " is " << number1 + number2 << '\n';
    }

    else if (symbol == '-')
    {
        double number1, number2 ;
        std::cout << "Please enter two numbers\n";
        std::cin >> number1 >> number2;


        std::cout << number1 << " minus " << number2 << " is " << number1 - number2 << '\n'
                  << number2 << " minus " << number1 << " is " << number2 - number1 << '\n';
    }

    else if (symbol == '/')
    {
        double number1, number2 ;
        std::cout << "Please enter two numbers\n";
        std::cin >> number1 >> number2;

        std::cout << number1 << " divided by " << number2 << " is " << number1 / number2 << '\n'
                  << number2 << " divided by " << number1 << " is " << number2 / number1 << '\n';
    }

    else if (symbol == '*')
    {
        double number1, number2 ;
        std::cout << "Please enter two numbers\n";
        std::cin >> number1 >> number2;

        std::cout << number1 << " times " << number2 << " is " << number1 * number2 << '\n';
    }

    else if (symbol == '%')
    {
        int number1, number2 ;
        std::cout << "Please enter two numbers(integers)\n";
        std::cin >> number1 >> number2;

        if( number2 != 0 ) std::cout << number1 << " modulus " << number2 << " is " << number1 % number2 << '\n';
        if( number1 != 0 ) std::cout << number2 << " modulus " << number1 << " is " << number2 % number1 << '\n';
    }

    else
    {
        std::cout << "\nInvalid operator entered\n" ;
    }
}
@MiiNiPaa I had more than two. Thanks for the corrections JLBorges. I'll try that
Also just curious. Why did you put std:: before all the cout/cin statements?
> Why did you put std:: before all the cout/cin statements?

See http://www.cplusplus.com/forum/beginner/142171/#msg750694
Topic archived. No new replies allowed.