Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
>C Program : I need the user to input his password and then have him input it once again

only if the 2 match the program must exit and save it into a file.

This is what I have got so far.

Would appreciate any further help on the same.

What I have tried:

C++
#include <stdio.h>
#include <stdlib.h>  /* For exit() function */
int main()
{
   char username[1000];
   char passwordone[1000];
   char passwordtwo[1000];

   FILE *fptr;

   fptr = fopen("program.txt", "w");
   if(fptr == NULL)
   {
      printf("Error!");
      exit(1);
   }
   
   printf("Enter your username\n");
   gets(username);

   fprintf(fptr,"%s", username);
 
   printf("Enter your password\n");
   gets(passwordone);

   printf("Enter your password again\n");
   gets(passwordtwo);

   if strcmp(passwordone,passwordtwo ==0)
   printf("The Passwords match");
   else
   printf("The passwords do not match");
   
   fprintf(fptr,"%s", password);
   fclose(fptr);

   return 0;
}
Posted
Updated 1-Aug-18 0:49am
v2

Quick fix: change from
Quote:
if strcmp(passwordone,passwordtwo ==0)
printf("The Passwords match");
else
printf("The passwords do not match");

fprintf(fptr,"%s", password);
to
C
if (strcmp(passwordone,passwordtwo)==0)
 {
   printf("The Passwords match");
   fprintf(fptr,"%s", passwordone);
 }
 else
  printf("The passwords do not match");


Please note:
  • Never, ever use gets. Do use fgets instead.
  • Saving clear text passwords is a very bad practice.


Fixed a bug, thanks to Nelek


[update]
Quote:
However I am not able to understand the concept of the while loop

You may arrange your program this way
C
#include <string.h>

int main()
{
  char username[1000];
  char passwordone[1000];
  char passwordtwo[1000];

  FILE *fptr;


  fptr = fopen("program.txt", "w");
  if(fptr == NULL)
  {
    printf("Error!");
    return -1;
  }

  printf("Enter your username\n");
  gets(username);

  fprintf(fptr,"%s", username);

  while (1)
  {
    printf("Enter your password\n");
    gets(passwordone);

    printf("Enter your password again\n");
    gets(passwordtwo);

    if ( strcmp(passwordone, passwordtwo) == 0)
      break; // this exits the while loop

    printf("The passwords do not match\n");
  }

  printf("The Passwords match\n");
  fprintf(fptr,"%s", passwordone);
  fclose(fptr);

  return 0;

[/update]
 
Share this answer
 
v5
Comments
KarstenK 1-Aug-18 6:40am    
it is better to ALWAYS use braces. ;-)
CPallini 1-Aug-18 6:41am    
That's a matter of personal taste.
Dave Kreskowiak 1-Aug-18 8:56am    
...and coding rules if you work in such a house.
CPallini 1-Aug-18 9:13am    
You know, I am a Klingon Developer.
Nelek 1-Aug-18 7:50am    
Thanks for the merit of solving the bug, but are you sure it was me?

BTW you forgot the "not exit if not matching" ;)
Hi,
C++
if strcmp(passwordone,passwordtwo ==0)
shouldn't that be
C++
if (strcmp(passwordone,passwordtwo) ==0)
or at least
C++
if strcmp(passwordone,passwordtwo) ==0 /* NOT VALID */


other point, you try to write:
C++
fprintf(fptr,"%s", password);
but "password" doesn't exist.

other point, if you want to write it down when 1 and 2 are the same... then the actions with the file should go within the if that gives OK if they are the same

and if they don't match, you should not get out of the program, which means you have to pack your code within an iteration / loop that gets ended only after matching and writing to a file.

Note: For us giving you the working code would cost not that much, but the goal is that you learn. So please think about these clues and correct your code. If you get stuck again, don't hesitate to come back and ask again.
 
Share this answer
 
v2
Comments
CPallini 1-Aug-18 6:43am    
if strcmp(passwordone,passwordtwo) ==0
Is invalid: round braces are mandatory in if statement.
Nelek 1-Aug-18 7:44am    
I was not sure (long time without compiling C / C++). I'll correct it right away. Thanks Carlo
Member 13933893 1-Aug-18 22:23pm    
Hi CPallini and Nelek.

Thank you for your valuable suggestions .

I am writing this program as a beginner and highly appreciate all your inputs .

After having gone through your suggestion and implementing the same , I am able to write the password only when they match. However I am not able to understand the concept of the while loop . If one of you guys coudl help me understand the same and give me a small example of an implementation it would be great.

This is what I have got till now
#include <stdio.h>
#include <stdlib.h> /* For exit() function */
int main()
{
char username[1000];
char passwordone[1000];
char passwordtwo[1000];

FILE *fptr;

fptr = fopen("program.txt", "w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}

printf("Enter your username\n");
gets(username);

fprintf(fptr,"%s", username);

printf("Enter your password\n");
gets(passwordone);

printf("Enter your password again\n");
gets(passwordtwo);


if (strcmp(passwordone,passwordtwo)==0)
{
printf("The Passwords match");
fprintf(fptr,"%s", passwordone);
}
else
{
printf("The passwords do not match");
}
fclose(fptr);

return 0;
}
CPallini 2-Aug-18 3:21am    
Please see my updated solution.
Member 13933893 2-Aug-18 3:50am    
Hello CPallini,

Thank you for the same. However I have some few doubts.

While I compile the same , I get the warning that "gets" is a dangerous function and to switch it to "fgets" wherever gets is used.

However on doing so and recompiling, I am receiving further errors now .

I am not sure if the arguements that is only the variable passed is sufficient.

What other parameters need to be passed on ?

Reference of the error :
test.c:22:3: error: too few arguments to function ‘fgets’
fgets(username);
^~~~~
In file included from test.c:1:0:
/usr/include/stdio.h:564:14: note: declared here
extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
^~~~~
test.c:29:5: error: too few arguments to function ‘fgets’
fgets(passwordone);
^~~~~
In file included from test.c:1:0:
/usr/include/stdio.h:564:14: note: declared here
extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)



Have you tried to compile it?

You will get some error message indicating which lines you should check:
if strcmp(passwordone,passwordtwo ==0)
That is obviously invalid C/C++ syntax. It must be
if (strcmp(passwordone,passwordtwo) == 0)

For this line the compiler complains about password being undeclared:
fprintf(fptr,"%s", password);
Should be obvious how to fix this.

The compiler might also complain about having no declaration for strcmp() (depends on the used compiler). Include string.h which contains the declaration.

Finally, never use the gets() function. Use fgets() instead (but note that this will not remove the new line from the input so that you have to do it by code).

To fulfill the requirement of writing to the file only when the passwords match, perform the writing to the output file including the opening and closing in the corresponding if block at the end of your program.
 
Share this answer
 
Comments
Nelek 1-Aug-18 7:46am    
you forgot the "not exiting when not matching" part :)
I suppose we were writing simultaneously
Jochen Arndt 1-Aug-18 7:49am    
Oh, yes. I was focused on all these errors.
But that can be done with a simple while loop.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900