| | |
Compiling errors with braces!
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
There are quite a lot of mistakes in your program (still!). So instead of pointing out each and every of them, I just whipped up my own code and have offered explanations through comments.
The above code is still a li'l crappy since it uses non-portable getch(). I couldn't figure out any other way of getting the password without it being echoed to the screen. Maybe someone else will show a way.
The reason for not using scanf(or gets) can be found here. You can read other related articles too over there.
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <conio.h> #include <string.h> int main() { char *loginID[] = {"user1", "user2", "user3"}; char *passwords[] = {"pass1", "pass2", "pass3"}; char login[6]; char pass[6], temp; int i, loginNumber; printf("Enter the login id\n"); fgets(login,6,stdin); // Use fgets instead of gets or scanf(%s) for(i=0; i < 3; i++) // Need to see if the entered login ID is any one of the three login IDs { if(!strcmpi(loginID[i],login)) //strcmpi() ignores case break; } if(i == 3) //If the login ID didn't match in any of the three cases { printf("Invalid login id\n"); getchar(); exit(0); } loginNumber = i; //need to store the subscript of the login ID login[6] = '\0'; //NULL terminated string printf("Enter the password\n"); while((temp = getc(stdin)) != '\n') // flush the input. Google it! Don't use fflush(stdin) ; for(i=0; (temp = getch()) != '\r'; i++) //Loop executes till 'enter' is pressed { printf("*"); pass[i] = temp;// store character in pass } pass[6] = '\0'; if(!strcmp(passwords[loginNumber], pass))//compare with the appropriate loginID. Use strcmp { printf("\nAccess granted!\n"); getchar(); exit(0); } else { printf("\nAccess denied!\n"); getchar(); exit(0); } return 0; }
The above code is still a li'l crappy since it uses non-portable getch(). I couldn't figure out any other way of getting the password without it being echoed to the screen. Maybe someone else will show a way.
The reason for not using scanf(or gets) can be found here. You can read other related articles too over there.
•
•
Join Date: Jan 2009
Posts: 14
Reputation:
Solved Threads: 0
•
•
•
•
There are quite a lot of mistakes in your program (still!). So instead of pointing out each and every of them, I just whipped up my own code and have offered explanations through comments.
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <conio.h> #include <string.h> int main() { char *loginID[] = {"user1", "user2", "user3"}; char *passwords[] = {"pass1", "pass2", "pass3"}; char login[6]; char pass[6], temp; int i, loginNumber; printf("Enter the login id\n"); fgets(login,6,stdin); // Use fgets instead of gets or scanf(%s) for(i=0; i < 3; i++) // Need to see if the entered login ID is any one of the three login IDs { if(!strcmpi(loginID[i],login)) //strcmpi() ignores case break; } if(i == 3) //If the login ID didn't match in any of the three cases { printf("Invalid login id\n"); getchar(); exit(0); } loginNumber = i; //need to store the subscript of the login ID login[6] = '\0'; //NULL terminated string printf("Enter the password\n"); while((temp = getc(stdin)) != '\n') // flush the input. Google it! Don't use fflush(stdin) ; for(i=0; (temp = getch()) != '\r'; i++) //Loop executes till 'enter' is pressed { printf("*"); pass[i] = temp;// store character in pass } pass[6] = '\0'; if(!strcmp(passwords[loginNumber], pass))//compare with the appropriate loginID. Use strcmp { printf("\nAccess granted!\n"); getchar(); exit(0); } else { printf("\nAccess denied!\n"); getchar(); exit(0); } return 0; }
The above code is still a li'l crappy since it uses non-portable getch(). I couldn't figure out any other way of getting the password without it being echoed to the screen. Maybe someone else will show a way.
The reason for not using scanf(or gets) can be found here. You can read other related articles too over there.
well..thanks a ton..!!
I will refer to google for understanding the comments and find their explanations..I am still trying to comprehend this program and what all was edited in it..!
thnx again...i shall disturb you again with my problem if any..
Modesty is the true virtue of the educated. Conceit always comes as a bonus to the hollow and haughty..!
Know thyself..
Know thyself..
![]() |
Similar Threads
- Function calling (C)
- compiler doesnt like my if/else statements?? (C++)
- compilation error (C++)
- major help (C++)
Other Threads in the C Forum
- Previous Thread: How to define MSWIN
- Next Thread: Image rotation
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab win32api windows.h





