这是我的代码:-
#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()
函数中的 op
和 i
未初始化(尽管它们在上面已初始化)。我是 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/