2016-01-18 49 views
-4

我有一段代碼,它無法將Bonus作爲變量讀取。無法將Bonus作爲變量讀取

這裏是我的代碼:

#include <iostream> 
#include <string> 
using namespace std; 

int main() 
{ 
    char Bonus,Salary,TotalSalary; 
    int num1, num2; 

    cout <<"Enter the hours of work for week"<<endl; 
    cin >> num1; 
    cout <<"Enter your hourly rate"<<endl; 
    cin >>num2; 

    Salary = num1 * num2; 

    if (Bonus >= 45) 
     cout <<"Your bonus is 500 pesos"<<endl; 
    else if (Bonus>40&&Bonus<=45) 
     cout <<"Your bonus is 250 pesos"<<endl; 
    else if (Bonus>45&&Bonus<=40) 
     cout <<"Your bonus is 150 pesos"<<endl; 

    TotalSalary=Salary + Bonus; 

    cout <<"Your basic salary is" <<Salary <<"with a bonus of" <<Bonus <<"and a   total of"<<TotalSalary<<endl; 

    system("pause"); 
    return 0; 
} 

我希望你能幫助我與我的問題,在此先感謝。

+1

在哪裏設定了'Bonus'場?你在期待用戶輸入嗎? – SMA

+2

你是什麼意思?你不能把它看作'cin'中的數值嗎?另外爲什麼你將這些變量聲明爲'char'? –

+0

'獎金'尚未被分配一個值,所以任何使用它的代碼都會產生廢話。另外,使用'char'變量來保存整數值通常不是一個好主意。它的工作原理,但是對於工資等,他們的範圍不夠大。 –

回答

1

因爲你的BonusSalaryTotalSalary變量是char。所以你必須聲明它們爲int變量。並且Bonus在未初始化的情況下使用,其調用https://en.wikipedia.org/wiki/Undefined_behavior,因此您可以將其初始化爲0。此外,你必須通過

cout <<"Enter the Bonus"<<endl; 
cin >> Bonus; 

要求用戶輸入Bonus,它應該工作。

`#include <iostream> 
#include <string> 
using namespace std; 
int main() 
{ 
int Salary,TotalSalary; 
int num1, num2, Bonus = 0; 

cout <<"Enter the hours of work for week"<<endl; 
cin >> num1; 
cout <<"Enter your hourly rate"<<endl; 
cin >>num2; 

cout <<"Enter the Bonus"<<endl; 
cin >> Bonus; 


Salary = num1 * num2; 

if (Bonus >= 45) 
cout <<"Your bonus is 500 pesos"<<endl; 
else if (Bonus>40&&Bonus<=45) 
cout <<"Your bonus is 250 pesos"<<endl; 
else if (Bonus>45&&Bonus<=40) 
cout <<"Your bonus is 150 pesos"<<endl; 

TotalSalary=Salary + Bonus; 

cout <<"Your basic salary is " <<Salary <<" with a bonus of " <<Bonus <<" and a total of "<<TotalSalary<<endl; 

system("pause"); 
return 0; 
} 
+0

謝謝,它確實有用,但是你能幫助我多做一件事嗎?我的結局也被搞砸了。我非常感謝你的幫助 – Alvin

+0

我剛剛更新了它,請再次檢查。 – Khuong

+0

非常感謝你的兄弟。 – Alvin

0

與代碼的幾個問題:

Salary = num1 * num2; 

BonusSalaryTotalSalary被定義爲char,而他們也應該被定義爲int

Bonus在未初始化的情況下使用,其調用undefined behavior

編譯器應該已經警告過你。確保警告處於最高級別。


if statements一些細節:

if (Bonus >= 45) 
    cout << "Your bonus is 500 pesos" << endl; 
else if (Bonus>40 && Bonus<=45) 
    // minor detail, but can't be equal to 45 again 
    cout <<"Your bonus is 250 pesos" << endl; 
else if (Bonus>45 && Bonus<=40) 
    // mayor detail: can never be >45 *and* <=40 at the same time 
    cout <<"Your bonus is 150 pesos" << endl; 

你可以修改成:

// You probably want to check Salary instead of Bonus? 
// although the values (like 45) might be off 
Salary = num1 * num2; 

if (Salary >= 45) 
    Bonus = 500; 
else if (Salary > 40) 
    Bonus = 250; 
else 
    Bonus = 150; 

cout << "Your bonus is " << Bonus << " pesos" << endl; 

完整的示例

#include <iostream> 
#include <string> 

using namespace std; 

int main() 
{ 
    int num1 = 0, 
     num2 = 0, 
     Bonus = 0, 
     Salary = 0, 
     TotalSalary = 0; 

    cout << "Enter the hours of work for week: "; 
    cin >> num1; 
    cout << "Enter your hourly rate: "; 
    cin >> num2; 

    Salary = num1 * num2; 

    // values (like 45) might be off here 
    if  (Salary >= 45) Bonus = 500; 
    else if (Salary > 40) Bonus = 250; 
    else     Bonus = 150; 

    cout << "Your bonus is " << Bonus << " pesos" << endl; 

    TotalSalary = Salary + Bonus; 

    cout << "Your basic salary is " << Salary 
     << " with a bonus of "  << Bonus 
     << " and a total of "  << TotalSalary << endl; 

    system("pause"); 
    return 0; 
} 
0

Hy! 如果你定義Bonus(char類型) 聲明,但你在

if(Bonus>=45) 

測試之前,在這一點上你的char Bonus可能在運行時發出的任何可能的價值從未定義。 沒有像Bonus = 0或類似的默認任務。

+0

你應該拋光你的術語。 –

+0

是的,它代表「聲明」,「定義」aso。謝謝 – Claudiu

+0

您應該使用_「initialization」_而不是_「definition」_。該變量也被實際聲明和定義。定義對於例如靜態成員變量,可以聲明並且必須單獨定義。 –

0

Bonus沒有值,它是char

類型的變量應該是int,而我懷疑你想設置到Bonus依賴於Salary值:

int Bonus = 0; 
int Salary = 0; 
int TotalSalary = 0; 

// ... 

if (Salary >= 45) 
    Bonus = 500; 
else if (Salary > 40) 
    Bonus = 250; 
else 
    Bonus = 150; 

TotalSalary = Salary + Bonus; 
// ... 
+0

感謝您的幫助。 =) – Alvin