C++ Keeping Track of the Longest Streak
<p>For my computer programming class, I decided to make a program that generates a random coin flip and keeps track of the longest consecutive streak for both heads and tails. I have searched the internet and found no answer. The count is not displaying right. Even just a hint would be great!
Thanks,
Justin</p>

<pre><code>int main(){

    int number_of_flips;
    int coin_flip;
    int previous_flip = 2;
    int head_count = 0;
    int tail_count = 0;
    int highest_head = 0;
    int highest_tail = 0;

    srand(time(NULL));

    cout &lt;&lt; "Enter the number of coin flips:" &lt;&lt; endl;
    cin &gt;&gt; number_of_flips;
    system("cls");

    for(int i = 0; i &lt; number_of_flips; i++){


        coin_flip = rand() % 2;

        if(coin_flip == 0){
            cout &lt;&lt; "Heads" &lt;&lt; endl;
            if(coin_flip == previous_flip){
                  head_count = head_count + 1;       
             }
             else{
                 if(head_count &gt; highest_head){
                     highest_head = head_count;                  
                 }

                 head_count = 0;      
             }
        }

        if(coin_flip == 1){
            cout &lt;&lt; "Tails" &lt;&lt; endl;
            if(coin_flip == previous_flip){
                  tail_count = tail_count + 1;        
            }
            else{
                 if(tail_count &gt; highest_tail){
                      highest_tail = tail_count;              
                 }

                 tail_count = 0;     
            }
        }


        previous_flip = coin_flip;
    }

    cout &lt;&lt; "The longest run of heads is " &lt;&lt; highest_head &lt;&lt; endl;
    cout &lt;&lt; "The longest run of tails is " &lt;&lt; highest_tail &lt;&lt; endl;

    system("pause");
    return 0;
}
</code></pre>

<p>Here is an example of the output:</p>

<pre><code>Tails
Tails
Tails
Heads
Heads
Tails
Tails
Tails
Tails
Heads
The longest run of heads is 1
The longest run of tails is 2
</code></pre>

<p>As a reference, here is my final code that I believe works now:</p>

<pre><code>int main(){

    int number_of_flips;
    int coin_flip;
    int previous_flip = 2;
    int head_count = 0;
    int tail_count = 0;
    int highest_head = 0;
    int highest_tail = 0;

    srand(time(NULL));

    cout &lt;&lt; "Enter the number of coin flips:" &lt;&lt; endl;
    cin &gt;&gt; number_of_flips;
    system("cls");

    for(int i = 0; i &lt; number_of_flips; i++){


        coin_flip = rand() % 2;

        if(coin_flip == 0){
            cout &lt;&lt; "Heads" &lt;&lt; endl;
            if(coin_flip == previous_flip){
                  head_count = head_count + 1;       
             }
             else{
                 if(head_count &gt; highest_head){
                     highest_head = head_count;                  
                 }

                 head_count = 1;      
             }
        }

        if(coin_flip == 1){
            cout &lt;&lt; "Tails" &lt;&lt; endl;
            if(coin_flip == previous_flip){
                  tail_count = tail_count + 1;        
            }
            else{
                 if(tail_count &gt; highest_tail){
                      highest_tail = tail_count;              
                 }

                 tail_count = 1;     
            }
        }
        previous_flip = coin_flip;
    }
    if(head_count &gt; highest_head){
           highest_head = head_count;              
    }
    if(tail_count &gt; highest_tail){
           highest_tail = tail_count;              
    }
    cout &lt;&lt; "The longest run of heads is " &lt;&lt; highest_head &lt;&lt; endl;
    cout &lt;&lt; "The longest run of tails is " &lt;&lt; highest_tail &lt;&lt; endl;

    system("pause");
    return 0;
}
</code></pre>
