2013-11-27 62 views
0

这里是我的代码: -为什么计算不正确?

#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; 

} 

为什么计算一些奇怪的长的答案?此外,编译器显示警告opimain()函数未初始化(虽然它们在上面初始化)。我是C++新手。帮助将不胜感激。

+0

您能否将代码缩小到有问题的部分?什么“奇怪的长答案”?您能否为某些输入显示实际的(和预期的)输出? –

+1

你并没有改变局部变量i。 –

+1

它应该是:'int m = InputFunction(&a,&b,op);'并且我看不到输入函数传递参数的点... – SHR

回答

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; 

} 
+1

谢谢你。但是编译器显示的警告怎么样? –

+0

你使用哪种编译器?我正在使用GNU C++编译器,但没有看到任何警告。 – sakthisundar

+1

Microsoft Visual Studio编译器。它表示 ''i':未引用的局部变量'和 '本地变量'op'未经初始化使用' –

1

对于编译器警告,你已经宣布opi,而不是用值初始化它们(如您a & b一样)。

至于输出,你实际得到了什么输出?

+1

8 + 8应该是16,但它给我-858993460。我应该用0初始化吗?你会怎么说同样的操作? –

+1

由于'i'被用作操作的答案,因此将'I'分配给来自'InputFunction'的输出,因为其他答案已经提到。对于'op',你应该询问你的用户他想要计算什么(与你已经用'ch'做“Y”或“y”的方式一样)。 – ThaMe90

1

对于警告,当您声明局部变量时,其值不会被设置。这意味着它的价值是不确定的,使用它会导致未定义的行为。

在使用它们之前,您需要初始化局部变量。顺便提一句,这可能是你的问题的根源。

您可能想要做

i = InputFunction (&a, &b, op); 

甚至更​​好,你并不真的需要在main功能i变量(或op),所以

m = InputFunction (&a, &b); 

应该足够(更改后的InputFunction不走作为参数的op)。

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等)相同的问题。返回值没有分配给任何东西。

+3

他似乎在假设,如果你有两个变量在不同的函数中具有相同的名称,他们会神奇地被链接并具有相同的值。 – benjymous

+0

@benjymous我同意,特别是当我看到OP如何定义返回值(i)时。但我相信上述问题无论如何都会解决问题。 – Inisheer

+0

这不会修复它,因为他在调用添加,子等等,在InputFunction – benjymous