Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I am getting no errors but no out put for my program??

+2 votes
asked Oct 22, 2017 by cypher9916190
#include <iostream>

using namespace std;

int
main ()
{
  int x, a, b, sum, diff, product, divison;
  cout << "enter a no.\n";
  cin >> a;
  cout << "enter another no. \n";
  cin >> b;
  cout << "enter the operator\n";
  cin >> x;

  if (x == sum)
    {
      sum = a + b;
      cout << "sum=" << sum << endl;
    }
  else if (x == diff)
    {
      diff = a - b;
      cout << "diff=" << diff << endl;

    }
  else if (x == product)
    {
      product = a * b;
      cout << "product=" << product << endl;

    }
  else if (x == divison)
    {
      divison = a / b;
      cout << "divison=" << divison << endl;
    }
  return 0;
}

when I ran this program online it gave the accurate result.... but when I tried running this program on my computer I get no output just three entries but no result

2 Answers

0 votes
answered Oct 30, 2017 by anonymous
change the sum,diff,product,division with the number because the x is an integer variable it cannot store alphabets
0 votes
answered Jun 27, 2025 by Jerry Jeremiah (2,040 points)
edited Jun 27, 2025 by Jerry Jeremiah

You have

int x, a, b, sum, diff, product, divison;

and then you use sum, diff, product, and divison without giving those variables a value;

It is undefined behaviour to use variables that you haven't given a value.  I would change that line to:

int x, a, b, sum = 1, diff = 2, product = 3, divison = 4;

Then when the program asks for the operator you can use 1, 2, 3 or 4 for the operator to get sum, difference, multiplication or division.

You could always change the program like this:

int a, b;

char x, sum = '+', diff = '-', product = '*', divison = '/';

And then when the program asks for the operator you can use +, -, * or / for the operator to get sum, difference, multiplication or division.  You will have to change the variables used to store the answer to something that isn't a char so instead of storing it in the sum, diff, product and division variables you could store it in a variable called 'answer'.

Keep in mind that the way you are doing division is integer division.  You need to make the answer variable a double and do the division by casting a or b to a double before doing the division
  

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...