Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Error comes up "else without a previous if."

0 votes
asked Feb 28 by anonymous

#include<stdio.h>
int main()
{
        int ch;
        printf("\n\t You enter a room and are met with a split in the direction you may go, left and right. Which way do you choose?");
        printf("\n\t 1. Left");
        printf("\n\t 2. Right");
        printf("\n\t 3. RUN AWAY!");
        printf("\n\n\t Make your choice ");
        scanf("%d", &ch);
        if(ch<2 && ch>0);

        switch(ch)
        {
                case 1:
                printf("\n You open the door and find an empty room, looks like nothing interesting.");
                            printf("\n 1. Head back and go Right.");
                            printf("\n 2. Stay in room and wait for the sweet release to grasp your soul,");
                            printf("\n\n\t Make your choice ");
                            scanf("%d", &ch);
                                   if(ch=1);
                                        printf("You turn around and head towards the right.");

                                        else
                                        {
                                        printf("You spend your days in the room until end of time.");
                                        break;
                                        }

                case 2:
                printf("\n You open the door and find a hallway with paintings of individuals dressed in royal garb.");
                break;

                default:
                printf("\n You run away and explode... The End");
                break;
        }

}

3 Answers

0 votes
answered Feb 28 by anonymous
please look over the semicolon in the statement if(ch==1);

remove it and you will be fine
0 votes
answered Feb 28 by Lingesh R A (190 points)
remove the semicolon in the if statement

if(ch==1);
0 votes
answered Feb 28 by PONSLIN JASH J S
your syntax is wrong: please update and check the results:-

if(ch=1){

         printf("You turn around and head towards the right.");
      }
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...