0

When I run my code, it shown unreachable code. It doesn't effect what I want to print. But I just can't print the input from the user.

I tried to define CRT_SECURE_NO_WARNINGS also can't help

I expect to print the name, SID, block, total payable, but it didn't print anything out. It will only print the Name, SID, and so on but not the input user key in.

if(GDR==1 && type==3){
        room_rate=150;
        strcpy(r[i].block, "A3");
        printf("\nHow many weeks of room booking?\n");
        scanf("%i",&R_week);

        printf("\nName\t\t:");
        scanf("%s",r[i].name);
        printf("Student ID\t :");
        scanf("%s",r[i].SID);
        printf("Type\t\t  :%s\n",rm[i].type);
        r[i].room=r[i].room+1;
        A3_no=1+A3_no;

        printf("\nDo you want to change your room type?\n1 Yes\n2 No\n3 Main Menu\nPlease enter your option:");
        scanf("%i",&change);
        if(change==1){
            Room_Type();
            Details();
            goto start;}
        if(change==2){
Meal2:
            printf("\nDo you want to subscribe for Meal?\n1 Yes\n2 No\nPlease enter your option:");
            scanf("%i",&A1_Meal);
            if(A1_Meal==1){
                printf("\n\nSorry, this service is not available for subscription. Thank you.\n");
                goto Gym2;}
            if(A1_Meal ==2){
                goto Gym2;}
            else{
                goto Meal2;}

Gym2:
            printf("\nDo you want to subscribe for Gym?\n1 Yes\n2 No\nPlease enter your option:");
            scanf("%i",&A1_Gym);
            if(A1_Gym==1){
                printf("\nSorry, this service is not available for subscription. Thank you\n");
                goto Internet2;}
            if(A1_Gym==2){
                goto Internet2;}
            else{
                goto Gym2;}
Internet2:
            printf("\nDo you want to subscribe for Internet?\n1 Yes\n2 No\nPlease enter your option:");
            scanf("%i",&A1_Internet);
            if(A1_Internet==1){
                Internet_rate=15;
                printf("\nHow many weeks of subscription?\n");
                scanf("%i",&I_week);
                goto Laundry2;}
            if(A1_Internet==2){
                Internet_rate=15;
                I_week=0;
                goto Laundry2;}
            else{
                goto Internet2;}
Laundry2:
            printf("\nDo you want to subscribe for Laundry?\n1 Yes\n2 No\nPlease enter your option:");
            scanf("%i",&A1_Laundry);
            if(A1_Laundry==1){
                Laundry_rate=20;
                printf("\nHow many weeks of subscription?\n");
                scanf("%i",&L_week);

                room_total= room_rate * R_week;
                L_total = Laundry_rate * L_week;
                I_total = Internet_rate * I_week;
                r[i].total = room_total + I_total+L_total;
                printf("\n============== Payment Details ===============\n");
                printf("\nWeekly rate for the room\t: RM%i\n%i weeks of subscription\t\t: RM%i\nWeekly rate for the laundry\t: RM%i\n%i weeks of subscription\t\t: RM%i\nWeekly rate for the Internet\t: RM%i\n%i weeks of subscription\t\t: RM%i\n\nTotal amount payable is RM%i.\n",room_rate, R_week, room_total, Laundry_rate,L_week,L_total,Internet_rate,I_week,I_total,r[i].total);
                goto Pay;}
            if(A1_Laundry==2){
                Laundry_rate=20;
                L_week=0;
                room_total= room_rate * R_week;
                L_total = Laundry_rate * L_week;
                I_total = Internet_rate * I_week;
                r[i].total = room_total + I_total+L_total;
                printf("\n============== Payment Details ===============\n");
                printf("\nWeekly rate for the room\t: RM%i\n%i weeks of subscription\t\t: RM%i\nWeekly rate for the laundry\t: RM%i\n%i weeks of subscription\t\t: RM%i\nWeekly rate for the Internet\t: RM%i\n%i weeks of subscription\t\t: RM%i\n\nTotal amount payable is RM%i.\n",room_rate, R_week, room_total, Laundry_rate,L_week,L_total,Internet_rate,I_week,I_total,r[i].total);
                goto Pay;}
            else{
                goto Laundry2;}
Pay2:
            printf("==============================================\n");
            printf("\nDo you want to cancel booking or proceed to make payment?\n1 Make Payment\n2 Cancel Booking\nPlease enter your option:");
            scanf("%i",&cancel);
            if(cancel==1){
                printf("\nDo you want to make payment now or later by cash?\n1 Now\n2 Cash\nPlease enter your option:");
                scanf("%i",&pay);
                if(pay==1){
                    strcpy(r[i].PS, "Paid");
                    printf("\nPlease deposit your money in the slot provided.\nMaximum cash per deposit is 100 pieces of any combined denominations.\n");
                    printf("Thank you for your payment.\n\n");
                    goto Printing_2;
                    continue;}
                if(pay==2){
                    strcpy(r[i].PS, "Pending");
                    printf("Please proceed to counter 1. Payment must be made within 7 days. Thank you.\n\n");
                    goto Printing_2;}
                else{
                    goto Pay2;
                    break;}}
            if(cancel==2){
                goto Main_Menu;}
            else{
                goto Pay2;}
Printing_2:
            printf("\n============== Hosteler Details ===============\n");
            printf("Name\t\t: %s\nStudent ID\t: %s\nGender\t\t: %s\nBlock\t\t: %s\nRoom Number\t: %i\nType\t\t: %s\nTotal payable\t: RM%i\nPayment Status\t: %s",r[i].name,r[i].SID,rm[i].Gender,r[i].block,A3_no,rm[i].type,r[i].total,r[i].PS);
            printf("\n============================================\n");}

        if(change==3){
            goto Main_Menu;}
        else{
            goto Main_Menu;}}
7
  • Are you sure the code is unreachable? Maybe the proper conditions didn't exist for that code to be reached.
    – wallyk
    Jun 7, 2019 at 5:06
  • Did the compiler warn that the code is unreachable or did you determine that by some other means. And where's the code? Jun 7, 2019 at 5:08
  • The same code in other loop function well, I don't know why in the others, no. Jun 7, 2019 at 5:08
  • 2
    Edit the code into your question. It's unreadable in the comments.
    – dbush
    Jun 7, 2019 at 5:10
  • I'm using JetBrains Clion 2019.1.4. It shows unreachable code from Pay2 to the end of Printing Jun 7, 2019 at 5:27

1 Answer 1

2

Compiler is right your code after Pay2 is unreachable as its above laundary2 ends with if condition that make your code run to pay or back to laundary2 in any case. Pay2 isn't reached in any case in your code and I haven't seen any line that made a call of jumping to Pay2.

Your code has some logical error, as we don't know the application and full code so we can't help further.

2
  • Oh ok. I didn't notice that I should put Pay2 instead of Pay. The problem is solved. Thank you so much Jun 7, 2019 at 5:45
  • @SofhinWong Please mark the answer as accepted if you're satisfied.
    – klutt
    Jun 7, 2019 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.