Hello people I'm new to this forum and only been suing c++ / c for a little while now. I'm trying to seek out help with a program. I've written some of the code:
....................................................................................................

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

using std::cin;
using std::cout;
using std::endl;
using std::setw;


int main()
{
srand(time(0));

for (int counter = 0; counter <=10; counter ++)


{

    cout << setw(4) << (1 + rand()%2)
        <<endl;

if 
}


return 0;

}

......................................................................................................

however has you can see it either prints out a 1 or 2. Basically I would like it to print out a Heads or a Tails depending on which number is randomized.

So either 1 prints out heads and do doesnt.

I've tried creating and if else statement but it seems that I'll need somewhere to store the number then the loop fails... Any ideas?

Recommended Answers

All 7 Replies

Good luck with your adventures in C++ ...

// haids or tails    (Dev C++)

#include <iostream>
#include <iomanip>    // for setw(), not used here
#include <ctime>
#include <cstdlib>

using namespace std;  // for std::cin, std::cout etc.

int main()
{
  int rn; 
  
  srand(time(0));

  for (int counter = 0; counter <= 10; counter++)
  {
    rn = 1 + rand() % 2;
    if (rn  == 1)
      cout << "Heads" << endl;
    else
      cout << "Tails" << endl;
  }

  cin.get();  // wait
  return 0;
}

is they a way to display the total number of heads and tails, from what i can gather it should be something like this:

int heads;
int tails

{
if (toss == 1)
cout <<"heads "<< endl;
heads = heads + 1

else
cout << "tails" <<endl;
tails = tails + 1
} //end else statement

cout <<"number of heads" <<heads;
cout <<"number of tails" <<tails;

is they a way to display the total number of heads and tails, from what i can gather it should be something like this:

int heads;
int tails

{
if (toss == 1)
cout <<"heads "<< endl;
heads = heads + 1

else
cout << "tails" <<endl;
tails = tails + 1
} //end else statement

cout <<"number of heads" <<heads;
cout <<"number of tails" <<tails;

Ja, that would work, but you have to initialize your heads and tails counters to zero. Not every compiler will do that for you! I know it's early, but you need to {} your if else statements!

it doesnt work at all :( I've tried this way now but even this fails to work
#include <iostream> 
#include <ctime> 
#include <cstdlib> 
#include <iomanip>

using namespace std;  // for std::cin, std::cout, endl. 

int main(int)
{
    int toss;  

    int frequency1 = 0;
    int frequency2 = 0;

   srand(time(0));  

   for (int counter = 0; counter <= 10; counter++) {
   toss = 1 + rand() % 2; 

   switch ( toss ) {

   case 1:
       ++ frequency1;
           cout <<"heads";
           break;


   case 2:
       ++ frequency2;
       cout <<"tails";
       break;

   default:
       cout << "this should never occur!!";

   }
   }

   cout << toss <<setw(13) << frequency2;
   cout <<setw(13)<< frequency1;




  return 0; //succesful terminiation 
} 

for some reason the frequency is not a running total instead it seems to randomize print out 1 set of results!

whoopies my fault i forgot to tell it to pirnt out something so it makes it clear...this way works but the if else statement doesnt.

If you want to keep track of your total heads and tails the code should look similar to this:

// haids or tails    (Dev C++)

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

using namespace std;  // for std::cin, std::cout etc.

int main()
{
  int rn, heads = 0, tails = 0; 
  
  srand(time(0));

  for (int counter = 0; counter <= 10; counter++)
  {
    rn = 1 + rand() % 2;
    if (rn  == 1)
    {
      cout << "Heads" << endl;
      heads++;
    }
    else
    {
      cout << "Tails" << endl;
      tails++;
    }
  }
  cout << "there are a total of " << heads << " heads";
  cout << " and " << tails << " tails" << endl;

  cin.get();  // wait
  return 0;
}

cool thats what i was missing, now im trying to stick a function in there to calculate heads - tails but im getting a missing function header... :S

Sorry that I have to double post. I now got the code running in a function. But instead all i have in main is the call to the function.

Is it possible to get values from inside the function main.

like

int head;
int tails;

here are 2 numbers randomly selected and incremented by the outcome, however can i use these values in a function?

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.