-4

I am trying to make a program that will randomly show the outputs of a coin flipping until there are 5 heads in a row and then the program has to stop. I do not have a lot of experience coding so any help is appreciated! So far all I have is a program that outputs the result of a coin flip one time.

#include <iostream>
#include <cstdlib>    
#include <ctime>    

using namespace std;   

int main()   
{   
  srand (time(NULL));
  int flip = (rand()%2)+ 1 ;
  cout<<coin;
  return 0;
}
Patrick
  • 1,615
  • 6
  • 18
  • 27
Joshua Ayala
  • 7
  • 1
  • 2

2 Answers2

1

Use something like this - a while loop. This kind of loop will continue to run while the condition in the parentheses is True. Once it is False it will break out of the loop.

This may not be exactly what works for you but this is the basic outline of the logic behind what you are trying to achieve.

Feel free to comment below if you have more questions.

int numberOfHeads = 0;

while (numberOfHeads <= 5) {
    flipCoin(); // or whatever method is used to call a coin flip -- you can replace this line with whatever logic works best for you

    if (coinIsHeads == true) { // again you may need to change the logic in the parentheses for however you wish to test for a head.
       numberOfHeads++;
    }
}
KSigWyatt
  • 1,347
  • 1
  • 17
  • 31
  • #include #include #include using namespace std; int main() { bool testHeads; srand (time(NULL)); int coin = (rand()%2)+ 1 ; for (coin == 1;coin<5;coin++) { coin = (rand()%2) + 1 ; cout< – Joshua Ayala Oct 12 '16 at 04:02
  • sorry I did not mean to put that . – Joshua Ayala Oct 12 '16 at 04:04
  • how do I comment the code I have now? I now have an infinite loop when I compile and adding break after the if statement is satisfied does nothing – Joshua Ayala Oct 12 '16 at 04:11
  • Do you understand why it is an infinite loop? – KSigWyatt Oct 12 '16 at 04:20
  • It's because you are re-declaring the coin variable inside your `for` loop -- Change it to `coin = coin++;` as I said that will increment the coin value. A for loop isn't the **best** loop to use in this case. Remove your `if` statement. OR Try changing your code to what I suggested and try it again. – KSigWyatt Oct 12 '16 at 04:20
0

Since this looks like homework, and it should be a way for you to learn, I will not code it for you. But, I shall rather provide you some direction.

Create an integer variable that holds the count value. What I mean by that is you have to keep count of how many times you have heads.

Read up on conditional statements, in this case a while loop would be wise, and the arguments associated to the while should be the counter and its relation to the value 5.

Assign a value for heads and tails, right now you don't know what the int flip results in. Maybe have it so that even numbers are head and odd numbers are tails. You gets these even and odd values from your flip.

Again, this should be a very simple program to write. I would highly encourage you to use your time to read your textbook (or find a beginners C/C++ book online) and understand datatypes, conditionals, compiling, ect...

Katserbot
  • 57
  • 2
  • 10