1

I started building a very simple version of a calculator in C++. The idea is to perform basic operations with only two numbers and then loop back so the user can make a new calculation.

The program looks like this:

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

int main()
{
  int x, y;
  string operation;
  string repeat = "y";

  while (repeat == "y" or "Y")
  {
    cout << "Welcome! This is a raw version of a calculator - only use two numbers." << endl;
    cin >> x >> operation >> y;

    if (operation == "+")
    {
      cout << "Result: " << add(x, y) << endl;
    }
    else if (operation == "-")
    {
      cout << "Result: " << subtract(x, y) << endl;
    }
    else if (operation == "*")
    {
      cout << "Result: " << multiply(x, y) << endl;
    }
    else if (operation == "/")
    {
      cout << "Result: " << divide(x, y) << endl;
    }
    else
    {
      cout << "This is not a valid sign. Please choose another one!" << endl;
    }

    cout << "Wanna go again? Type 'y' or 'n'." << endl;
    cin >> repeat;

    if (repeat == "n" or "N")
    {
      cout << "Alright, have a nice day!" << endl;
      break;
    }
  }
}

int add(int x, int y)
{
  return x + y;
}

int subtract(int x, int y)
{
  return x - y;
}

int multiply(int x, int y)
{
  return x * y;
}

int divide(int x, int y)
{
  return x / y;
}

NOTE: There is a 'mathOperations.h' file in which I have made forward declarations of all functions used.

The problem is that whenever I type in 'y' to make it loop, it simply outputs the following 'if' statement and breaks out of the loop and the program finishes. I couldn't quite figure out why this is happening, since the 'if' statement is only supposed to run if I type in 'n'.

11
  • 6
    or doesn't give a selection of possible values, it is a combination of boolean expressions Commented Oct 19, 2016 at 15:55
  • 1
    if (repeat == "n" or repeat == "N"). Same with y Commented Oct 19, 2016 at 15:57
  • 2
    @JesperJuhl How do you know the OP isn't working from a book? If questions like this don't represent the reason for this site, what is? Comments like this drive away the very people who need this site. Please comment/answer when you have something constructive to say. Commented Oct 19, 2016 at 16:02
  • 2
    @DaveSwersky I appreciate your comment. I really do feel annoyed when I see a question (especially my own) get instantly downvoted because of people who think like this because the question isn't good enough for them... Commented Oct 19, 2016 at 16:14
  • 4
    I actually think this is a perfectly good question - it's well formed, the question is clear, the code is included (although not minimal; but pretty small)... it satisfies all the requirements for a question here. Commented Oct 19, 2016 at 16:18

2 Answers 2

7
repeat == "n" or "N"

evaluates to

(repeat == "n") || "N"

see the C++ operator precedence.

The first repeat == "n" evaluates to true or false depending on your input, but the second clause of the OR, i.e. "N", always evaluates to true because it is a string literal that decays to a non-zero const char* pointer, and in C or C++ everything non-zero is implicitly converted to true. So your OR clause is always true, which implies that the if block will always be executed.

As mentioned in the comments, you need to do

if(repeat == "n" || repeat == "N") {...}

Similarly with the first while condition.

Sign up to request clarification or add additional context in comments.

Comments

1

Nice code! I try using "||" in place of your "or" in your if statements. Might want to refresh your knowledge with C++ short-circuiting of booleans.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.