-1

so the beginning of this semester i started taking a coding class and i know the basics but not all the in depth technical things could someone help me out with this error?

this is the error i get and its talking about the if else statement around line 43

if anyone knows what to do to fix this error please let me know im very confused

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

int main()
{

char response;
int flip ();
int coin, counter, tails = 0, heads = 0, dice, S1 = 0, S2 = 0, S3 = 0, S4 = 0, S5 = 0, S6 = 0;




if (response == 'C')
for (counter = 1; counter <= 1; counter++)
{
    coin = flip ();

    if (coin == 0)
    {
        cout << "T ";
        tails = tails + 1;
    }
    else if( coin == 1)
    {
        cout << "H ";
        heads = heads + 1;
    }
}
cout<<endl;
cout<<"Tails Was Tossed "<<tails<<"times"<<endl;
cout<<"Heads Was Tossed "<<heads<<"times"<<endl;
}
int flip()
{
return rand ( ) % 2;
}

if (response == 'D'){
int flip();

{
for (counter = 1; counter <= 1; counter++)
{
    dice = flip ();

    if(dice == 0)
    {
        cout<<"1 ";
        S1 = S1 + 1;
    }
    else if( dice == 1) 
    {
        cout<<"2 ";
        S2 = S2 + 1;
    }
    else if( dice == 1) 
    {
        cout<<"3 ";
        S3 = S3 + 1;
    }
    else if( dice == 1) 
    {
        cout<<"4 ";
        S4 = S4 + 1;
    }
    else if( dice == 1) 
    {
        cout<<"5 ";
        S5 = S5 + 1;
    }
    else if( dice == 1) 
    {
        cout<<"6 ";
        S6 = S6 + 1;
    }
}

cout<<endl;
cout<<"Side 1 was Rolled "<< S1 <<" times"<<endl;
cout<<"Side 2 was Rolled "<< S2 <<" times"<<endl;
cout<<"Side 3 was Rolled "<< S3 <<" times"<<endl;
cout<<"Side 4 was Rolled "<< S4 <<" times"<<endl;
cout<<"Side 5 was Rolled "<< S5 <<" times"<<endl;
cout<<"Side 6 was Rolled "<< S6 <<" times"<<endl;


}
int flip()
{
return rand( ) % 2;
}
}
Mehdi Mostafavi
  • 880
  • 1
  • 12
  • 25
  • 3
    correct intendation is crucial to avoid such problem. I cannot see at a glance where a function starts and ends in your code and I bet you also cannot – 463035818_is_not_an_ai Apr 08 '20 at 16:53
  • The problem is the if () at 43 is not inside a function. – drescherjm Apr 08 '20 at 16:54
  • Yeah this has to do with mis matched braces. Format your code properly to help avoid this. – Derek C. Apr 08 '20 at 16:55
  • After the first definition of `flip` there is a random `if` outside the function body. Also not that you have two definitions of `flip`. – Lukas-T Apr 08 '20 at 16:55
  • 1
    frankly there are too many issues to cover them all in an answer and it seems like you try to discover how the language works by trial and error, this wont get you far, you should better learn the basics from a [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – 463035818_is_not_an_ai Apr 08 '20 at 17:04
  • 1
    Side note: When you have a variables in a contiguous sequence like `S1`, `S2`, and friends, odds are very good what you really want is an array. – user4581301 Apr 08 '20 at 17:48

1 Answers1

0

I tried to reformat your snippet. There are several problems with that, but look at the result:

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

int main()
{
    char response;
    int flip ();
    int coin, counter, tails = 0, heads = 0, dice, S1 = 0, S2 = 0, S3 = 0, S4 = 0, S5 = 0, S6 = 0;

    if (response == 'C')
        for (counter = 1; counter <= 1; counter++)
        {
            coin = flip ();

            if (coin == 0)
            {
                cout << "T ";
                tails = tails + 1;
            }
            else if( coin == 1)
            {
                cout << "H ";
                heads = heads + 1;
            }
        }
    cout<<endl;
    cout<<"Tails Was Tossed "<<tails<<"times"<<endl;
    cout<<"Heads Was Tossed "<<heads<<"times"<<endl;
}

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

if (response == 'D'){
    int flip();

    {
        for (counter = 1; counter <= 1; counter++)
        {
            dice = flip ();

            if(dice == 0)
            {
                cout<<"1 ";
                S1 = S1 + 1;
            }
            else if( dice == 1) 
            {
                cout<<"2 ";
                S2 = S2 + 1;
            }
            else if( dice == 1) 
            {
                cout<<"3 ";
                S3 = S3 + 1;
            }
            else if( dice == 1) 
            {
                cout<<"4 ";
                S4 = S4 + 1;
            }
            else if( dice == 1) 
            {
                cout<<"5 ";
                S5 = S5 + 1;
            }
            else if( dice == 1) 
            {
                cout<<"6 ";
                S6 = S6 + 1;
            }
        }

        cout<<endl;
        cout<<"Side 1 was Rolled "<< S1 <<" times"<<endl;
        cout<<"Side 2 was Rolled "<< S2 <<" times"<<endl;
        cout<<"Side 3 was Rolled "<< S3 <<" times"<<endl;
        cout<<"Side 4 was Rolled "<< S4 <<" times"<<endl;
        cout<<"Side 5 was Rolled "<< S5 <<" times"<<endl;
        cout<<"Side 6 was Rolled "<< S6 <<" times"<<endl;
    }
// Oops... cannot format
int flip()
{
    return rand( ) % 2;
}
    }

Even the braces are wrong. Moreover the if (response == 'D'){ goes outside of any function. This is the reason of the compilaton error, but you need to revise the whole snippet.

Keep your code clean and well formatted.

Dmitry Kuzminov
  • 6,180
  • 6
  • 18
  • 40