سؤال

I am Getting undefined symbol error for all the integer and char values.Please Help me. The int x y and z are not working and also the char value of function.

#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <string.h>

class Calculator
{
    public:
    int x;
    int y;
    int z;
    char function;

    void Calculate()
    {
      if(function=='+')
      {z=x+y;}
      else if(function=='-')
      {z=x-y;}
      else if(function=='*')
      {z=x*y;}
      else if(function=='/')
      {z=x/y;}
      else
      {cout<<"Wrong Function!!!";}
    }
};

void main()
{
    clrscr();
    Calculator  working;
    cout<<"Welcome!"<<endl;
    cout<<"Enter your first number:"<<endl;
    cin>>x;
    cout<<"Enter your function:"<<endl;
    cin>>function;
    cout<<"Enter your second number:"<<endl;
    cin>>y;
    working.Calculate();
    cout<<"Your Result is:"<<z<<endl;
    getch();
}
هل كانت مفيدة؟

المحلول 3

You need to pass the values to the function Calculate. Variables x, y, z and function are not accessible outside the class and also u need a return type to the function so that you can get the output from the function Calculate.

class Calculator
{
public:
int x;
int y;
int z;
char function;

int Calculate(int main_x,int main_y,char main_function)
{
    x= main_x;
    y=main_y;
    function = main_function;
  if(function=='+')
  {z=x+y; return z;}
  else if(function=='-')
  {z=x-y; return z;}
  else if(function=='*')
  {z=x*y; return z;}
  else if(function=='/')
  {z=x/y; return z;}
  else
  {cout<<"Wrong Function!!!"; return 0;}
}
 };

void main()
 {
     clrscr();
   int main_x,main_y,main_z;
    char main_function;

Calculator  working;
cout<<"Welcome!"<<endl;
cout<<"Enter your first number:"<<endl;
cin>>main_x;
cout<<"Enter your function:"<<endl;
cin>>main_function;
cout<<"Enter your second number:"<<endl;
cin>>main_y;
main_z = working.Calculate(main_x,main_y,main_function);
cout<<"Your Result is:"<<main_z<<endl;
getch();
}

نصائح أخرى

Either use, std::cin, std::cout, std::endl or include std namespace,

using namespace std;

This code compiles:

#include <iostream>
#include <math.h>
#include <string.h>

using namespace std;

class Calculator
{
public:
int x,y;
float z;
void add()
{
cin>>x;
cin>>y;
z=x+y;
cout<<"The addition is:"<<z<<endl;
}
void substract()
{
cin>>x;
cin>>y;
z=x-y;
cout<<"The substraction is:"<<z<<endl;
}
void multiply()
{
cin>>x;
cin>>y;
z=x*y;
cout<<"The multiplication is:"<<z<<endl;
}
void divide()
{
cin>>x;
cin>>y;
z=x/y;
cout<<"The division is:"<<z<<endl;
}

};

int main()
{
cout<<"Hello User!"<<endl;
char Name[23];
cout<<"Enter your name:";
cin>>Name;
cout<<"Hy "<<Name<<endl;

cout<<"Calculator:"<<endl;
Calculator maths;

cout<<"Enter two numbers to Add:"<<endl;
maths.add();
cout<<"Enter two numbers to Substract:"<<endl;
maths.substract();
cout<<"Enter two numbers to Multiply:"<<endl;
maths.multiply();
cout<<"Enter two numbers to Divide:"<<endl;
maths.divide();
}

You are missing std:: qualifiers whenever you use cin, cout, or endl. Either use std::cout, std::cin and std::endl or include using namespace std; in the beginning of your file.

Error is coming because you are trying to access class member variable x, y, z from outside in the main() where x, y, z is not declared.

In order to calculate() work correctly x, y, z should get correct value, in your case these variable has garbage value.

This is successful compiles version of your code.

#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;

class Calculator
{
public:
  int x;
  int y;
  int z;
  char function;

  void Calculate()
  {
    if(function=='+')
      {z=x+y;}
    else if(function=='-')
      {z=x-y;}
    else if(function=='*')
      {z=x*y;}
    else if(function=='/')
      {z=x/y;}
    else
      {cout<<"Wrong Function!!!"<<endl;}
  }
};

int main()
{

  Calculator  working;
  cout<<"Welcome!"<<endl;
  cout<<"Enter your first number:"<<endl;
  cin>>working.x;
  cout<<"Enter your function:"<<endl;
  cin>>working.function;
  cout<<"Enter your second number:"<<endl;
  cin>>working.y;
  working.Calculate();
  cout<<"Your Result is:"<<working.z<<endl;
  return 0;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top