Creating a LOGIN (username and password)

Im tryin to create a login that accepts a user login and password. it should accept 3 tries in which when the user enters their username the system should tell whether if the username is correct if not it should display a message saying invalid username and directs the user to register like entering their dob and name. BUT I CANT SEEM TO GET THAT PART RUN CORRECTLY IF I ENTER THE USERNAME INVALID CAN ANYONE EDIT THIS CODE TO SEE WHERE I GO WRONG PLZZZZ!!!!!!!




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


void main()
{
char userId[10];
char passwrd[10];
int p = 0;
char uoid[] = "ndrew";
char opasswrd[] = "program";
int chances = 3;


while (chances)
{
printf("Enter User ID:\n");
scanf("%s", &userId);
printf("\n");
printf("Enter Your Password:\n");
scanf("%s", &passwrd);

if (strcmp(uoid, userId) == 0 && strcmp(passwrd, opasswrd) == 0)
{
p = 1;
break;
}
else
{
printf("Invalid User Id And Password\n");
chances--;
}
}

if (p==1)
{
printf("Correct User Id And Password\n");
}
else
{
printf("You have no more chances...");
}

getch();
}
closed account (28poGNh0)
New version!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <string.h>

int main()
{
    char userId[10],passwrd[10];
    char uoid[] = "ndrew",opasswrd[] = "program";
    int chances = 3;

    while (chances)
    {
        printf("Enter User ID -> ");
        scanf("%s", userId);
        printf("\n");
        printf("Enter Your Password -> ");
        scanf("%s", passwrd);

        if (strcmp(uoid, userId) == 0 && strcmp(passwrd, opasswrd) == 0)
        {
            printf("Correct User Id And Password\n");
            break;
        }
        else
        {
            printf("Invalid User Id And Password\n\n");
            chances--;

            if(!chances)
                printf("You have no more chances...");
        }
    }

    getch();

    return 0;
}
Thank you so much Techno1 but there a little problem still how would i make the program ask for the user to register to login if their username is not valid after it runs the 3 chances to login? could u provide a solution where after that it makes the user do a register function to accept there name and age then send them back to login successfully.

Earlier solution!!!


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

int main()
{
char userId[10],passwrd[10];
char uoid[] = "ndrew",opasswrd[] = "program";
int chances = 3;

while (chances)
{
printf("Enter User ID -> ");
scanf("%s", userId);
printf("\n");
printf("Enter Your Password -> ");
scanf("%s", passwrd);

if (strcmp(uoid, userId) == 0 && strcmp(passwrd, opasswrd) == 0)
{
printf("Correct User Id And Password\n");
break;
}
else
{
printf("Invalid User Id And Password\n\n");
chances--;

if(!chances)
printf("You have no more chances...");
}
}

getch();

return 0;
}
closed account (28poGNh0)
We can do so much things but I think you're looking for basics so..

another new version!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <string.h>

int main()
{
    char userId[10],passwrd[10];
    char uoid[] = "ndrew",opasswrd[] = "program";
    int chances = 3;
    char cChoice;

    while (chances)
    {
        printf("Enter User ID -> ");
        scanf("%s", userId);
        printf("\n");
        printf("Enter Your Password -> ");
        scanf("%s", passwrd);

        if (strcmp(uoid, userId) == 0 && strcmp(passwrd, opasswrd) == 0)
        {
            printf("Correct User Id And Password\n");
            break;
        }
        else
        {
            printf("Invalid User Id And Password\n\n");
            chances--;

            if(!chances)
            {
                printf("You have no more chances...\n");
                printf("Do you want to register y/n -> ");

                cChoice = getch();
                printf("\n");

                if(cChoice=='Y'||cChoice=='y')
                {
                    printf("Enter your new ID -> ");
                    scanf("%s",uoid);
                    printf("Enter your new PW -> ");
                    scanf("%s",opasswrd);

                    chances = 3;
                }
            }
        }
    }

    getch();

    return 0;
}

closed account (28poGNh0)
PS : when you register for the first time your new ID takes the place of the old one "ndrew" and you password same way

if you tried to log in with ndrew and program after a register yo'll probably be surprised

if you want to "ndrew" and "program" data be unteachable well that what I meant by
We can do so much things

Last edited on
Ok here is a clearer explanation of what i'm doing.


Authentication:

1. Login: when this activity is performed, the user enters their user-id; the program
searches the “login.bat” or “login.txt” file for that user-id. If the user-id is not found
in the login file the program informs the user that he/she is not a registered user and
then asks if he/she would like to register with the application. If the user chooses to
register with the application, initiate the Registration functionality (see functionality
2). Otherwise, if the user-id is found ask the user to enter their password and then
compare it with a copy of the password from the “login.*” file. If the credentials
match the program displays the program menu; other wise, advise the user that their
credentials are incorrect and ask the user is he/she wish to exit the program.


2. Register: when this activity is performed, the program accepts the following
customer date: full name, date of birth, age [calculated using date of birth], email
address, contact number, password, and date registered. The program should generate
a sequential user-id. Save all data (except the password) to a file called
“customer.dat”. The following data is saved to the login file (“login.dat” “login.txt”):
user-id, password, and category. The program randomly assigns a category [Worker,
Customer] to the user and informs the User of the category he/she is assigned. After
the user registers successfully offer the login screen (see functionality 1).
closed account (28poGNh0)
I was expecting dealing with files ,do you know how to deal with files? what about functions?
functions yes, files no.
closed account (28poGNh0)
Well I think you need to start leaning files right away
I can code the program for you but I dont see the point if you cant undertand it

Yea that's what i'm doing and i like using these examples to be ahead so i become advance. i normally go through it line by line and so far your input have been perfect and very easily to read would appreciate if u could enlighten me more on the coding for this program for files.
closed account (28poGNh0)
It's been a long time since I programmed with c ,I start to forget some ..

By the way
all the functions you need are in the cstdio||stdio.h library
http://www.cplusplus.com/reference/cstdio/

start with fopen function to learn how to open a file for reading||writting
http://www.cplusplus.com/reference/cstdio/fopen/

then explore getc() putc() gets() and puts()
http://www.cplusplus.com/reference/cstdio/getc/
http://www.cplusplus.com/reference/cstdio/gets/
http://www.cplusplus.com/reference/cstdio/putc/
http://www.cplusplus.com/reference/cstdio/puts/

also fprintf and fscanf
http://www.cplusplus.com/reference/cstdio/fprintf/
http://www.cplusplus.com/reference/cstdio/fscanf/

I think thats all you need
good look

C's gets is deprecated and isn't even available in the latest standards. Don't use it. Ever. It's a guaranteed buffer overrun vulnerability.
closed account (28poGNh0)
First thanks @LB honor to have presence from someone with .. 10629 man how do you do that ?

second I know that gets is deprecated but in some contries like mine they still use the first version of dev cpp compiler ,and in the uneversity !,how do you find that ?

finaly if @LB advice not using the gets ,I advice not using the all c languge for more security

hope for more critics
this seems complicated just wanted to see how this code would implemented in this situation can u code it for me please.
closed account (28poGNh0)
I'll try to code some wait couple mins
ok no problem.
closed account (28poGNh0)
// I just did it for one member if you have a lot of person you need to consult structors

bref ,this just the simplest I can do

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <string.h>

// This function does the folowing tasks
// 1: Checks if the file exists
// 2: Gets The raw lines
// 3: extracts raw ID and PASSW
// I let errors handling to you ,now there is a standard
//   the first line containes the id the second containes the passW

void get_ID_and_PASS(char fileName[30],char *id,char *pass)
{
    FILE *F = fopen(fileName,"r");

    if(F)// 1
    {
        int count = 0;
        while(!feof(F))
        {
            char rawLine[50];

            fscanf(F,"%s",rawLine);// 2

            if(!count++)// 3
                strcpy(id,rawLine);
            else
                strcpy(pass,rawLine);

        }
    }else printf("Cannot open this file");

    fclose(F);
}

int main()
{
    char fileName[30] = "IDs_file.txt";
    char userID[20],userPassW[20];

    get_ID_and_PASS(fileName,userID,userPassW);

    int chances = 3;
    char cChoice;

    while (chances)
    {
        char strID[30]="\0",strPASSW[30]="\0";
        char IDpref[20] = "id:\0",PASSWpref[20] = "pass:\0";

        printf("Enter User ID -> ");
        scanf("%s",strID);
        printf("\n");
        printf("Enter Your Password -> ");
        scanf("%s",strPASSW);

        strcpy(strID,strcat(IDpref,strID));
        strcpy(strPASSW,strcat(PASSWpref,strPASSW));

        if (!strcmp(strID,userID)&&!strcmp(strPASSW,userPassW))
        {
            printf("Correct User Id And Password\n");
            break;
        }
        else
        {
            printf("Invalid User Id And Password\n\n");
            chances--;

            if(!chances)
            {
                printf("You have no more chances...\n");
                printf("Do you want to register y/n -> ");

                cChoice = getch();
                printf("\n");

                if(cChoice=='Y'||cChoice=='y')
                {
                    FILE *F = fopen(fileName,"w");

                    printf("Enter your new ID -> ");
                    scanf("%s",strID);
                    fprintf(F,"id:%s\n",strID);

                    printf("Enter your new PW -> ");
                    scanf("%s",strPASSW);
                    fprintf(F,"pass:%s",strPASSW);

                    fclose(F);

                    break;
                }
            }
        }
    }

    getch();

    return 0;
}


I have added on to your code a little bit @Techno01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <string.h>

/// This function does the folowing tasks
/// 1: Checks if the file exists
/// 2: Gets The raw lines
/// 3: extracts raw ID and PASSWORD
/// I let errors handling to you ,now there is a standard
///   the first line containes the id the second containes the PASSWORD

void get_ID_and_PASS(char fileName[30],char *id,char *pass)
{
    FILE *F = fopen(fileName,"r");

    if(F)// 1
    {
        int count = 0;
        while(!feof(F))
        {
            char rawLine[50];

            fscanf(F,"%s",rawLine);// 2

            if(!count++)// 3
                strcpy(id,rawLine);
            else
                strcpy(pass,rawLine);

        }
    }else printf("Cannot open this file");

    fclose(F);
}
///
int main(void)
{
    char fileName[30] = "IDs_file.txt";
    char userID[20],userPassW[20];
    char strID[30]="\0",strPASSW[30]="\0";
    char IDpref[20] = "id:\0",PASSWpref[20] = "pass:\0";
    get_ID_and_PASS(fileName,userID,userPassW);

    int chances = 3;
    char cChoice;

    printf("Do you want to register y/n -> ");

                cChoice = getch();
                printf("\n");

                if(cChoice=='Y'||cChoice=='y')
                {
                    FILE *F = fopen(fileName,"w");

                    printf("Enter your new ID -> ");
                    scanf("%s",strID);
                    fprintf(F,"id:%s\n",strID);

                    printf("Enter your new PASSWORD -> ");
                    scanf("%s",strPASSW);
                    fprintf(F,"pass:%s",strPASSW);

                    fclose(F);
                }
    while (chances)
    {
        printf("Enter User ID -> ");
        scanf("%s",strID);
        printf("\n");
        printf("Enter Your Password -> ");
        scanf("%s",strPASSW);

        strcpy(strID,strcat(IDpref,strID));
        strcpy(strPASSW,strcat(PASSWpref,strPASSW));

        if (!strcmp(strID,userID)&&!strcmp(strPASSW,userPassW))
        {
            printf("Correct User ID And Password\n");
            printf("\n\nDo something here!\n\n"); ///DO SOMETHING HERE!

            break;
        }
        else
        {
            printf("Invalid User ID And Password\n\n");
            chances--;
            system("cls");

            if(!chances)
            {
                printf("You have no more chances...\n");
                printf("Do you want to register y/n -> ");

                cChoice = getch();
                printf("\n");

                if(cChoice=='Y'||cChoice=='y')
                {
                    FILE *F = fopen(fileName,"w");

                    printf("Enter your new ID -> ");
                    scanf("%s",strID);
                    fprintf(F,"id:%s\n",strID);

                    printf("Enter your new PW -> ");
                    scanf("%s",strPASSW);
                    fprintf(F,"pass:%s",strPASSW);

                    fclose(F);

                    break;
                }
            }
        }
    }
    printf("press any key to continue");
    getch();
}
thnkz i have not gotten to really edit it much yet because i've been learning pointers this week so i havebeen quiet busy doing that for pointers kind of tricky to me don't u think? since your an expert i think i can better get this figure out if u could code it in a psuedocode format for me please.




1. Login: when this activity is performed, the user enters their user-id; the program
searches the “login.bat” or “login.txt” file for that user-id. If the user-id is not found
in the login file the program informs the user that he/she is not a registered user and
then asks if he/she would like to register with the application. If the user chooses to
register with the application, initiate the Registration functionality (see functionality

2). Otherwise, if the user-id is found ask the user to enter their password and then
compare it with a copy of the password from the “login.*” file. If the credentials
match the program displays the program menu; other wise, advise the user that their
credentials are incorrect and ask the user is he/she wish to exit the program.

2. Register: when this activity is performed, the program accepts the following
customer date: full name, date of birth, age [calculated using date of birth], email
address, contact number, password, and date registered. The program should generate
a sequential user-id. Save all data (except the password) to a file called
“customer.dat”. The following data is saved to the login file (“login.dat” “login.txt”):
user-id, password, and category. The program randomly assigns a category [Worker,
Customer] to the user and informs the User of the category he/she is assigned. After
the user registers successfully offer the login screen (see functionality 1).

3. Add Movie: when this activity is performed, the program accepts the following movie
data from a Worker: movie code, title, PG rating [13, 16, MA], duration in minutes,
director of the movie, type of movie [action, comedy, romance], status [active,
inactive]. Only ACTIVE movies can be scheduled for showing. The Worker uses this
activity to add a new movie to the “movies.dat” file. This file will have data
pertaining to all movies shown/showing at the movie theatre.


4. Update Movie: when this activity is performed, the program accepts the movie code
from the Worker and changes the corresponding movie status in the “movies.dat”
file.

5. Movie Schedule: when this activity is performed the program generate a daily movie
schedule from the “movies.dat” file based on ACTIVE movies. All movies are
schedule 3 hours apart. Movies are shown from 11:00A.M. to MIDNIGHT every day.
The following data is stored in a file called “movieschedule – dd-mm-yyyy.dat”:
movie code, title, PG rating [13, 16, MA], duration in minutes, director of the movie,
type of movie [action, comedy, romance], and show time and price of movie ticket.
Only one (1) movie can be scheduled in a cinema at a time.

6. Buy Ticket: when this activity is performed the program displays a copy of the movie
schedule for that day. The Customer enters the movie code and the program searches
the “movieschedule – dd-mm-yyyy.dat” file using the movie code and displays the
movie title, duration, PG rating, show date, show time and price of the ticket. The
programs then asks the user to enter payment for the ticket if he/she is sure they want
to buy that ticket. Payment must be greater than or equal to the cost of the ticket.
Once payment is validated the program prints receipt and automatically initiates the
Generate Movie Ticket functionality.

7. Generate Movie Ticket: when this activity is initiated, ticket details such as: movie
title, date of movie, show time, PG rating, and price is displayed for the customer to
view on the screen and then added to a file called “tickets.dat”.

Notes
• On STARTUP the program should ONLY display the login screen. See
functionality 1.
• The Worker can perform all functionalities. While the Customer menu can ONLY
perform 1, 2, 6, and 7.
• The theatre can only seat 100 persons. Therefore the number of tickets sold
CANNOT exceed the number of seats in the cinema. See functionality 6.
• A customer can ONLY purchase 2 tickets at a time. See functionality 6.

Instructions
* use of modularity, randomization, arrays, repetition, structures, unions,
enumeration, and files.The user should be able to exit a screen/area of the program at
anytime.
Topic archived. No new replies allowed.