943,831 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 879
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jan 28th, 2009
0

Re: Compiling errors with braces!

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.
Reputation Points: 124
Solved Threads: 18
Junior Poster
devnar is offline Offline
148 posts
since Sep 2008
Jan 28th, 2009
0

Re: Compiling errors with braces!

Click to Expand / Collapse  Quote originally posted by devnar ...
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..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
orthographer is offline Offline
14 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: How to define MSWIN
Next Thread in C Forum Timeline: Image rotation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC