Вопрос

I'm creating a program that compares two arrays; the first array, user inputs 5 integers. the second array, the computer selects 5 numbers at random (between 0 & 9 for both arrays).

I need to compare the elements of each array to see if they match. if they match, i add 1 to a counter variable.

( for example user inputs 0 2 2 9 6, computer [ using rand ] picks 2 3 2 9 0. elemnts 3 & 4 match, so thats two. now each match has a different scenario )

My problem: the user also inputs how many times they want to compare these arrays (it's a game called pick 5 ). I need the counter to reset to zero each time it loops I tried using the flush command, but it didn't work.

any ideas?

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
Это было полезно?

Решение

There's no need to "pre-declare" all variables at the start of function.

If you want the variables to "reset", move the variable declarations to where you're using them, inside the loop:

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

Другие советы

You should get into the habit of delegating tasks to functions. You are having a problem with total because your main() function is so long it's hard to keep track of what's happening to that variable. You'll have less trouble if your for loop looks like this:

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                                                                        
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top