Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

2 Sided Coin Game (Easy)

Here are some projects of actual programs you can practice making.

2 Sided Coin Game (Easy)

Postby FlawedLogic on Sun May 10, 2009 3:44 am

Make a game that flips a coin 100 times and outputs hows many times it landed on heads and how many times it landed on tales.

This is a challenge i got from a book when i was learning Python, i had fun with it so i thought i would share :) good luck.
FlawedLogic
 
Posts: 12
Joined: Sun May 10, 2009 3:08 am

Re: 2 Sided Coin Game (Easy)

Postby Phate on Thu May 28, 2009 2:09 pm

I'm sure it could be much shorter, but meh...

Code: Select all
#include "stdafx.h"
#include <time.h>
#include <iostream>
#include <stdlib.h>
#include <sstream>

using namespace std;

void main()
{
   srand(time(0));
   char input;

   while (1>0)
   {
      int heads = 0, tails = 0;
      cout<<"\nBegin? y or n";
      cin>>input;
      if( input == 'n' || input == 'N')
      {
         break;
      }
         if(input == 'y' || input == 'Y')
         {
      
   for(int i=0; i<=49; i++)
   {
      int x = rand() % 2 +1;
      if(x == 1)
      {
         heads++;
         cout<<" heads \n";
      }
      else{
         tails++;
         cout<<" tails \n";
      }
   }                                            // end for loop
   }
   cout<<"\nTotal number of Heads: "<<heads;
   cout<<"\nTotal number of Tails: "<<tails;
   }                                           //end of while
}
User avatar
Phate
 
Posts: 20
Joined: Mon May 25, 2009 11:24 pm

Re: 2 Sided Coin Game (Easy)

Postby burgert on Tue Jun 16, 2009 5:32 pm

heres mine

Code: Select all
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand(time(0));
    int heads = 0;
    int tails = 0;
    for (int i = 0;i < 100;i++)
    {
        int randNum = rand()%2;
        if (randNum == 1)
        {
            heads++;

        }
        else if (randNum == 0)
        {
            tails++;
        }
    }
    cout << heads << endl;
    cout << tails << endl;
}

burgert
 
Posts: 4
Joined: Wed Jun 10, 2009 7:04 pm

Re: 2 Sided Coin Game (Easy)

Postby DerHenker on Fri Jun 19, 2009 11:52 am

This one was a little more stuff ive done bevore using the rand()..

Code: Select all
#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;




void coin(){

   cout<<"Lets flip a coin 100 times...  ";
   int head;
   head = 0;
   int tale;
   tale = 0;

   for(int x = 0;x <= 100;x++){
   int random;
   random = rand()% +2;
   
      if (random == 1)
      {
         head++;
      }
      else
      {
         tale++;
      }

   }
   {
         cout<<"Done!\n\n";


         cout<<"Head came: "<<head<<endl;
         cout<<"Tale came: "<<tale<<endl;

         if (head >tale)
         {
         cout<<"I guess head von with "<<head<<" times\n\n";
         }
         else
         {
         cout<<"I guess tale von with "<<tale<<" times\n\n";
         }

   }

   cout<<"Again? (Y)es/(N)o \n";

   char nextTry;
   cin>>nextTry;

   if (nextTry == 'y' || nextTry == 'Y')
   {
      coin();
   }
   else
   {
      cout<<"Thanks for playing!";
   }

}

int main(){

   srand(time(NULL));
   coin();

return 0;
}


Did a little more but in the end it does the same i guess. But i got a warning that wont go away cant put time in a unsigned int blabla... have tryed around but it wont go away :(
DerHenker
 
Posts: 11
Joined: Fri May 15, 2009 3:54 am

a question

Postby vikidobe on Wed Jul 29, 2009 5:03 am

This is my first post here, and I am just learning. I copied and pasted the above, just to see how it looked, when done. But my compiler says there are mistakes. If there were no mistakes after I did it, I was going to copy it, instead of copying and pasting it...so can you help me here? Why are there mistakes? thank you
vikidobe
 
Posts: 10
Joined: Thu Jul 23, 2009 10:39 pm

Re: 2 Sided Coin Game (Easy)

Postby programmattic on Sun Oct 18, 2009 8:39 pm

Here's the coin flipping thing:

Code: Select all

#include <iostream>

using namespace std;

int main(){
   
   int counter;
   bool flip;
   srand(time(NULL));

   for(counter = 0; counter < 100; counter++){
            
            flip = rand() % 2;
            if(flip == 0){
                  
                  cout << "Heads\n";
                  
                  }
                  
            else if(flip == 1){
               
                   cout << "Tails\n";
               
                   }
            
            }
   
   system("pause");
   return 0;
   }
programmattic
 
Posts: 8
Joined: Sun Oct 18, 2009 5:02 pm

Re: 2 Sided Coin Game (Easy)

Postby SurowiTheKiller on Fri Nov 20, 2009 7:28 pm

Again , simple one...
Code: Select all
#include<iostream>
using namespace std;
int main()
{
    int head,tail,x,c;
    head=tail=0;
    srand(time(0));
    for(int i=0;i<100;i++)
    {
            x=rand()%2+1;
            switch(x){
            case 1:
                 head++;
                 cout<<"Head!\n";
                 break;
            case 2:
                 tail++;
                 cout<<"Tail!\n";
                 break;
            }
    }
    cout<<"\nHeads: "<<head;
    cout<<"\nTails: "<<tail<<endl;
    cout<<"Flip again?1-Yes,2-No  ";
    cin>>c;
    switch(c){
    case 1:
         main();
         break;
    case 2:
         return 0;
    default:
            return 0;
    }
}
SurowiTheKiller
 
Posts: 29
Joined: Sat Oct 10, 2009 5:09 pm
Location: windows/system32

Re: 2 Sided Coin Game (Easy)

Postby thefreakk on Tue Nov 24, 2009 5:12 pm

I tryed my own one.. tell me your ideas guys

Code: Select all
#include <iostream>
#include <cmath>


using namespace std;

int main(){
char resp;
bool redo = true;
int coinheads=0,cointales=0;

while(redo){
           
           
for(int i=0; i<100; i++){
int time = rand()%2;         
while(time == 0){
           cointales++;
           break;
                }
while(time == 1 ){
           coinheads++;
           break;
                 }
      }
 
     

cout << "\ncoin landed on tales " << cointales << " times ";
cout << "\ncoin landed on heads " << coinheads << " times \n";

cointales = 0 , coinheads = 0;

cout << "\n Do it again? \t (y) Yes or (n) No\n" << endl;
cin >> resp;
redo = (resp == 'y') ? true : false;

}      //while(redo)
system("pause");
} //main
thefreakk
 
Posts: 6
Joined: Sun Nov 22, 2009 1:42 pm


Return to Projects

Who is online

Users browsing this forum: No registered users and 0 guests