2013-12-16 58 views
0

我在創建一個比較兩個數組的程序;第一個數組,用戶輸入5個整數。第二個數組,計算機隨機選擇5個數字(兩個數組之間爲0 &)。C++需要刷新變量

我需要比較每個數組的元素以查看它們是否匹配。如果它們匹配,我給計數器變量加1。

(例如用戶輸入0 2 2 9 6,計算機[使用蘭特]拾取2 3 2 9 0 3 elemnts 4 &匹配,所以這就是2。現在每個匹配具有不同的情形)

我問題:用戶還輸入了他們想要比較這些數組的次數(這是一個名爲pick 5的遊戲)。 我需要計數器重置爲零,每次它循環我試過使用flush命令,但它沒有工作。

有什麼想法?

int main() 
{ 
string name; 
float total_amt, bet_amt, win_amt; 
int play_amt, user_choice[5] = { }, com_choice[6], i, total=0; 
int size = 5; 

srand((unsigned)time(0)); 

cout<<"Welcome to pick 5, where the odds are never in your favor!"<<endl; 
cout<<"What is your name?"<<endl; 
cin>>name; 
cout<<"How much money do you have?"<<endl; 
cin>>total_amt; 
cout<<"How much money would you like to bet?"<<endl; 
cin>>bet_amt; 
cout<<"How many times would you like to play?"<<endl; 
cin>>play_amt; 

for (i=0;i<play_amt;i++) 
{ 
    cout<<"Pick 5 integers between 0 & 9 "<<endl; 

    cin>>user_choice[0]; 
    cin>>user_choice[1]; 
    cin>>user_choice[2]; 
    cin>>user_choice[3]; 
    cin>>user_choice[4]; 

    com_choice[0] = (rand()%9); 
    com_choice[1] = (rand()%9); 
    com_choice[2] = (rand()%9); 
    com_choice[3] = (rand()%9); 
    com_choice[4] = (rand()%9); 

    cout<<com_choice[0]; 
    cout<<com_choice[1]; 
    cout<<com_choice[2]; 
    cout<<com_choice[3]; 
    cout<<com_choice[4]; 



    if (user_choice[0]==com_choice[0]) 
    { 
     total++; 
    } 
    if (user_choice[1]==com_choice[1]) 
    { 
     total++; 
    } 
    if (user_choice[2]==com_choice[2]) 
    { 
     total++; 
    } 
    if (user_choice[3]==com_choice[3]) 
    { 
     total++; 
    } 
    if (user_choice[4]==com_choice[4]) 
    { 
     total++; 
    } 

    cout<<"User matched "<<total<<"times"<<endl; 


    // scenarios 
    if (total>=0 &&total<3) 
    { 
     total_amt-=bet_amt; 
     cout<<"Less than 3 of your numbers matched, tough luck"<<endl; 
     cout<<"You now have $"<<total_amt<<"dollars left."<<endl; 
    } 
     else if (total == 3) 
     { 
      cout<<"You matched 3 and broke even"<<endl; 
     } 
     else if(total == 4) 
     { 
      win_amt = bet_amt*2; 
      total_amt = total_amt + (win_amt - bet_amt); 
      cout<<"Congrats! you matched 4!!"<<endl; 
      cout<<"You now have $"<<total_amt<<"dollars!"<<endl; 
     } 
     else 
     { 
      win_amt = bet_amt*5; 
      total_amt = total_amt + (win_amt - bet_amt); 
      cout<<"WINNER WINNER YOU MATCHED 5!!"<<endl; 
      cout<<"You now have $"<<total_amt<<"dollars!"<<endl; 
     } 
    // end nested if-else 

} // end for loop 

} // end main 
+1

不確定我完全理解,沒有變量稱爲「counter」。你不是隻想設置總數= 0;在你的「for」循環的開始? – OldProgrammer

+0

元素從0開始。 –

+0

正如答案所述,移動變量是一種好方法。如果您發現自己處於需要重置某些內容的情況,'var = {};'通常可以很好地執行此操作。 – chris

回答

1

沒有必要在功能開始時「預先聲明」所有變量。

如果你想要的變量「重置」,將變量聲明你使用它們,其中,內環路:

for (int i=0;i<play_amt;i++) 
{ 
    int total = 0; // A new variable 'total', that starts at 0 each time through the loop. 
+0

謝謝!代碼正在以我想要的方式工作。 – user2480658

+0

@ user2480658,這是很好的禮儀,對於網站來說非常重要,它能夠很好地接受最能解決您問題的答案。 – chris

+0

要做到這一點,我點擊響應左側的綠色複選標記嗎? – user2480658

1

你應該進入的委託任務功能的習慣。您在total上遇到問題,因爲您的main()函數太長,很難跟蹤該變量發生的情況。如果您的for循環看起來像這樣:

for (i=0;i<play_amt;i++) 
{ 
    read_choices(user_choice); 
    choose_random(com_coice); 
    display_choice(com_choice); 

    total = count_matches(); 
    cout<<"User matched "<<total<<"times"<<endl; 

    total_amt += evaluate_scenario(total, bet_amt); 
} // end for loop                   
+0

我首先想讓程序以我想要的方式運行(正確的邏輯,我仍然想像你說的那樣將這些任務委託給自己的函數,然後我想把所有這些都放在一個類中。 – user2480658