我在創建一個比較兩個數組的程序;第一個數組,用戶輸入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
不確定我完全理解,沒有變量稱爲「counter」。你不是隻想設置總數= 0;在你的「for」循環的開始? – OldProgrammer
元素從0開始。 –
正如答案所述,移動變量是一種好方法。如果您發現自己處於需要重置某些內容的情況,'var = {};'通常可以很好地執行此操作。 – chris