after the 1st iteration the toss is 100 and then the iteration increases toss to 200 and then 300. Why is this happening i want toss to be made only hundred times. PLease help me.

#include <iostream>

using namespace std;
#include <cstdlib>
#include<ctime>
int toss(void);
int main(void)
{
   srand(time(0));
    int headcount=0;
    int tailcount=0;
char ht;
loop:cout<<"press h for calling out head"<<endl<<"or t for calling out tail"<<'\n'<<"e for exiting game"<<'\n';
cin>>ht;
if(ht=='h'||ht=='t')
{



for(int i=0;i<100;i++)
{
    int k=toss();
    if(k==0)
    {
        cout<<"heads"<<endl;
        headcount++;
    }
else
{
tailcount++;
cout<<"tails"<<endl;
}

}
cout<<"the number of heads is "<<headcount<<endl;
cout<<"the number of tails is "<<tailcount<<endl;
if(headcount>tailcount)
{
    cout<<endl<<"head wins"<<endl;
}
else
{
cout<<endl<<"tails wins"<<endl;
}

}
else if(ht=='e')
{
    exit(0);
}
else
{
    cout<<"wrong input";
}
cout<<"do you want to continue:press y for yes and press any key for quitting";
char ch;
cin>>ch;
if(ch=='y')
{
  goto loop;
}
else
{
    exit(0);
}
}
int toss(void)
{

    return rand()%2;

}
Member Avatar for iamthwee

if you want to go again reset the counter back to zero.

#include <iostream>
#include <cstdlib>
#include<ctime>
using namespace std;

#define ITERATIONS 100
int HEAD;

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

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

loop:
    HEAD = 0; // <- You missed such like counter reset!
    char c;
    cout << "press H for calling out head or T for calling out tail E for exiting game" << endl;
    cin >> c;

    switch(c){
        case 'h':
        case 'H':
        case 't':
        case 'T':

            int toss_result;
            for(int i=0; i<ITERATIONS; i++)
            {
                toss_result = toss_coin();
                if(!toss_result){
                    cout<<"Heads"<<endl;
                    HEAD++;
                }
                else
                cout<<"Tails"<<endl;

            }
            cout << "The number of heads is " << HEAD << endl;
            cout << "The number of tails is " << (ITERATIONS - HEAD) << endl;

            if(HEAD > (ITERATIONS/2))
                cout <<"\nHead wins! \n";
            else
                cout <<"\nTail wins! \n";
            break;

        case 'e':
        case 'E':

            exit(0);

        default:
           cout<<"\n~ Wrong input!\n";
    }

    cout << "Do you want to continue: press Y for yes and press any key for quitting.\n";
    cin >> c;

    if(c == 'y' || c == 'Y') goto loop;

    return 0;
}

I left space there for you to add that "You guessed right message" ;)

Or if you prefer your code more, just move loop: 3 places top, before

    int headcount=0;
    int tailcount=0;

"goto loop" ???????

Why not just write a loop? You can easily wrap your code in a do...while structure

Because maybe he need to show example of goto statement, I don't care about reasons. Problem is solved and that's all I care about.

Because maybe he need to show example of goto statement, I don't care about reasons. Problem is solved and that's all I care about.

@Kristian_2
You do know that there is much more to coding, than ...

Problem is solved and that's all I care about.

Program (logic) flow ... using a standard structured coding style ... will make your code MUCH easier for you ... or some-else ... to maintain.

Some (complex) error handling may sometimes yield simpler code using a goto ... otherwise best to stick with 'best practice' usage of (a) standard coding style.

Sadly I joined this community few days ago, so I'm still learning how it's all working around here, but I must say that I was active on different site for a long time (http://www.hiveworkshop.com/forums/members/-kobas-/) and I have experience working with people. Trust me, it's always better not to force your way of thinking if not asked.

Kristian. Tnq so much :)...ya i will the gaps ;)
David: I wanted to use goto so that user chooses to stop when he/she wants to
and i am not good at using do-while loops....I will try to implement it in my future programs....

Hey ... in Assembly Language, the structures we use in 'structured programming' ... are all built up from jumps / goto's ... no worries :)

And ... variety is nice ... but so is clear, clean logic flow :)

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.