Hello, I am new to C++ and I was trying to write a simple calculator program that has a do-while loop and also includes a series of != statements. Even though I make the statements with all the appropriate keystrokes it keeps giving me my invalid sign statement. Also, I would like to know if I used the "while" statement properly. Thank you for your help in advance =D!

#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int result,value;
char sign;

int main()

{
    result = 0;

    do
    {
        cout<< "Current value is "<<result<<endl;

        cout<< "Please enter an operation +,-,*,/ <Press 'Q' to quit>"<<endl;

        cin>> sign;

        cout<< "Input value"<<endl;

        cin>> value;

        if ((sign != '+') || (sign != '-') || (sign != '*') || (sign != '/') || (sign != 'Q') || (sign != 'q'))
        {
        cout<<"Invalid sign "<<"("<<sign<<") used.";
        }

        else
        { 
            if (sign == '+')
            {
                result += value;
            }

            else
            {
                if (sign == '-')
                {
                    result *= value;
                }

                else
                {
                    if (sign == '/')
                    {
                        result /= value;
                    }
                }
            }
        }
    }
while(sign != 'Q' || sign != 'q');
return 0;
}

The issue is on line 26. You check if the sign isnt + OR isnt - OR isnt etc.. you want ANDs there. :)

Thank you very much! That is a real facepalm moment XD

Same thing again. sign != 'Q' || sign != 'q' is always going to be true, you want ANDs there...

one last question I have, sorry for all of these questions btw, XD. When should I use the logical OR over the logical AND?

In the case of testing for equality you would use OR

.....
char sign;
while(true)
{
    .....
    cin >> sign;
    if(sign == 'Q' || sign == 'q')
    break; // break from the loop.
}

...and for a cleaner coding you can cause the switch statement:

After lines #7 & #8 of Milton Neal's post:

switch( sign )
{
    case '+':
        statements;
        break;
    case '-':
        statements;
        break;
    case '*':
        statements;
        break;
    case '/':
        statements;
        break;
    default:
        cout<<"Invalid sign "<<"("<<sign<<") used.";
}

Replace statements with your actual statements.

commented: The best solution +14

Here's your original code cleaned up:

#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int result,value;
char sign;
int main()
{
    result = 0;
    do
    {
        cout<< "Current value is "<<result<<endl;
        cout<< "Please enter an operation +,-,*,/ <Press 'Q' to quit>"<<endl;
        cin>> sign;
        if(toupper(sign) == 'Q')
            break;
        cout<< "Input value"<<endl;
        cin>> value;
        switch(sign)
        {
        case '+':
            result += value;
            break;
        case '-':
            result -= value;
            break;
        case '*':
            result *= value;
            break;
        case '/':
            result /= value;
            break;
        default:
            cout<<"Invalid sign "<<"("<<sign<<") used.";
        }
    }
    while(true);
    return 0;
}

As for when to use && and when to use || you should really learn about boolean algebra. That way you may be able to actually reduce some of your expressions. Basically boolean algebra deals with true/false, 1/0. It has 2 fundamental operators + (or, ||) and x (and, &&). It has many of the rules that normal algebra has. For example a(b+c)=ab+bc. Replace a with for example sign!='+' and b with sign!='-' and you can create logical expressions that do what you want them to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.