2012-06-10 238 views
2

我剛學完如何使用隨機功能,所以我決定做一個小型的賭場遊戲。但是,我的代碼不會更新double Money;變量。那就是我無法設置,添加或減去這個變量的值。我無法確定發生這種情況的原因。爲什麼變量不變?

這裏是我的代碼:

int main() 
{ 
    double Money;//Variable Money; 

    srand (time(0)); 
    int randomNumber; 
    randomNumber = (1+(rand() % 10)); 
    cout << "\nEnter your Starting Money: "; 
    int Guess; 
    cin >> Money; 
    cout << "Your Starting money is: " << Money; 
    cout << "\nGuess the number between 1 and 10: "; 
    start: 
    cin >> Guess; 
    if(Guess == randomNumber) 
    { 
    Money + 10; 
    cout << "You were correct! The number was " << randomNumber << "!"; 
    } 
    else 
    { 
    Money - 10;//Nothing Happens 
    cout << "You were wrong! Your money is now: " << Money << "\nRetry Please: "; 
    goto start; 
    } 
    char f; 
    cin >> f; 
    return 0; 
} 
+1

你沒有設置變量,試試'Money = Money + 10' –

回答

6
Money + 10; 

是沒有副作用的聲明。你想

Money += 10; 

這相當於

Money = Money + 10; 

想想看,如果你寫

4 + 5; 

,你會想到4成爲9? ...

1

money - 10;是一個有效的C和C++表達式,它在概念上計算該值,然後丟棄結果,與42;大致相同。代替使用money = money - 10;money -= 10;。同樣也可以添加。

1

你需要新的谷分配給Money

Money = Money + 10或者,如前所述,Money += 10

+0

Thankyou太多了! – Dasttann777

0

金錢+ 10犯規,如果你想改變的價值用金錢改變值=金錢+ 10 你也可以使用Money + = 10 ,並用btw代替char f; cin >> f; .. cout < < endl;系統( 「暫停」);看起來更好