This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

help me out...

i am working on following project.
& its showing me some errors... & i am unable to remove them... please help me out...

program:-

/*HEADER FILES*/
#include<reg51.h>
#include<string.h>

/*LCD PIN CONNECTIONS*/
#define lcd P0
sbit rs=P0^1;
sbit en=P0^3;

/*LCD FUNCTIONS DECLARATIONS*/
void init_lcd(void);
void cmd_lcd(unsigned char);
void data_lcd(unsigned char);
void str_lcd(unsigned char *);
void Delay_ms(unsigned int);

/*KEYPAD PIN CONNECTIONS*/
sbit row0=P2^3;
sbit row1=P2^5;
sbit row2=P2^6;
sbit row3=P2^0;
sbit col0=P2^4;
sbit col1=P2^2;
sbit col2=P2^1;

/*MOTOR PIN CONNECTIONS*/
sbit M1=P1^0;
sbit M2=P1^5;

/*BUZZER PIN CONNECTION*/
sbit BUZZER=P1^2;

/*VARIABLES DECLARATION*/
unsigned char i,j;
unsigned char colval,rowval,pwdchange;

unsigned char pwd[15],temp[],str1[]="12345";

unsigned char keypad[4][3]={'1','2','3','4','5','6','7','8','9','*','0','#',};
/*PASSWORD FUNCTION DECLARATION*/
void password(void);

/*KEYPAD FUNCTION DECLARATION*/
unsigned char key(void);

/*MAIN FUNCTION*/
main()
{ unsigned char k=0; BUZZER=0; //BUZZER OFF M1=M2=0; //MOTOR OFF

init_lcd(); //LCD INITIALIZATION FUNCTION CALLING str_lcd("ENTER PASSWORD:"); //DISPLAY STRING ON LCD while(1) { cmd_lcd(0xc0); //2ND LINE DISPLAY password(); //PASSWORD FUNCTION CALLING if(pwdchange) { pwdchange=0; continue; } if(!strcmp(str1,pwd)) //COMPARING WITH 1ST PASSWORD { BUZZER=0; //BUZZER OFF cmd_lcd(0xc0); str_lcd("DOOR OPEN");

M1=0; //MOTOR RUNS IN FORWARD DIRECTION M2=1; Delay_ms(30); //30 MILLISECONDS DELAY cmd_lcd(0xc0); str_lcd(" "); M1=0; //MOTOR OFF M2=0;
Delay_ms(1500);

cmd_lcd(0xc0); str_lcd("DOOR CLOSE");

M1=1; M2=0; Delay_ms(30); cmd_lcd(0xc0); str_lcd(" "); M1=0; //MOTOR OFF M2=0; k=0;

} else {

strcpy(temp,pwd); cmd_lcd(0x01); str_lcd("Confirm Password"); cmd_lcd(0xc0); password(); if(!strcmp(temp,pwd)) { strcpy(str1,temp); cmd_lcd(0x01); str_lcd("Password'Changed"); Delay_ms(1000); cmd_lcd(0x01); str_lcd("Enter Password"); cmd_lcd(0xc0); pwdchange=1; return; } else { cmd_lcd(0x01); str_lcd("Password Error"); Delay_ms(1000);
cmd_lcd(0x01); str_lcd("Enter Password"); cmd_lcd(0xc0); pwdchange=1; return; } } else { cmd_lcd(0x01); str_lcd("Password Error"); Delay_ms(1000); cmd_lcd(0x01); str_lcd("Enter Password"); cmd_lcd(0xc0); pwdchange=1; return; } } else {goto label;

} else { goto label; }

else { label:pwd[i++]=j; data_lcd('*'); }

pwd[i]='\0';
}

//its showing 4 syntax error near else...

Parents
  • There are no syntax for if with multiple else. There can only be zero or one else statement for every if statement. As I already mentioned, it is a logical impossibility to know which of multiple else to use if there isn't an "if" statement with a logical expression that performs the select of which of the two alternatives to use.

    In some situations, it is possible to use a "computed goto".

    One implementation of that is the switch statement in C.

    Another implementation (better suited for processor architectures that doesn't suffer huge costs from indirect memory accesses) is tables with function pointers.

    But your code is not a situation for any computed goto. You do not have a "one-out-of-ten" problem. You have a sequential problem. Each action can potentially trig a following action.

    If A fails, we are done. Else we do B. If B fails we are done, else we do C.

    This is a trivial problem that can be seen as having a number of postit notes and move around them until you get the most "beautiful" structure.

    Note that the outcome of nested if/else can change drastically depending on if you favour the "if" part or the "else" part. Is it the "if" part that represents a failure? Or the "else" part?

    A slightly naughty method for multiple error exits is the abuse of a loop:

    do {
        if (!do_a()) break;
        if (!do_b()) break;
        ...
        if (!do_z()) break;
        // wow - we managed everything.
        return good_result;
    } while (0);
    // ouch - something went bad
    do_error_recovery();
    return bad_result;
    

    But no - your problem is not a problem that should make use of the above code. Your teacher would probably give you a long speech if you try that route.

    So why not just consider the following?

    if (xx) {
        do_a();
    } else if (yy) {
        do_b();
    } else if (zz) {
        do_c();
    }
    

    By the way - several days later. Aren't your deadline up yet? This is not a tough problem. It's rather easy. And there are a number of acceptable solutions. But "break" and "goto" should not be needed or used for this problem.

Reply
  • There are no syntax for if with multiple else. There can only be zero or one else statement for every if statement. As I already mentioned, it is a logical impossibility to know which of multiple else to use if there isn't an "if" statement with a logical expression that performs the select of which of the two alternatives to use.

    In some situations, it is possible to use a "computed goto".

    One implementation of that is the switch statement in C.

    Another implementation (better suited for processor architectures that doesn't suffer huge costs from indirect memory accesses) is tables with function pointers.

    But your code is not a situation for any computed goto. You do not have a "one-out-of-ten" problem. You have a sequential problem. Each action can potentially trig a following action.

    If A fails, we are done. Else we do B. If B fails we are done, else we do C.

    This is a trivial problem that can be seen as having a number of postit notes and move around them until you get the most "beautiful" structure.

    Note that the outcome of nested if/else can change drastically depending on if you favour the "if" part or the "else" part. Is it the "if" part that represents a failure? Or the "else" part?

    A slightly naughty method for multiple error exits is the abuse of a loop:

    do {
        if (!do_a()) break;
        if (!do_b()) break;
        ...
        if (!do_z()) break;
        // wow - we managed everything.
        return good_result;
    } while (0);
    // ouch - something went bad
    do_error_recovery();
    return bad_result;
    

    But no - your problem is not a problem that should make use of the above code. Your teacher would probably give you a long speech if you try that route.

    So why not just consider the following?

    if (xx) {
        do_a();
    } else if (yy) {
        do_b();
    } else if (zz) {
        do_c();
    }
    

    By the way - several days later. Aren't your deadline up yet? This is not a tough problem. It's rather easy. And there are a number of acceptable solutions. But "break" and "goto" should not be needed or used for this problem.

Children
  • Per Westermark

    now its showing me 5 errors...
    '(' expected instead '{' near 1st else if statement..
    and remaining syntax errors are as it is... :( :( (

    
    
    /*HEADER FILES*/
    #include<reg51.h>
    #include<string.h>
    
    /*LCD PIN CONNECTIONS*/
    #define lcd P0
    sbit rs=P0^1;
    sbit en=P0^3;
    
    /*LCD FUNCTIONS DECLARATIONS*/
    void init_lcd(void);
    void cmd_lcd(unsigned char);
    void data_lcd(unsigned char);
    void str_lcd(unsigned char *);
    void Delay_ms(unsigned int);
    
    /*KEYPAD PIN CONNECTIONS*/
    sbit row0=P2^3;
    sbit row1=P2^5;
    sbit row2=P2^6;
    sbit row3=P2^0;
    sbit col0=P2^4;
    sbit col1=P2^2;
    sbit col2=P2^1;
    
    /*MOTOR PIN CONNECTIONS*/
    sbit M1=P1^0;
    sbit M2=P1^5;
    
    
    /*BUZZER PIN CONNECTION*/
    sbit BUZZER=P1^2;
    
    /*VARIABLES DECLARATION*/
    unsigned char i,j;
    unsigned char colval,rowval,pwdchange;
    
    unsigned char pwd[15],temp[],str1[]="12345";
    
    unsigned char keypad[4][3]={'1','2','3','4','5','6','7','8','9','*','0','#',};
    /*PASSWORD FUNCTION DECLARATION*/
    void password(void);
    
    /*KEYPAD FUNCTION DECLARATION*/
    unsigned char key(void);
    
    /*MAIN FUNCTION*/
    main()
    {
        unsigned char k=0;
        BUZZER=0;           //BUZZER OFF
            M1=M2=0;                //MOTOR OFF
    
            init_lcd();             //LCD INITIALIZATION FUNCTION CALLING
            str_lcd("ENTER PASSWORD:");           //DISPLAY STRING ON LCD
            while(1)
            {
                    cmd_lcd(0xc0);          //2ND LINE DISPLAY
                    password();                     //PASSWORD FUNCTION CALLING
                    if(pwdchange)
                            {
                            pwdchange=0;
                            continue;
                            }
                    if(!strcmp(str1,pwd))           //COMPARING WITH 1ST PASSWORD
                    {
                            BUZZER=0;                               //BUZZER OFF
                            cmd_lcd(0xc0);
                            str_lcd("DOOR OPEN");
    
                            M1=0;           //MOTOR RUNS IN FORWARD DIRECTION
                            M2=1;
                            Delay_ms(30);                   //30 MILLISECONDS DELAY
                            cmd_lcd(0xc0);
                            str_lcd("             ");
                            M1=0;                                   //MOTOR OFF
                            M2=0;
    Delay_ms(1500);
    
                            cmd_lcd(0xc0);
                            str_lcd("DOOR CLOSE");
    
                                    M1=1;
                            M2=0;
                            Delay_ms(30);
                            cmd_lcd(0xc0);
                            str_lcd("             ");
                            M1=0;                                   //MOTOR OFF
                            M2=0;
                            k=0;
    
                    }
                    else
                    {
    
                                                            strcpy(temp,pwd);
                                                            cmd_lcd(0x01);
                                                            str_lcd("Confirm Password");
                                                            cmd_lcd(0xc0);
                                                            password();
                                                            if(!strcmp(temp,pwd))
                                                                    {
                                                                    strcpy(str1,temp);
                                                                    cmd_lcd(0x01);
                                                                    str_lcd("Password'Changed");
                                                                    Delay_ms(1000);
                                                                    cmd_lcd(0x01);
                                                                    str_lcd("Enter Password");
                                                                    cmd_lcd(0xc0);
                                                                    pwdchange=1;
    
                                                                    }
                                                            else if
                                                                    {
                                                                    cmd_lcd(0x01);
                                                                    str_lcd("Password Error");
                                                                    Delay_ms(1000);
                                                                    cmd_lcd(0x01);
                                                                    str_lcd("Enter Password");
                                                                    cmd_lcd(0xc0);
                                                                    pwdchange=1;
    
                                                                    }
                                                            }
                                                    else if
                                                            {
                                                             cmd_lcd(0x01);
                                                            str_lcd("Password Error");
                                                            Delay_ms(1000);
                                                            cmd_lcd(0x01);
                                                            str_lcd("Enter Password");
                                                            cmd_lcd(0xc0);
                                                            pwdchange=1;
    
                                                            }
                                            }
                                            else if
                                            {goto label;
    
                                            }
                                    else if
                                    {
                                    goto label;
                                    }
    
    
                    else
                    {
                    label:pwd[i++]=j;
                              data_lcd('*');
                }
    
            pwd[i]='\0';
    }
    
    
    

  • You are having a laugh, aren't you?

  • You need to go back to absolute basics!

    www.loirak.com/.../ctutor.php

    Look at

    3. Loops and Conditions
    

  • You need to go back to absolute basics!

    www.loirak.com/.../ctutor.php

    even better, get a C book e.g. Kochan "programming in ANSI C" and learn C on the PC. C51 is not a good learning platform.

    Erik

  • Absolutely!

    You have basic 'C' syntax errors - you need to go back to your 'C' textbook, and your 'C' class notes, and study them carefully!!

    More basic 'C' learning links here: blog.antronics.co.uk/.../

  • You have basic 'C' syntax errors

    I would argue that knowing C syntax isn't the biggest issue here.

    For someone wanting to figure out a way to utilize multiple "else"s following one "if", growing a functional brain is probably far more urgent.