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()
{

}

>>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.