c++ - 为什么计算不对?

标签 c++ pointers initialization return-value calculator

这是我的代码:-

#include <iostream>
using namespace std;


int Add (int *x , int *y)
{
    int a=*x;
    int b=*y;
    int c=a+b;

    return (c);
}

int Sub (int *x , int *y)
{
    int a=*x;
    int b=*y;
    int c=a-b;

    return (c);
}

int Mul (int *x , int *y)
{
    int a=*x;
    int b=*y;
    int c=a*b;

    return (c);
}

int Div (int *x , int *y)
{
    int a=*x;
    int b=*y;
    int c=a/b;

    return (c);
}

int Mod (int *x , int *y)
{
    int a=*x;
    int b=*y;
    int c=a%b;

    return (c);
}



int InputFunction (int *a , int *b , char op)
{
    int x=*a;
    int y=*b;
    int c=0;

    cout<<"Please enter first number : ";
    cin>>x;

    cout<<"Please enter second number : ";
    cin>>y;

    cout<<endl;

    cout<<"Please choose an operator to perform the operation :- "<<endl<<endl;
    cout<<" \t \t \t + for addition"<<endl;
    cout<<" \t \t \t - for sunbtraction"<<endl;
    cout<<" \t \t \t x for mutiplication"<<endl;
    cout<<" \t \t \t / for division"<<endl;
    cout<<" \t \t \t % for modulus"<<endl<<endl<<endl;
    cout<<" \t \t    Your choice : ";
    cin>>op;

    switch (op)
    {
        case '+':
                 Add (&x , &y);
                 break;

        case '-':
                 Sub (&x , &y);
                 break;

        case 'x':
                 Mul (&x , &y);
                 break;

        case '/':
                 Div (&x , &y);
                 break;

        case '%':
                 Mod (&x , &y);
                 break;

        default:
                 cout<<"Your symbol is not recognized!";
                 break;
    }

    int i=c;


    return (i);
}

int main()
{
    int a=0;
    int b=0;
    char op;
    char ch;
    int i;

    do
    {
    InputFunction (&a , &b , op);

    int m=i;

    cout<<" \t \t Your answer : "<<m<<endl<<endl;
    cout<<"Do you want to repeat the program ? (Y/N) ";
    cin>>ch;

    }while (ch == 'Y' || ch == 'y');

    cout<<"Good- Bye"<<endl;

    return 0;

}

为什么会计算出一些奇怪的长答案?编译器还显示警告,main() 函数中的 opi 未初始化(尽管它们在上面已初始化)。我是 C++ 的新手。帮助将不胜感激。

最佳答案

我已经为您修改了代码。实际问题是您没有分配返回值。你的 InpuFunction 中的 i 和你的主要方法中的 i 是不同的。除非您将值从 InputFunction(调用方法)返回到 main 函数(调用方法),否则您的值不会改变。

#include <iostream>
using namespace std;


int Add (int *x , int *y)
{

  int a=*x;
  int b=*y;
  int c=a+b;

  return (c);
}

int Sub (int *x , int *y)
{
  int a=*x;
  int b=*y;
  int c=a-b;

  return (c);
}

int Mul (int *x , int *y)
{
  int a=*x;
  int b=*y;
  int c=a*b;

  return (c);
}

int Div (int *x , int *y)
{
  int a=*x;
  int b=*y;
  int c=a/b;

  return (c);
}

int Mod (int *x , int *y)
{
  int a=*x;
  int b=*y;
  int c=a%b;

  return (c);
}



int InputFunction (char op)
{
  int x;
  int y;
  int c=0;

  cout<<"Please enter first number : ";
  cin>>x;
  cout<<"Please enter second number : ";
  cin>>y;
  cout << " x = " << x << " y = " << y << endl; 

  cout<<endl;

  cout<<"Please choose an operator to perform the operation :- "<<endl<<endl;
  cout<<" \t \t \t + for addition"<<endl;
  cout<<" \t \t \t - for sunbtraction"<<endl;
  cout<<" \t \t \t x for mutiplication"<<endl;
  cout<<" \t \t \t / for division"<<endl;
  cout<<" \t \t \t % for modulus"<<endl<<endl<<endl;
  cout<<" \t \t    Your choice : ";
  cin>>op;


  switch (op)
    {
    case '+':
      c = Add (&x , &y);
      break;

    case '-':
      c = Sub (&x , &y);
      break;

    case 'x':
      c = Mul (&x , &y);
      break;

    case '/':
      c = Div (&x , &y);
      break;

    case '%':
      c = Mod (&x , &y);
      break;

    default:
      cout<<"Your symbol is not recognized!";
      break;
    }
  cout << "c is " << c << endl;
  int i=c;


  return (i);
}

int main()
{

  char op;
  char ch;
  int i;

    do
      {
    int m = InputFunction (op);

    cout<<" \t \t Your answer : "<<m<<endl<<endl;
    cout<<"Do you want to repeat the program ? (Y/N) ";
    cin>>ch;

      }while (ch == 'Y' || ch == 'y');

    cout<<"Good- Bye"<<endl;

    return 0;

}

关于c++ - 为什么计算不对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20238967/

相关文章:

c++ - 在几何中使用 boost rtree 查找结果点

C++/OpenCV 多线程简单同步

c++ - 如何使用引用,避免 header 膨胀和延迟初始化?

c++ - 如何识别.exe文件是否需要输入参数

c++ - 通过 C++ 使用 BIOS 函数

c - 数组和指针与函数的和

c - 取消引用指向不完整类型(节点)的指针

c++ - 更改 vector 值导致段错误

ios - 除了 .storyboard(Outlet 引用)或 alloc int in view 之外的其他地方初始化的 UIlabels 是否加载?

loops - Verilog 向量内积