mercredi 14 février 2018

Nesting if-else statements in C

I have an assignment due for my intro to programming class that I am having some trouble on. I've been asked to create a decision tree program and the way I think I want to achieve this is by doing nested if - else statements. The code I have now is nearly complete but I keep getting this error: expected identifier or '(' before 'else'. I think this problem is coming from my bracket placement but I'm not sure how to fix it.

#include <stdlib.h>
#include <stdio.h>

int main()
{
char response;
printf("Check the gasoline gauge. Is it empty? Enter 'y' for yes or 'n' for no.\n");
scanf(" %c", &response);

if(response == 'y')
{
    printf("You are out of gas, idiot! Put gas in the tank\n");
}

else if(response == 'n')
{
    printf("Did it stop running during use or was it parked? Please enter 'r' for running or 'p' for parked.\n");
    scanf(" %c", &response);

    if(response == 'r'){
        printf("It is likely to be a failure of the generator. Please take the car to a mechanic.\n");
    }

    else if(response == 'p')
    {
        printf("Does the engine turn over normally but does not start? (y or n)\n");
        scanf(" %c", &response);

        if(response == 'y')
            {
            printf("Might be a dirty fuel injector. Put fuel injector cleaner in gas. Does it start?");
            scanf(" %c", &response);
            }
                if(response == 'y')
              {
                printf("Ok you are set now. See ya.");
              }
                else if(response == 'n')
                {
                printf("It is likely the fuel pump or spark plugs. Take it to a mechanic");
            }
            else{
                printf("Invalid answer\n");
            }
    }
        else if(response == 'n'){
            printf("Check the battery voltage. It is around 12 volts or more?");
            scanf(" %c", &response);
        }
                if(response == 'y'){
                    printf("It is probably the starter motor. Take it to a mechanic");
                }
                else{
                    printf("Your battery is not holding a charge. You need a new battery.");
                }
}
        else{
            printf("Invalid answer\n");


       }
    }
    else{
        printf("Invalid answer\n");
    }


else{
    printf("Invalid answer\n");
}

return 0;

}

Aucun commentaire:

Enregistrer un commentaire