Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. int main(){
  2. char uname, passwd;
  3.  
  4. printf("Enter your username: ");
  5. scanf("%c", uname);
  6. printf("Enter your password: ");
  7. scanf("%c", passwd);
  8.  
  9. printf(" n");
  10.  
  11. if (uname == "waw" && passwd == "wow"){
  12. printf("You have logged inn");
  13. } else {
  14. printf("Failed, please try againn");
  15. }
  16. return 0;
  17. }
  18.  
  19. log.c: In function ‘main’:
  20. log.c:7:10: warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
  21. scanf("%c", uname);
  22. ^
  23. log.c:9:10: warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
  24. scanf("%c", passwd);
  25. ^
  26. log.c:13:14: warning: comparison between pointer and integer
  27. if (uname == "waw" && passwd == "wow"){
  28. ^
  29. log.c:13:33: warning: comparison between pointer and integer
  30. if (uname == "waw" && passwd == "wow"){
  31. ^
  32.  
  33. #include <stdio.h>
  34.  
  35. int main(){
  36. int res;
  37. char uname[40]; // char arrays instead of single char
  38. char passwd[40];
  39.  
  40. printf("Enter your username: ");
  41. res = scanf("%39s", uname); // Set a maximum number of chars to read to avoid overflow
  42. if (res != 1) exit(1); // Error on stdin
  43. printf("Enter your password: ");
  44. res = scanf("%39s", passwd);
  45. if (res != 1) exit(1); // Error on stdin
  46.  
  47. printf(" n");
  48.  
  49. // Use strcmp
  50. if ((strcmp(uname, "waw") == 0) && (strcmp(passwd, "wow") == 0)){
  51. printf("You have logged inn");
  52. } else{
  53. printf("Failed, please try againn");
  54. }
  55. return 0;
  56. }
  57.  
  58. char *uname, *passwd;
  59.  
  60. uname = malloc(X); //where X is the number of bytes you want to allocate for uname
  61. if (uname == NULL)
  62. printf ("Error allocating memory for uname.");
  63. passwd = malloc(Y); //where Y is the number of bytes you want to allocate for passwd
  64. if (passwd == NULL)
  65. printf ("Error allocating memory for passwd.");
  66.  
  67. #define MAX_LENGTH_UNAME X //where X is the maximum length for uname, for instance 25
  68. #define MAX_LENGTH_PASS Y //where Y is the maximum length for passwd, for instance 20
  69. ....
  70. char [MAX_LENGTH_UNAME] uname;
  71. char [MAX_LENGTH_PASS] passwd;
  72.  
  73. scanf("%s", uname);
  74. scanf("%s", passwd);
  75.  
  76. if ( (strcmp(uname, "waw") == 0) && (strcmp(passwd, "wow") == 0) ) {
  77. printf("You have logged inn");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement