I am so close to finsihing my homework im on last programmign assignment. I have the program written but i cant figure out what to take from main and put into my flip function. the teacher said it should only contain one lineof code. Anbody have any ideas?Which is :

Write a C++ program that uses the rand(), the srand(), and time() functions from the cstdlib and ctime libraries to simulate coin tossing. Your program should ask the user how many times the coin will be tossed. For each toss of the coin, the program should keep track of which side of the coin (e.g. Heads or Tails) appears, and it should count the total for each side. At the end ot tossing the coin the user-specified number of times, the program should print the results. The program should call a separate function named flip() that takes no arguments and returns 0 for tails and 1 for heads.

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

// Function prototype

unsigned getFlip();

int main()
{
// Variables   
    
   unsigned toss;
   unsigned heads = 0;
   unsigned tails = 0;
   srand(time(0));

cout<< "Please enter how many times you'd like to flip the coin"<<endl;
cin>> toss;

for (unsigned i = 0; i < toss; i ++)  
{ 
         if (rand() % 2)
  { 
         tails ++;
  }
         else
  {
         heads ++;
  }
    
}

// How many times the coin hit heads and tails

cout<<"You have flipped tails "<<tails<<" times"<<endl;
cout<<"You have flipped heads "<<heads<<" times"<<endl;

//Initializing the time, and the total number of flips.

   unsigned total = 0;
   total = heads + tails;
   
time_t rawtime;
time ( &rawtime );
cout<<"The day of "<<ctime (&rawtime)<<"You flipped the coin "<<total;
cout<<" number of times"<<endl;

system("pause");
return 0;
    
}


// function flip
// returns 0 for tails and 1 for heads



unsigned getFlip()
{

}

Dani AI

Generated

Your core problem is that the getFlip() return value is never used. The function should be a tiny one-liner (as pointed out) and each loop iteration must read that return value and update the counters accordingly. In the code in Post #3 you call getFlip() but then test if (tails == 0) — that checks the counter, not the coin result.

Use this pattern inside the loop: call getFlip(), store the result, then increment the right counter.

for (unsigned i = 0; i < toss; ++i) {
    unsigned outcome = getFlip();   // outcome: 0 = tails, 1 = heads
    if (outcome == 1)
        ++heads;
    else
        ++tails;
}

Practical tips and checks:

  • Seed once (e.g., srand(time(0));) before the loop. Do not reseed inside getFlip().
  • Validate user input (cin) so toss is a sensible positive integer before the loop.
  • Keep the mapping consistent: decide and document whether 0 means tails and 1 means heads, then use that everywhere.
  • system("pause") is nonportable; remove it or replace with a portable wait if required for your environment.
  • For large numbers of flips, use a wider integer type (e.g., unsigned long long or size_t) to avoid overflow of heads/tails.
  • If you need better randomness for learning beyond homework, prefer C++11 <random> (e.g., std::mt19937 and std::uniform_int_distribution) instead of rand().

This addresses what said about calling the function inside the loop and fixes the incorrect counter check you used in Post #3.

Recommended Answers

All 3 Replies

>>the teacher said it should only contain one lineof code.

int flip()
{
    return rand() % 2;
}

Ok I am not gettin this, sorry for the ignorance, hehe first program using functions. What would i implement into main from here. Again sorry spent hours trying to figure this out and writing and rewriting the code, but i cant seem to get it right.

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

// Function prototype

unsigned getFlip();

int main()
{
// Variables   
    
   unsigned toss;
   unsigned heads = 0;
   unsigned tails = 0;
   srand(time(0));

cout<< "Please enter how many times you'd like to flip the coin"<<endl;
cin>> toss;

for (unsigned i = 0; i < toss; i ++)  
{ 

getFlip();

if (tails == 0)
   tails ++;
else
    heads++;
}

// How many times the coin hit heads and tails

cout<<"You have flipped tails "<<tails<<" times"<<endl;
cout<<"You have flipped heads "<<heads<<" times"<<endl;

//Initializing the time, and the total number of flips.

   unsigned total = 0;
   total = heads + tails;
   
time_t rawtime;
time ( &rawtime );
cout<<"The day of "<<ctime (&rawtime)<<"You flipped the coin "<<total;
cout<<" number of times"<<endl;

system("pause");
return 0;
    
}


// function flip
// returns 0 for tails and 1 for heads



unsigned getFlip()
{
    return (rand() % 2);
    
}

once you have a function, you can implement it wherever you want...

in this case, you should include it inside a for loop that counts the times you want your coin flipped... it will return 1 for heads and 0 for tails (or whatever you wish it represents...)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.