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...
its showing me some errors
please ship me a crystal ball IMMEDIATELY I know no other way to see aht errors you got
and learn to read (especially the text over an entry window)
Erik
"help me out..." Can you explain exactly how that would represent a "Summary"? You did notice that the field name was "Summary", did you?
Maybe you could exmplain how many threads on this forum that are people who want help? If everyone wrote "help me out", there would be thousands and thousands of "help me out" threads.
What if one user decided to instead write "can't understand syntax error" - wouldn't that suddenly give some form of summary to what the thread is about? Too hard to come to figure out such a strange summary? Or did you feel it would be too specific?
Take a look at this picture: www.danlhenry.com/.../keil_code.png - what did you miss...?
/*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'; }
Is this a new Keil extension to the C language?
if (...) { ... } else { ... } else { ... } else { ... }
1) What was the reason you did not specifically mark the source code lines you got compilation errors from? Or did you expect every reader of this thread to create a new Keil uVision project (trying to figure out what settings that may be relevant) and compile the code?
2) Skip using goto. Try Google and note the common view on the use of goto as a method to structure a C/C++ program. There could potentially exist one situation where a goto could help structurally. That is to do the job of a multi-level break statement. But even then, it is very easy to rewrite the code to not need any goto. In short - a goto is a developers failure to structure the code properly.
3) You have managed a huge number of return in your code. That doesn't always help the readability. But it becomes even more interesting when you have code like:
if (xx) { ... return; } else { goto xx; }
Note that the "return" statement makes that "else" statement meaningless, since the logic is the same as:
if (xx) { ... return; } goto xx;
But then that "goto" suddenly don't happen in a way that makes it meaningful anymore, since the decision to do that goto can be pushed further down/later.
In the end, that kind of code can often be rewritten as:
if (xx) { do_something(); } else if (yy) { do_something_else(); } else if (zz) { do_something_yet_different(); } else { see_got_here_with_zero_goto_in_source(); }
... he has lots of time on his hands.
I, however, do not have any intention of going through 100s of lines of code for free
post an extraction (or a small sample program) that show the problem
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!
any decent editor will have a button "convert tabs to spaces"