plzzzzzzzz

Question:


Last edited on
Your code is broken in a few places... I'll help you fix it, but you need to complete it.

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
 
#include <iostream>
#include <string>
void user (string name);
void password (string pass);
void list (void);


void main ()
{
	string name=""; // this is c++, lets use a string
	cout << "enter username: "; // c++ again, proper IO streams
        cin.ignore();
        getline(cin, name);
	user(name);
}
void user (string)
{
        /* It's good practice to use descriptive names */
	string auth_name="student";
        /* This does nothing here, move its intended purpose into the if statement */
	//strcmp(name[],"student");
	if(name == auth_name)
	{
                string pass = "";
                cout << endl << "Please enter your password: ";
                cin.ignore();
                getline(cin, pass);
		password(pass);
	}
	else
	{
		cout << endl << "ERROR NAME";
		break;
	}
}

void password (string pass)
{
	auth_pass="cs";
	if (pass == auth_pass)
	{
		cout << endl << "You are authorized user";
     	        list();
	}
	else
	{
		cout << endl << "WRONG";
	        break;
	}
}

void list ()
{

}
Last edited on
Your code is not coded properly and has lots of errors. And also I see that you are using C and not C++. Working with strings in C is a pain but since you started it in C:

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 <string.h>

int user_name(char name[]);
int password(char passw[]);
int list();

int main()
{
    //Declare variables to hold username and password and give a good size to stop overflow errors
    char usrn[15];
    char pass[15];
	
    printf("Enter username: ");
    scanf("%s", &usrn); //Get the username
    if(user_name(usrn) == 1) //Check whether the username is correct or not; exit if wrong
    {
	return 0;
    }
    printf("Enter password: ");
    scanf("%s", &pass); //Get the password
    if(password(pass) == 1) //Check whether the password is correct or not; exit if wrong
    {
	return 0;
    }
    puts("\"You are authorized user\"");
    list();
    return 0;
}

int user_name(char name[])
{
    if(strcmp(name, "student") != 0) //Compares the user's input and the correct username
    {
        puts("WRONG USERNAME!");
        return 1; //Returns 1 if not same
    }
    return 0; //Return 0 if same (no need 'else' because if its same the if statement won't be executed)

}

int password(char passw[])
{
    if(strcmp(passw, "cs") != 0) //Compares the user's input and the correct password
    {
        puts("WRONG PASSWORD!");
        return 1; //Returns 1 if not same
    }
    return 0; //Return 0 if same
}

int list()
{
    
}
Last edited on
Nice work Stormboy...

Only thing I would change is return 1; from main if there's an error.

Dax.
Last edited on
thank you all
Topic archived. No new replies allowed.