这是我的代码: -
#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;
}
为什么要计算一些奇怪的长答案?此外,编译器还显示警告op
函数中的i
和main()
未初始化(尽管它们已在上面初始化)。我是C ++的新手。帮助将不胜感激。
答案 0 :(得分:2)
int InputFunction (int *a , int *b , char op)
您永远不会将函数的返回值赋给主循环中的任何内容。
即,
int i = InputFuction(...);
int a = Add(1, 2);
int b = Sub(1, 2);
// etc.
您的所有功能(Add,Sub,Mul等)都存在同样的问题。返回值未分配给任何内容。
答案 1 :(得分:2)
我已经为你修改了代码。实际问题是您没有分配返回值。我在你的InpuFunction中,我的主要方法是不同的。除非将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;
}
答案 2 :(得分:1)
对于编译器警告,您已声明op
和i
,但未使用值初始化它们(与a
&amp; b
一样)。
至于输出,你实际得到了什么输出?
答案 3 :(得分:1)
对于警告,当您声明局部变量时,不会设置其值。这意味着它的价值是不确定的,使用它将导致不确定的行为。
您需要在使用之前初始化局部变量。顺便说一下,它可能是您问题的根本原因。
您可能想要这样做。
i = InputFunction (&a, &b, op);
或者甚至更好,您在i
函数中确实不需要op
变量(或main
),所以
m = InputFunction (&a, &b);
应该足够了(在您将InputFunction
更改为不将op
作为参数后)。