Need help with a password program assignment

Hi there,

I am having some issues editing a program for my lab. The assignment requires converting character arrays into strings in order to enhance the security of the logins. The unedited code looks as following:

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
#include <cassert>
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>

using namespace std;

void get(char * askfor, int numchars, char * input);
void get_password(char * name, char * pwd);

int main()
{
    //  Change these character arrays to strings.

	char name[8]; char pwd[8]; char passwd[8]; 
	cout << "Address of name =" <<  &name <<"\n";
	cout << "Address of pwd =" <<  &pwd <<"\n";
	cout << "Address of passwd =" <<  &passwd <<"\n";

	bool authenticated=false;
	while(! authenticated)
	{

                // input the name here 
		get("Name", 7, name);
                // get the password pwd for the name
		get_password(name, pwd);
               // input a password passwd
		get("Password", 7, passwd);
	 	// cout <<pwd<<" vs " <<passwd<<endl;
                // compare the two passwords
		authenticated= ! strncmp(pwd,passwd,7);

		if(!authenticated)
			cout << "Please try again\n";
	}
	cout << "Welcome "<<name<<"\n";

	//...
	return 0;
}
void get(char * askfor, int numchars, char * input)
{
   // this inputs a chracter array from input ... change it to input a string
	cout << askfor<<"("<<numchars<<" characters): ";
	cin>>input;
	return;
}
void get_password(char * name, char * pwd)
{
        // Yuch! This returns pwd depending on the variable name
        // Rewrite so it accepts a string name and returns a string

	if(!strcmp(name,"botting"))
		strcpy(pwd, "123456");
	else if(!strcmp(name,"ernesto"))
		strcpy(pwd, "765432");
	else if(!strcmp(name,"tong"))
		strcpy(pwd, "234567");
	else
		 strcpy(pwd, "qwert");
	return;
}


So I edited out the characters and replaces them with strings as instructed, however the program seems unable to match up the correct name with the correct password. Thus, it constantly outputs "Please try again" even when the correct password is entered with the corresponding name. Here's my edited code:

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
#include <cassert>
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>

using namespace std;

void get(const string askfor, string input);
void get_password(const string name, string pwd);

void get(const string askfor, string input)
{

	cout << askfor <<": ";
	cin>>input;
	return;
}
void get_password(const string name, string pwd)
{

    
    if(name == "botting")
		pwd = "123456";
	else if(name == "ernesto")
		pwd = "765432";
	else if(name == "tong")
		pwd = "234567";
	else
        pwd = "qwert";
	return;
}

int main()
{    
	string name; string pwd; string passwd;
	cout << "Address of name =" <<  &name <<"\n";
	cout << "Address of pwd =" <<  &pwd <<"\n";
	cout << "Address of passwd =" <<  &passwd <<"\n";
    
	bool authenticated=false;
	while(! authenticated)
	{
  
		get("Name", name);
		get_password(name, pwd);
		get("Password", passwd);
        
        if (pwd == passwd)
            authenticated;
        if (!authenticated)
			cout << "Please try again\n";
	}
	cout << "Welcome "<<name<<"\n";

	return 0;
}


I know I'm probably over thinking this and it's simple, but any suggestions or help/advice would be appreciated. :)
Unlike char*s, which when accessed inside a function affects the pointed-to data outside of the functions, strings do not. So when you pass pwd to your unction get_password() you get a copy that you assign to which is then destroyed when the function returns, leaving the pwd in main() as it was before the call. You'll want to pass by reference to do this.
In other words:

1
2
3
4
5
6
7
8
9
void get(const string& askfor, string& input)
{
        ...
}

void get_password(const string& name, string& pwd)
{
        ...
}

Notice also that I caused the 'askfor' and 'name' arguments to be const references. This is how to pass a non-modifiable string with the most grace in C++.

BTW, you only need to prototype functions if you plan to use them before you give the compiler their definition (with the function body).

Hope this helps.
Hmm little confused on how pass my reference works exactly. Been trying to read it on other sites, but I can't seem to make much sense of it honestly. XD

Awesome that prototypes aren't a must here! Less editing, which I like!
Those are the only changes you need. The function calls on lines 45-47 remain the same.
Hmm only thing is nothing is still happening. Like I compile the program and enter in the correct name (e.g. "botting") and the correct password ("123456") and still it outputs "Please try again" rather than logging me in. So, I can't figure out what's wrong.
Oh, I missed line 50. Make sure you fix that.
Awesome! Thanks! I completely overlooked that! Works perfectly now. :)
Topic archived. No new replies allowed.