Thread: how to create a program that he ask you to enter a password if you want to open it ?

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    10

    how to create a program that he ask you to enter a password if you want to open it ?

    hello,
    i wanna ask you about a program that he save the password that you have entered the last time and he ask you to entered it again if you want to continue and open it, so the most important part is to save the password that you have entered.
    thanks.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Save it in an external database, file, or inside the program memory itself?
    Post your code.
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    10
    this is my code, and i want to save the name that i have entered

    Code:
    #include<stdio.h>
    #include<string.h>
    int main (void)
    {
    
    
    char answer1[100];
    char answer2[100];
    
    
    printf("what's your name?\n");
    scanf("%s",&answer1);
    printf("what's your name?\n");
    scanf("%s",&answer2);
    
    
    if(strcmp(answer1,answer2)==0)
    {
    
    
    
    
    printf("good\n");
    
    
    }
    else
    {
        printf("not good\n");
    }
    return 0;
    Last edited by You Ness; 04-08-2016 at 07:54 AM.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Is this just a beginner exercise? If so, you can just save the password in a file as plain-text, and read it from the file when it's time to do the comparison.

    If you want real password protection, it will be more complicated.
    Last edited by Matticus; 04-08-2016 at 08:12 AM.

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Once again, do you want to save it to an external file/database or inside the programs memory?

    First things first. When your scanf needs to take a pointer to your char array. As of right now you have a pointer to a pointer. The name of the array is essentially a pointer to the first element in the array, meaning that scanf will suffice with the following

    Code:
    scanf("%s",answer1); //notice how the '&' was removed
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  6. #6
    Registered User
    Join Date
    Mar 2016
    Posts
    10
    Once again, do you want to save it to an external file/database or inside the programs memory?
    i want to save it inside the programs memory

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Do what you are doing with the "answers", except make sure you apply the change I mention in the scanf.
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  8. #8
    Registered User
    Join Date
    Mar 2016
    Posts
    10
    i didn't understand anything , i'm asking about saving the name taht the user choise, like when a simple program ask you to enter your password and when you forget it he tell you that your password is not valid, i'm sure that my question is clear, but i don't know why the answers is not clear!

  9. #9
    Registered User PenCzar's Avatar
    Join Date
    Apr 2016
    Location
    New York
    Posts
    7
    You can only do two things:

    1) Save the input onto the disk.(Requires that you create a text-file.)
    2) Save the program to RAM.(Data will disappear once the program has terminated.)


    To save data to a file first you need to create a file pointer to point to a file we will use to save user input.

    Code:
    FILE fp;
    Also, we will use fgets() function instead of the scanf() function.

    Full program to save user data:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define NAMECAP 15
    
    //Holds user data.
    struct data {
     
    char *password; 
    char *username; 
    }info;
    
    /*
    ***********************
    *****Program Start*****
    ***********************
    */
    
    int main(void)
    
    {
    
    //File pointer.
    FILE *fp;
    
    /*
    **********************
    ********MENU**********
    **********************
    */
    
    
    //Get username.
    printf("\nWhat is your username?: ");
    info.username = (char*)malloc(NAMECAP * sizeof(char)); //Dynamically allocate memory.
    if(info.username == NULL)
    {    
    printf("\nError: Out of memory.\n");
    exit(0);
    }
    fgets(info.username, NAMECAP, stdin);
    info.username[strlen(info.username)-1] = '\0'; //Remove '\n' from input.
    
    
    //Get password.
    printf("What is your password?: ");
    info.password = (char*)malloc(NAMECAP * sizeof(char)); //Dynamically allocate memory.
    if(info.password == NULL)
    {    
    printf("\nError: Out of memory.\n"); //If failed.
    exit(0);
    }
    fgets(info.password, NAMECAP, stdin);
    info.password[strlen(info.password)-1] = '\0'; //Remove '\n' from input.
    
    
    //Open file for appending, create a new one if it doesn't already exist.
    if((fp = fopen("data.txt", "a+")) == NULL)
    {
    printf("\nCould not open file.\n\n"); //If failed.
    exit (0);
    }
    
    fprintf(fp,"%s\n%s",info.username,info.password); //Write to file with fprintf().
    printf("%s\n%s\n",info.username,info.password); //Output to screen.
    
    //Close file.
    fclose(fp);
    
    //Free up memory.
    free(info.password);
    free(info.username);
    
    //Exit program.
    exit(0);
    
    }
    Last edited by PenCzar; 04-09-2016 at 02:43 PM.

  10. #10
    Registered User PenCzar's Avatar
    Join Date
    Apr 2016
    Location
    New York
    Posts
    7
    Correction for the first code snippet - pointer declaration should have a star to create a file pointer:

    Code:
    FILE *fp;

  11. #11
    Registered User
    Join Date
    Mar 2016
    Posts
    10
    he don't save the password, i can enter every password i want!

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Post your updated code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program : enter a password
    By slyer4ever in forum C Programming
    Replies: 16
    Last Post: 10-08-2013, 02:11 PM
  2. To enter password without displaying it
    By hello in forum C Programming
    Replies: 3
    Last Post: 01-07-2009, 11:04 AM
  3. Replies: 2
    Last Post: 01-07-2009, 10:35 AM
  4. program failing while trying to open/create file
    By 1978Corvette in forum C Programming
    Replies: 17
    Last Post: 04-26-2006, 10:55 AM
  5. how to enter a password with asterisks
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-06-2001, 12:38 PM