Compiling errors with braces!

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: Compiling errors with braces!

 
0
  #11
Jan 28th, 2009
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.

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4.  
  5.  
  6. int main()
  7. {
  8.  
  9. char *loginID[] = {"user1", "user2", "user3"};
  10. char *passwords[] = {"pass1", "pass2", "pass3"};
  11. char login[6];
  12. char pass[6], temp;
  13. int i, loginNumber;
  14. printf("Enter the login id\n");
  15. fgets(login,6,stdin); // Use fgets instead of gets or scanf(%s)
  16. for(i=0; i < 3; i++) // Need to see if the entered login ID is any one of the three login IDs
  17. {
  18. if(!strcmpi(loginID[i],login)) //strcmpi() ignores case
  19. break;
  20. }
  21. if(i == 3) //If the login ID didn't match in any of the three cases
  22. {
  23. printf("Invalid login id\n");
  24. getchar();
  25. exit(0);
  26. }
  27.  
  28. loginNumber = i; //need to store the subscript of the login ID
  29. login[6] = '\0'; //NULL terminated string
  30. printf("Enter the password\n");
  31. while((temp = getc(stdin)) != '\n') // flush the input. Google it! Don't use fflush(stdin)
  32. ;
  33.  
  34. for(i=0; (temp = getch()) != '\r'; i++) //Loop executes till 'enter' is pressed
  35. {
  36. printf("*");
  37. pass[i] = temp;// store character in pass
  38. }
  39. pass[6] = '\0';
  40. if(!strcmp(passwords[loginNumber], pass))//compare with the appropriate loginID. Use strcmp
  41. {
  42. printf("\nAccess granted!\n");
  43. getchar();
  44. exit(0);
  45. }
  46. else
  47. {
  48. printf("\nAccess denied!\n");
  49. getchar();
  50. exit(0);
  51. }
  52. return 0;
  53. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 14
Reputation: orthographer is an unknown quantity at this point 
Solved Threads: 0
orthographer orthographer is offline Offline
Newbie Poster

Re: Compiling errors with braces!

 
0
  #12
Jan 28th, 2009
Originally Posted by devnar View Post
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.

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4.  
  5.  
  6. int main()
  7. {
  8.  
  9. char *loginID[] = {"user1", "user2", "user3"};
  10. char *passwords[] = {"pass1", "pass2", "pass3"};
  11. char login[6];
  12. char pass[6], temp;
  13. int i, loginNumber;
  14. printf("Enter the login id\n");
  15. fgets(login,6,stdin); // Use fgets instead of gets or scanf(%s)
  16. for(i=0; i < 3; i++) // Need to see if the entered login ID is any one of the three login IDs
  17. {
  18. if(!strcmpi(loginID[i],login)) //strcmpi() ignores case
  19. break;
  20. }
  21. if(i == 3) //If the login ID didn't match in any of the three cases
  22. {
  23. printf("Invalid login id\n");
  24. getchar();
  25. exit(0);
  26. }
  27.  
  28. loginNumber = i; //need to store the subscript of the login ID
  29. login[6] = '\0'; //NULL terminated string
  30. printf("Enter the password\n");
  31. while((temp = getc(stdin)) != '\n') // flush the input. Google it! Don't use fflush(stdin)
  32. ;
  33.  
  34. for(i=0; (temp = getch()) != '\r'; i++) //Loop executes till 'enter' is pressed
  35. {
  36. printf("*");
  37. pass[i] = temp;// store character in pass
  38. }
  39. pass[6] = '\0';
  40. if(!strcmp(passwords[loginNumber], pass))//compare with the appropriate loginID. Use strcmp
  41. {
  42. printf("\nAccess granted!\n");
  43. getchar();
  44. exit(0);
  45. }
  46. else
  47. {
  48. printf("\nAccess denied!\n");
  49. getchar();
  50. exit(0);
  51. }
  52. return 0;
  53. }

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.
omg..!!

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..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC