2013-08-23 90 views
-5

夥計們請再次幫助我與我的程序。我改變了代碼的順序。請檢查我的代碼有什麼問題。它運行,但它不執行它應該做的任務。它應該計算用戶輸入的等級總數並顯示相應的評論。不幸的是,它不工作:(請幫我聲明函數第二部分

#include<iostream> 
#include<conio.h> 

using namespace std; 

void computePG(int& PG); 
void Remark(int PG); 

    int x, y, z, w, p; 
    int prelimGrade,yourRemark,PG; 
    int preliminaryGrade; 

int main() 
{ 
    int pGrade; 

cout<<"Programmed by: Katrina G. Gozo, 1ISC"; 
cout<<endl<<"\nDate: Aug. 23,2013"; 
cout<<endl<<"\nThis program intends to compute the PG and make the necessary remarks"; 

cout<<"\n\nPlease enter your score on quiz 1 "; 
cin>>x; 

cout<<"\nPlease enter your score on quiz 2 "; 
cin>>y; 

cout<<"\nPlease enter your score on quiz 3 "; 
cin>>z; 

cout<<"\nPlease enter your score on prelims "; 
cin>>p; 


computePG(pGrade); 
Remark(pGrade); 


getch(); 
} 

void computePG(int& PG) 
{ 
    PG = x/30 * 20 + y/50 * 20 + z/40 * 20 + w/100 * 40; 
    cout << "\nYour prelim grade is " << PG; 

} 

void Remark(int PG) 
{ 
    if (PG>=90) 
     cout<<"A." <<endl; 
    else if (PG>=80) 
      cout<<"B."<<endl; 
    else if (PG>=70) 
      cout<<"C."<<endl; 
    else if (PG>=60) 
      cout<<"D."<<endl; 
    else 
      cout<<"E."<<endl; 
} 
+1

定義「不起作用」。 – Michael

+1

學習如何使用調試器的時間 – Cornstalks

+2

我希望這是您輸出的名字! – pamphlet

回答

1

你最有可能發生衝突運行的整數運算的注意事項:將一個整數被另一個整數時,你會得到一個整數(取整到零) 。

所以你需要使用double作爲PGpGrade類型,並在computePG浮點數字常量,以及,通過寫下他們作爲30.020.0

+0

例如'29/30 * 20'爲0. OTOH,'29 * 20/30'爲19. – MSalters

+0

請進一步詳細說明先生,我只是一個開山人 –

+0

一個整數只關心數字的整個部分,所以那裏即使你不關心小數點後的情況,也可以在分割時舍入錯誤。例如,如果你使用'int i = 1/2;','i'將等於0,而不是1,因爲它只是下降了0.5。正如我在我的回答中提到的,使用double將解決此問題,但我建議您熟悉C++中的不同數值類型。 – IllusiveBrian

0

你應該使用雙倍你的VAR ble「PG」,這樣你就有足夠的小數精度。

此外,您可能希望避免將來使用全局變量,因爲我猜測這是您發生此錯誤的方式 - 在使用它之前,您從未將值指定爲w,這意味着它將被賦值爲0編譯器,可能是什麼在搞砸你的結果。

+0

'w'是一個全局的,因此是0初始化的;它的使用實際上是明確定義的,它對計算沒有任何貢獻。當然,這並不意味着我支持全局變量。 –

+0

我從來不知道這一點,它顯示了我使用全局變量的程度。我會更新我的答案。如果她希望w具有有用價值,那麼這仍然是她的問題。 – IllusiveBrian