-3

I'm new to C, and I was trying to create a password system with 2 questions. 1. "What is your password?" I prompted the user to answer and recorded the argument. Any input was assigned to the variable. 2."Are you sure? Do you want this password? Enter Yes or No." It would decide whether to use the answer or discard it. Everything was working smoothly until this part. I couldn't type Yes or No. It automatically assumed that I had typed yes and saved it. Can somebody please give me some advice?

#include <stdio.h>

int
main ()
{
  char password;
  printf ("Hello User.");
  printf ("Please type in your password:");
  scanf ("%d", & password);
  
  char answer;
  printf ("\nAre you sure? Do you want this password? Enter Yes or No: \n");
  scanf ("%c", answer);
  printf ("\nAnswer is %c");
  if (answer == 'Yes')
  printf ("Confirmed.");
  
  else (answer == 'No');
  printf ("OK. Thank you.");
  password = 0;
  
  return 0;
}
  • 2
    `scanf ("%d", & password);` invokes *Undefined Behavior* when you try and store an integer value in the storage of a character. You need `scanf (" %c", answer);` (note the leading `' '` space) to discard the leading `'\n'` left by your first `scanf()` call. All of this is why `scanf` is so full of pitfalls for the new C programmer, you are recommended to handle all user-input with `fgets()` and trim the trailing `'\n'` with `strcspn()`. – David C. Rankin Oct 07 '21 at 02:11
  • 1
    This code should be producing lots of warnings that highlight a lack of understanding of the C language. I recommend more studying of basic C language texts, and asking either yourself or us why the compiler is generating these warnings. – Ken Y-N Oct 07 '21 at 02:12
  • Does this answer your question? [Scanf skips every other while loop in C](https://stackoverflow.com/questions/1669821/scanf-skips-every-other-while-loop-in-c) – Ken Y-N Oct 07 '21 at 02:13
  • 2
    A variable of type `char` holds *one* `char`. I'm inclined to suspect that you want to accommodate a password longer than that. Similar applies to the answer to the confirmation question. – John Bollinger Oct 07 '21 at 02:14
  • 2
    `answer == 'No'` this is completely wrong. Please [read a book first](https://stackoverflow.com/q/562303/995714) – phuclv Oct 07 '21 at 02:57

2 Answers2

1

As noted in the comments, you likely didn't intend to store a password in a single character. Nor did you likely intend to scanf the password as an integer using, which is what you're saying with %d.

You may want to use getline instead to read an entire line, and store it in password if you declare it as char array.

Let's say you gave it room for 100 chars.

char password[100];

You might read your password in with:

getline(password, 100, stdin);

You can do the same for the second question, but should always use strcmp to compare strings in C. When the two strings are equal, the return value is 0. Be careful as this is opposite of the way that "true" is usually represented by integers in C.

if (strcmp(response, "yes") == 0) { 
    ...
}

Hopefully these tips will point you in the right direction.

Chris
  • 26,361
  • 5
  • 21
  • 42
0

Research and study the functions and syntax.

#include <stdio.h>
#include <string.h>

//function: Clears password
void clearPassword(char p[])
{     
   for(int i = strlen( p ); i>=0; i--) 
   {
       p[i]='\0';
   } 
}

int main ()
{
  char password[101]; //C version of a String
  char line[100]; //C version of a String
  char answer;
  
  printf ("Hello User.");
  printf ("Please type in your password: ");
  fgets(line, sizeof(line), stdin); // read input
  sscanf(line, "%s", &password); // store password
  
  printf ("\nAre you sure? Do you want this password? Enter Y or N: \n");
  fgets(line, sizeof(line), stdin); //read input
  sscanf(line, "%c", &answer); // store answer
  
  printf ("\nAnswer is %c \n", answer); // print answer
  if (answer == 'Y') // 
   printf ("Confirmed.");
  else 
  {
    clearPassword(password);
    printf ("OK. Thank you.");
  }
  return 0;
}

enter image description here

Jaguar Nation
  • 122
  • 11