Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,094 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,600 people online right now. Registration is fast and FREE... Join Now!




function help

 
Reply to this topicStart new topic

function help

jrice528
16 Oct, 2007 - 05:18 PM
Post #1

New D.I.C Head
*

Joined: 29 Sep, 2007
Posts: 31


My Contributions
I finished all the programs you guys have helped me with in past, been huge help. My teach return my function program and wants me to edit it, so the function only had one line of code in it. I think figured it out but i am not gettin output i was looking for. Have to use a function for this program and it cant have arguments. Any suggestions on what i should do so get the right output of heads and tails

.

CODE
#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);
    
}

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Function Help
16 Oct, 2007 - 09:39 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,198



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Try setting your for loop up like this...

CODE

for (unsigned i = 0; i < toss; i++)  
{
      // Compare the return value against zero.
      if (getFlip() == 0)
            tails++;
      else
            heads++;
}


Notice how I use the result of the function call and compare. The problem you had was that you were not comparing the result of your method and instead just incrementing tails once, then heads the rest of the time (since after incrementing it once, tails no longer equaled zero).

This should function as you wanted.

Edit: Oh and I wanted to let you know that you should be using return types and not just "unsigned". You are probably getting some warnings because of this. Try using int or if you want unsigned int.

Enjoy! decap.gif

This post has been edited by Martyr2: 16 Oct, 2007 - 09:41 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:22PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month