954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Making a program that will display the result of a coin

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
#include
#include
#include

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

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

Good luck with your adventures in C++ ...
[php]// haids or tails (Dev C++)

#include
#include // for setw(), not used here
#include
#include

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;
}
[/php]

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

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" < 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!

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 
Ja, that would work, but you have to initialize your heads and tails counters to zero.

it doesnt work at all :( I've tried this way now but even this fails to work
------------------------------------------------------------------------
#include
#include
#include
#include

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 <

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

If you want to keep track of your total heads and tails the code should look similar to this:
[php]// haids or tails (Dev C++)

#include
#include
#include

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;
}
[/php]

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

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?

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You