Jebin Matthew
Jebin Matthew

Reputation: 35

Calculator program that I just created in c++ does only addition! Whats wrong with my code?

I made a calculator program in c++ (after seeing a model in learncpp.com {section 1.10a})

It was created to add, sub, multiply, divide...

But it always adds the given two numbers.

It compiles fine but only adds! Even if I chose any number during operator selection (ie, 1 for add, 2 for sub etc.,), like 2,3 or even 25 or 2678, It just adds the two given numbers...(It can add only if i selected 1 right?)

I've spent hours trying to resolve but I'm just too new to c++ I don't know how!

Please help guys...

Heres my program

#include "stdafx.h"
#include <iostream>

int GetNo()
{
    std::cout << "Enter your Number: ";
    int a;
    std::cin >> a;
    return a;
}

int GetOp()
{
    std::cout << "Your Operator?" << std::endl;
    std::cout << "  (1 = +)" << std::endl;
    std::cout << "  (2 = -)" << std::endl;
    std::cout << "  (3 = *)" << std::endl;
    std::cout << "  (4 = /)" << std::endl;
    int o;
    std::cin >> o;
    return o;
}

int Calc(int x, int op, int y)
{
    if (op == 1);
        return x + y;
    if (op == 2);
        return x-y;
    if (op == 3);
        return x*y;
    if (op == 4);
        return x/y;

        return -1;

}

void PRINT(int q)
{
    std::cout << "Therefore, Your Result is, " << q << std::endl;
}

int main()
{
    int x = GetNo();
    int opr = GetOp();
    int y = GetNo();
    int q = Calc(x,opr,y);
    PRINT(q);
}

TQ guys! I'm waiting for helpful replies... And if possible, Be more elaborate...(cuz im new to cpp)

Upvotes: 1

Views: 218

Answers (4)

Sourav Kanta
Sourav Kanta

Reputation: 2757

When you put a ; after an if clause it means that the if is an empty block.Therefore whether the statement is true or false the statement next to if is always excecuted.So your code

   if (op == 1);
    return x + y;

evaluates to :

  if (op == 1)
      {            //empty block
      }
  return x + y; // Outside if statement

Therefore always addition is returned.

Upvotes: 3

marc_s
marc_s

Reputation: 455

Your Mistake Is In The calc()Function.Take A Look:

int Calc(int x, int op, int y)
{
if (op == 1);  //the ; is not needed, remove it! 
    return x + y;
//same happens with the rest of the conditions, remove all semicolons after the if conditions 
//...
}

Upvotes: 0

g19fanatic
g19fanatic

Reputation: 10921

Your code has the following:

int Calc(int x, int op, int y) { if (op == 1); return x + y; if (op == 2); return x-y; if (op == 3);

the problem is the ; after if (op == 1);. This is being evaluated and nothing is happening, then you are always performing return x + y;.

A better way to do loops is to always include the brackets so that these simple mistakes are less common, such as:

if (op==1) { return x + y; }

Upvotes: 0

Gaurav Sehgal
Gaurav Sehgal

Reputation: 7542

    if (op == 1);//this semicolon makes the statement end here so it tests the condition and ends the statement 
    return x + y;//so this is an independent statement and will always be executed

So remove the semicolon at the end of all if(condition) statements

Upvotes: 3

Related Questions