c++ - C language break statement reports error
ringa_lee
ringa_lee 2017-06-22 11:53:37
0
4
1520

I don’t know where I went wrong. Please give me some advice. I’ve been looking at it for a long time and I think it’s a problem with the brackets, but I don’t know how to change it.

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
main()
{
    int rollDice();
    void delay();
    int i,result = 0 ,result1 = 0;
    printf("Game Start!!!!\n");
    result = rollDice();
    printf("%d\n",result);
    delay();
    if ((result == 7 )||(result == 11))
        {printf("Y\n");break;}
    else if ((result == 2) || (result == 3)|| (result == 12))
        {printf("N\n");}
    else
        printf("C\n");
        for (i = 0;i<7;i++)
            {result1 = rollDice();
            printf("%d\n",result1);
            if (result1==result)
                {printf("Y\n");break;}
            else if ((result1!=result)&&(i==6))
                printf("N\n");
        }
    return 0;
}
int rollDice()
{
     void delay();
     int a,b,c;
         srand((unsigned)time(NULL));
         a= rand()%6 + 1;
         delay();
         b= rand()%6 + 1;
         c = a + b;
     return c;
}
void delay()
{
    long t;
    for (t=0;t<50000000;t++)
    {

    }
}
ringa_lee
ringa_lee

ringa_lee

reply all(4)
巴扎黑

The first break is not used properly. Break must be used inside a loop. The second break is fine. It is recommended that your code format be consistent, and if there are curly braces, add them uniformly. Change the code to this:

int rollDice();
void delay();
int i,result = 0 ,result1 = 0;
printf("Game Start!!!!\n");
result = rollDice();
printf("%d\n",result);
delay();
if ((result == 7 )||(result == 11))
    {printf("Y\n");}
else if ((result == 2) || (result == 3)|| (result == 12))
    {printf("N\n");}
else
    {printf("C\n");}
    
    
    
    
    for (i = 0;i<7;i++)
        {result1 = rollDice();
        printf("%d\n",result1);
        if (result1==result)
            {printf("Y\n");break;}
        else if ((result1!=result)&&(i==6))
            printf("N\n");
    }
    
return 0;
三叔

The break statement has two uses:
1. Used in the switch statement to exit the switch statement midway.
2. Used in loop statements to exit the current loop directly from the loop body.

The first break statement in the question does not belong to these two usages.

phpcn_u1582

There are three problems with your code:

1, main() function has no return value type

2. The two functions rollDice() and delay() are called without prior declaration

3, break usage error

Any of the above three points is enough to cause the program to report an error directly.

In addition, I advise you to master the basic language skills first, and then debug more.

迷茫

break is used to break out of while statements, switch statements, do{}while, for{} Yu opera

Not used to jump out of if/else

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template