//Program adds, subtracts, multiplies, or divides any two numbers that the user enters.

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
    char operation = ' ';
    int num1 = 0;
    int num2 = 0;
    int answer = 0;
    
    cout <<"Enter A (add) or S (subtract) or M (multiply) or D (divide): ";
    cin >> operation;

    if (operation != 'A' || 'a' || 'S' || 's' || 'M' || 'm' || 'D' || 'd')
    {
        cout <<"Error: Wrong letter entered" << endl;
        return 0;
    }

    cout <<"Enter first number: ";
    cin >> num1;
    cout <<"Enter second number: ";
    cin >> num2;
    
    if (operation == 'A' || 'a')
    {
        answer = num1 + num2;
        cout <<"Sum: " << answer << endl;;
    
    }//end if

    if (operation == 'S' || 's') 
    {
            if (num1 > num2)
            {
                answer = num1 - num2;
            }//end if
            else 
                answer = num2 - num1;

        cout <<"Difference: " << answer << endl;
    }//end if
    
    
    if (operation == 'M' || 'm')
    {
        answer = num1 * num2;
        cout <<"Product: " << answer << endl;
    }//end if
    
    if (operation == 'D' || 'd')
    {    
        if (num1 > num2)
        {
            answer = num1 / num2;
        }//end if
        else
            answer = num2 / num1;
    
        cout <<"Quotient: " << answer << endl;
    }//end if

    return 0;
}//end of main function

I'm having trouble with the error message that I need to display at the beginning if the user enters an invalid letter. It's the first if statement.

It is suppose to display the error message if the user enters an invalid letter. Otherwise it should continue one and ask the user for the 2 numbers. What it does is it always dispalys the error message in cout and statements that as the user to enter the number never appear.
Any help appreciated!
Thanks. :)

Recommended Answers

All 5 Replies

This is an older version of a calculator that I made when I was just srated. Look around and find some snippets you might like to see how they work or add to yours. For example use of the if statement for displaying an error message, and having a repeating loop asking if a person wants to use the calculator again.

#include <iostream>
using namespace std;

int main()
{

float num1;
    float num2;
    char op;
    float result;
    char again = 'y';
    while (again == 'y')
    
    {
    cin >> num1;
    cout <<"\n num 1 = " << num1 << endl;
    cin >> num2;
    cout << "\n num2 = " << num2 << endl;
  
    cout <<"\n Now enter an operating symbol (+ - / *)"<<endl;
    cout <<"\n You can also enter 2 to sqaure a # and 3 to cube a #" <<endl;
    cin >> op;
    
    if (op == '+')
        cout << "\n num1 + num2 = " << num1 + num2 << endl;
    if (op == '-')
        cout <<"\n num1 - num2 = " << num1 - num2 << endl;
    if (op == '*')
        cout <<"\n num1 * num2 = " << num1 * num2 << endl;
    if (op == '/')
        cout << " \n num1 / num2 = " << num1 / num2 << endl;
    if (op == '2')
        cout <<" \n num1 to the second power = " << num1 * num1 << endl;
    if (op == '3')
        cout <<" \n num1 to the third power = " << num1 * num1 * num1 << endl;
    else 
        cout << "\n Invalid Operation";
 

    cout << "\n Do you want to try again? (y/n): ";
    cin >> again;
    }
    
     return 0;
}

> if (operation != 'A' || 'a' || 'S' || There is no compare with a list type function, you need to compare with each one in turn if (operation != 'A' || operation != 'a' || operation != 'S' || Look up the toupper() function operation = toupper(operation); then you would only need half as many comparisons.

> if (operation != 'A' || 'a' || 'S' || There is no compare with a list type function, you need to compare with each one in turn if (operation != 'A' || operation != 'a' || operation != 'S' || Look up the toupper() function operation = toupper(operation); then you would only need half as many comparisons.

Thanks for all the replies and code.

I have done the if (operation != 'A' || operation != 'a') and so on for the rest of the letters.

But when I run it and enter either A,a,S,s,M,m,D,d it still displays the error message and the program doesn't ask the user for the numbers.

Try something like:

int main()
{
    int continue_session = 1 ;
    do
    {
        // your entire code goes here
        printf ("do you want to continue (0 if not) ? ") ;
        scanf ("%d", &continue_session) ;
    }
    while ( continue_session != 0 ) ;
    return 0;
}

Just make the changes as mentionned by Mr. Salem and try the snippet i have placed in front of you and your program will definately work.

Well I got it to work. :)

I just changed the || to && in the if statement that checks what the user entered.

//Program adds, subtracts, multiplies, or divides any two numbers that the user enters.

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
    char operation = ' ';
    int num1 = 0;
    int num2 = 0;
    int answer = 0;
    
    cout <<"Enter A (add) or S (subtract) or M (multiply) or D (divide): ";
    cin >> operation;
    operation = toupper(operation);

    if (operation != 'A' && operation != 'S' && operation != 'S' && operation != 'M' && operation != 'D')
//Changed this to && instead of ||
    {
        cout <<"Error: Wrong letter entered" << endl;
        return 0;
    }

    cout <<"Enter first number: ";
    cin >> num1;
    cout <<"Enter second number: ";
    cin >> num2;
    
    if (operation == 'A' || 'a')
    {
        answer = num1 + num2;
        cout <<"Sum: " << answer << endl;;
    
    }//end if

    if (operation == 'S' || 's') 
    {
            if (num1 > num2)
            {
                answer = num1 - num2;
            }//end if
            else 
                answer = num2 - num1;

        cout <<"Difference: " << answer << endl;
    }//end if
    
    
    if (operation == 'M' || 'm')
    {
        answer = num1 * num2;
        cout <<"Product: " << answer << endl;
    }//end if
    
    if (operation == 'D' || 'd')
    {    
        if (num1 > num2)
        {
            answer = num1 / num2;
        }//end if
        else
            answer = num2 / num1;
    
        cout <<"Quotient: " << answer << endl;
    }//end if

    return 0;
}//end of main function
commented: good job +3
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.