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...
Per Westermark:-
i am jst in second year... and i don't know many things... thank you for your advice...
can you please do me a favour??
can you please post this program by correcting all its errors??? please..........................
i"ll b greatfull to you......
can you please post this program by correcting all its errors???
can I help?
here is the corrected version of your code:
/*MAIN FUNCTION*/ main() { }
hope it help.
But the big question you have to ask yourself is: Will you learn more, by receiving a rearranged piece of code, or by spending some time with the editor, trying to move around the different code blocks and see how that affects the logic?
If the goal is just to get a programming exercise passed, then the best solution would be to get someone else to rewrite the code. If the goal is to learn (which I do hope - that is normally the reason for schools), it would be way better if you do spend time with this problem.
As noted by another poster - there is no "if else else else" support in the language.
Try to rearrange the code and see what it takes to get max one "else" per "if". And what it would take to remove those "goto". The aim should be to see if you can get a sequence of code that you feel proud of.
Another thing is that if you have too much code so you can't feel you can grasp all logic in one view, then it's time to split some of the functionality into functions. Just so that you can see and understand all logic for a single function at the same time. The amount of bugs you will have in a piece of code will directly relate to how well you can look at the code and grasp the exact behaviour. That obviously demands that you can look at every "else" statement and instantly be able to point at the corresponding "if".
Per Westermark
i know i am doing wrong thing... :( i dont have that much time... its not like i dint try... i tried a lot but then also i was uable to remove those errors... even i knew that there are errors because of improper use of nested if... if i get the correct code then i know i"ll be able to grasp it....
so please do me favour nah!!
What part of the sentence: "sit on your butt and do your homework" don't you understand? How can actually learn something if others do it for you? Time is of little significance where you are at. You may have _people's_ lives in your (not)able hands one day - did you think of that? Who will do your "homework" them?!
Read comment from 1-Mar-2012 13:16 GMT.
It does not discuss nested if, but if with multiple else. There can not be two "else" to one "if".
With proper indentation, it should be instantly visible that every else have a "if" with same indentation abit higher up. If there instead is another "else", then you have stumbled, and the compiler will cry.
Programming languages are no different from normal languages. You have "if xx then yy else zz". Not "if xx then yy else zz else ww". Because this variant would not have any rule to separate either zz or ww that "else" can't be logically processed.
Proper indentation (many styles exists and are just a question of preference unless you work for a company with official guidlines) is not really optional. Everything written should follow the same indentation rules. Then you will quickly learn to see unbalanced code without even reading the text. There is a huge reason why all modern programming languages are designed to allow white space to be used to "shape" the text lines, letting the text lines create a graphical pattern that we can see without zooming in our concentration on the actual characters and keywords.
You have lack of time. How many hours did you spend on this issue yesterday? Or the day before? I'm pretty sure the teacher gave you all a reasonable amount of time for the project. But with a fixed deadline, it doesn't work to stop and wait for others to supply answers. That is just hours eating away towards that deadline. Real tasks requiers real hours spent before that deadline. Real hours builds real experience. And real experience is what you need to get through the courses. And what you need to be able to get someone to pay you if you have any intention of developing software professionally.
Proper indentation (many styles exists and are just a question of preference unless you work for a company with official guidlines) is not really optional. Everything written should follow the same indentation rules.
the bane of proper indentation is right there on your keyboard labelled 'tab'
use spaces!
Erik
any decent editor will have a button "convert tabs to spaces"
Then you need to speak to your teacher, and request extra time! He/she will, of course, expect you to give a very good reason for the extension...
"i tried a lot but then also i was uable to remove those errors"
Then, again, you need to speak to your teacher - that's the correct place to go when you're stuck with your homework!
i agree!! i"ll try again...........:)
hi!! tell me exact syntax for if with multiple else.. with using goto... as u said i changed some sentences... but its giving me same errors...:((
please explain me with some example... you are my teacher now!!
tell me exact syntax for if with multiple else.. with using goto...
I'd like to see how you do that too ;)
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.
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