i have some kind of code in the error but i have searched and looked i do not no what is wrong i am getting this erro message :error C2065: 'loop' : undeclared identifier but i thought that you did not have to id loop they are not a varibale
#include <iostream>
#include <cstdlib>

using std::cout;
using std::rand;

int flip();

int main()
{
int coinToss;
int heads, tails;

heads = 1;
tails = 0;
coinToss = 100;
for(loop=0; loop<5; loop++){
for (int toss = 0; toss < coinToss; toss++)
{
flip();
if (flip()== 1)
heads = heads + 1;
else
tails = tails + 1;
}}
return 0;
cout << "Heads appear " << heads << " times.\n";
cout << "Tails appear " << tails << " times.\n";
return 0;
}

int flip()
{
return rand() % 2;
}

Recommended Answers

All 8 Replies

Remove return 0 from line 23

And add int before loop on line 14 and you're done

Member Avatar for sandyneedshelp

You will have to declare the variable loop as int in line 14. It is treated as integer inside the for loop

for(int loop=0; loop<5; loop++){

Add randomize() on line 7 for more diverse reasults

thank you for the tips but the loop funtion is still;not working correctly wha ti want is for program to run 5 time it is till only running once
#include <iostream>
#include <cstdlib>

using std::cout;
using std::rand;

int flip();

int main()
{
int rand();
int coinToss;
int heads, tails;

heads = 1;
tails = 0;
coinToss = 10;
for(int loop=0; loop<5; loop++){
for (int toss = 0; toss < coinToss; toss++)
 {
 flip();
if (flip()== 1)
heads = heads + 1;
else
tails = tails + 1;
 }
if (flip() == 1)
 {
heads++;
 cout << "H";
 }
 else
 {
 tails++;
 cout << "T";
 }
  }
cout << "Heads appear " << heads << " times.\n";
 cout << "Tails appear " << tails << " times.\n";
 return 0;
  }

 int flip()
 {
 return rand() % 2;
  }

Your nested for loop on line 15 IS looping five times.

ok i donlt see it doing that what i want is for the coin to be flipped ten time and have the display show up of how often it is flipped for each side and then i want it to rum throu the loop and diplay ten flips 4 more time it is not doing that. this is what i am getting :
HHTTTHeads appear 26 times.
Tails appear 30 times.
Press any key to continue . . .

Move line 34 to between line 36 and 37. This brings the output statement to the end of the inner for loop but still in the outer for loop. Reset heads and tails to zero each time before starting through outer loop.

Eliminate lines 26 and 31 or lines 19 to 23

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.