2013-11-27 64 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