A little help

I was trying to set up a simple calculator. I have no idea why it's not working
correctly. No matter what operation I input, it says input not accepted.

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
#include <iostream>
#include <string>
using namespace std;
int main()
// Prompts user to enter an operation followed by operands. Has Bugs
{
	string operation = "???"; // Initiates string, operation. Can holds 4 common operations depending on human input.
	string message = "???"; // Initiates string, message. Can hold 2 messages as a result of human input.
	int answer = 0; // Initiates variable, answer
	int left_input = 0; // Initiates variable, left_input
	int right_input = 0; // Initiates variable, right_input
	cout << "---------------------------Welcome!----------------------------" << endl; 
	cout << "\n"; // New line (Just for miniscule asthetics)
	cout << "---------------------------Sign Key----------------------------" << endl; // Shows a key for humans who don't recognize certain operations.
	cout << "\n";
	cout << "Add = +" << endl; 
	cout << "Subtract = -" << endl;
	cout << "Multiply = *" << endl;
	cout << "Divide = /" << endl;
	cout << "\n";
	cout << "\n";
	cout << "Please input left hand operand.\n"; // Prompts human for left hand operation input
	cout << "\n";
	cin >> left_input;
	cout << "\n";
	cout << "Please input right hand operand.\n"; // Prompts human for right hand operation input
	cout << "\n";
	cin >> right_input;
	cout << "\n";
	cout << "Please input the operation you wish to use.\n"; // Prompts human for operation input
	cin >> operation;
	cout << "\n";
	if (operation == "+") // If typed operation is +, Initiate equation
	message = "If you wish to continue, reboot this program";
	answer = left_input + right_input;
	if (operation == "-") // If typed operation is -, Initiate equation
	message = "If you wish to continue, reboot this program";
	answer = left_input - right_input; 
	if (operation == "*") // If typed operation is *, Initiate equation
	message = "If you wish to continue, reboot this program";
	answer = left_input * right_input;
	if (operation == "/") // If typed operation is /, Initiate equation
	message = "If you wish to continue, reboot this program";
	answer = left_input / right_input; 
	if (operation != "+") // If typed operation is not + initiate message
	message = "Input not accepted. Reboot Program";
	if (operation != "-") // If typed operation is not - initiate message
	message = "Input not accepted. Reboot Program";
	if (operation != "*") // If typed operation is not * initiate message
	message = "Input not accepted. Reboot Program";
	if (operation != "/") // If typed operation is not / initiate message
	message = "Input not accepted. Reboot Program";
	
	cout << "Your answer is " << answer << "\n"; // Prints answer on screen
	cout << message << endl; // message can either be a human unaccepted input error or message prompting restart for continued usage
	
	return 0;

}
closed account (Dy7SLyTq)
use getline instead of cin and see if that works
Well this is out of a lesson and the point is using the most simple things to get it to work. Is there anyway to get cin to work?
closed account (Dy7SLyTq)
clear the buffer. getline isnt complicated. whats probably happening is your putting too much in the buffer and getline can solve that
The problem is with your "if" statements.

1) You perform all four arithmetic operations and the answer will always be a quotient.
2) Three of the statements checking the operator will always be true hence the message.
you else if statements of a switch. I would also reccommend inputting the operator as a character since you know its one character and not an array of characters.

http://www.cplusplus.com/doc/tutorial/control/

*edit also it would be if( operator != "+" && operator != "-" && operator != "*" && operator != "/" )

Right now it is like using or..so you are saying if it is not any of these then don't do the stuff...which will always be true.
Last edited on
Try to use fuctions.
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>

using namespace std;

float division (float a, float b)
{
    float r;
    r=a/b;
    return (r);
}
float multiplication (float a, float b)
{
    float r;
    r= a*b;
    return (r);
}
float addition(float a, float b)
{
    float r;
    r=a+b;
    return(r);
}
float subtraction(float a, float b)
{
    float r;
    r=a-b;
    return(r);
}
int main()
{
    char opselect;
    bool playagain = true;
    char playagainchar;
    float ma;
    float mb;
    float ans;

    cout<<"Welcome to my Calculator!\n\n";
    while (playagain == true)
    {
        cout<<"Please select an operation:\n\n";
        cout<<"D Division \nM Multiplication \nA Addition \nS Subtraction \n\n";
        cout<<"Please use a letter to select the operation: ";
        cin>>opselect;

        if (opselect == 'D' || opselect == 'd')
        {
            cout<< "What two numbers would you like to divide: ";
            cin>>ma;
            cin>>mb;
            ans = division (ma, mb);
            cout<<"The answer is: "<<ans<<"\n";
            cout<<"Would you like to do another Calculation (Y/N): ";
            cin>>playagainchar;
            cout<<"\n\n";
            if(playagainchar != 'y') playagain = false;
        }

        else if (opselect == 'M' || opselect == 'm')
        {
            cout<< "What two numbers would you like to multiply: ";
            cin>>ma;
            cin>>mb;
            ans = multiplication (ma, mb);
            cout<<"The answer is: "<<ans<<"\n";
            cout<<"Would you like to do another Calculation (Y/N): ";
            cin>>playagainchar;
            cout<<"\n\n";
            if(playagainchar != 'y') playagain = false;
        }

        else if (opselect == 'A' || opselect == 'a')
        {
            cout<< "What two numbers would you like to add: ";
            cin>>ma;
            cin>>mb;
            ans = addition (ma, mb);
            cout<<"The answer is: "<<ans<<"\n";
            cout<<"Would you like to do another Calculation (Y/N): ";
            cin>>playagainchar;
            cout<<"\n\n";
            if(playagainchar != 'y') playagain = false;
        }

        else if (opselect == 'S' || opselect == 's')
        {
            cout<< "What two numbers would you like to subtract: ";
            cin>>ma;
            cin>>mb;
            ans = subtraction (ma, mb);
            cout<<"The answer is: "<<ans<<"\n";
            cout<<"Would you like to do another Calculation (Y/N): ";
            cin>>playagainchar;
            cout<<"\n\n";
            if(playagainchar != 'y') playagain = false;
        }
        else
        {
            cout<< "Wrong Selection ";
            cout<<"Would you like to do another Calculation (Y/N): ";
            cin>>playagainchar;
            cout<<"\n\n";
            if(playagainchar != 'y') playagain = false;
        }
    }


    cout<<"Thanks for calculating!";
    cin.get();
    return 0;
}

Let me know if you are still having problem
You guys are making this too complicated.

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

int main()
{
    char operation = '+';
    double lhs = 0.0 , rhs = 0.0;
    
    std::cout << "Please enter your math problem: ";
    std::cin >> lhs >> operation >> rhs;
    switch( operation )
   {
        case '+':
            std::cout << lhs + rhs << std::endl;
            break;
        case '-':
            std::cout << lhs - rhs << std::endl;
            break;
        case '*':
            std::cout << lhs * rhs << std::endl;
            break;
        case '/':
            std::cout << lhs / rhs << std::endl;
            break;
        default:
            std::cout << "Invalid operation." << std::endl;
   }
    return( 0 );
}
closed account (Dy7SLyTq)
personally i would write a variation of the infamous stroustop calculator, but in the spirit of keeping it simple, the only thing i would change about giblits is do testing so it doesnt have to be ' ' seperated
To jump on the bandwagon here: I wrote this last night. And I would agree. With giblit.

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

using namespace std;

int main(int argc, const char * argv[])
{

    double num1, num2;
    char op;
    cout << "pleaset enter in a problem: ";
    cin >> num1  >> op >> num2;
    
    switch (op) {
        case '+':
            cout << num1 << '+' << num2 << '='  << num1 + num2 << endl;
            break;
        case '-':
            cout << num1 << '+' << num2 << '='  << num1 - num2 << endl;
            break;
        case '/':
            if (num1 == 0 || num2 == 0) {
                cout << "Division by zero is not possible" << endl;
            }
            else
                cout << num1 << '+' << num2 << '='  << num1 / num2 << endl;
            break;
        case '*':
            cout << num1 << '+' << num2 << '='  << num1 * num2 << endl;
            break;

        default:
            break;
    }
closed account (Dy7SLyTq)
i stand by what i said, however your code is not compliant with proper math laws bdanielz. it wont let you do 0 / 4. it will say that is in error, but its perfectly acceptable. its only an error if its number / 0.
thank you for pointing that out.

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

using namespace std;

int main(int argc, const char * argv[])
{

    double num1, num2;
    char op;
    cout << "pleaset enter in a problem: ";
    cin >> num1  >> op >> num2;
    
    switch (op) {
        case '+':
            cout << num1 << '+' << num2 << '='  << num1 + num2 << endl;
            break;
        case '-':
            cout << num1 << '+' << num2 << '='  << num1 - num2 << endl;
            break;
        case '/':
            if ( num2 == 0) {
                cout << "Division by zero is not possible" << endl;
                break;
            }
            else
                cout << num1 << '+' << num2 << '='  << num1 / num2 << endl;
            break;
        case '*':
            cout << num1 << '+' << num2 << '='  << num1 * num2 << endl;
            break;

        default:
            break;
    }


updated.
Last edited on
Topic archived. No new replies allowed.