Yes, this is homework, but I am trying. The problem is:

Simulate coin tossing
Program must print Heads or Tails. Toss 100 times and count the number of times each side of the coin appears. Call a function "flip" that takes no arguments and returns 0 for tails and 1 for heads.

I know I have to use random, I think I have the logic for the function correct. What I'm trying to accomplish is flip the coin, if the coin is 0 then inclement the counter for tails, or if its not 0 inclement the heads counter and cout the appropriate response. My int Main is basically all messed up because based on the examples in my text book it appears that I should have my cout statements in main, but they are also in the function on some examples. Some of the commented out lines are my attempt to debug this thing. I spent four hours getting to this point and I'm not sure where to go. I don't want to just google the answer, I really want to learn this, which is difficult for a 43 y/o system admin trying to increase his knowledge base. C++ seems really neat and I look forward to getting a better understanding of it all. I seem to remember doing these problems when I took Java back in 1998, but, its a perishable skill apparently!!

Regards

Richard

// Chapter3_3_34.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using std::cin;
using std::endl;
using namespace std;
int flip(void);

int main();

//start random generator
//	srand( time( 0 ) );

//    result = flip;

//start defenition of flip function

int flip( void )
{
	int heads = 1; //count the heads result of the flip
	int tails = 0; //count the tails result of the flip
	int headsCounter = 0; //start the counter at 0
	int tailsCounter = 0; //start the counter at 0
    int flip; //the coin toss

	flip = rand() % 2; //flip the coin
		if flip == 1
			headsCouter++;
		else tailsCounter++;
			cout << "You flipped heads: " << headsCounter "times" << endl;
			cout << "You flipped tails: " << tailsCounter "times" << endl;

}

>My int Main is basically all messed up because based on the examples in my text book it
>appears that I should have my cout statements in main, but they are also in the function on some examples.
Your main is basically messed up because it's a declaration and not a definition. ;) You need to have a body for any function you intend to run. Something like this:

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

void flip();

int main()
{
  std::srand ( static_cast<unsigned> ( std::time ( 0 ) ) );
  flip();
}

void flip()
{
  // Your coin flipping logic
}

Note that srand needs to be called only one time, or very very infrequently. With modern random number generators, a single seed goes a long way.

As for your coin flipping logic, it looks largely decent. The syntax for your conditional is wrong because you need to surround the condition in parens:

flip = rand() % 2;

if ( flip == 1 )
  headsCounter++;
else
  tailsCounter++;

Finally, you should be placing this in a loop. Right now you're only testing one coin toss, so I'd recommend an argument to the function that specifies how many tosses you want, and a counting loop that contains the tossing code.

Member Avatar for iamthwee

>Call a function "flip" that takes no arguments and returns 0 for tails and 1 for heads

I think narue's function declaration needs to be
int flip(), to adhere to the OP's homework assignment.

I would plan how to do this on paper, drawing a flow chart first. Then code the program without functions for brevity say. Once confident then use a function.

excellent!! Thanks for the help. I'll give it a shot when I get home from work.

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.