search

Home  >  Q&A  >  body text

c++ - C language break statement reports error

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_leeringa_lee2790 days ago1543

reply all(4)I'll reply

  • 巴扎黑

    巴扎黑2017-06-22 11:55:37

    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;

    reply
    0
  • 三叔

    三叔2017-06-22 11:55:37

    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.

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-06-22 11:55:37

    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.

    reply
    0
  • 迷茫

    迷茫2017-06-22 11:55:37

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

    Not used to jump out of if/else

    reply
    0
  • Cancelreply