i'm trying to make a password system for my program. a user will input upto 20 chars, and if the password is correct, load the main program and if not, loop until the user gives up or enters the correct answer.

here is the code

void Password(char pass);

int main()
{
	/* how do i insert the call to Password() ? */
	
	cout << "test code here!"; 
}

void Password(char pass)
{
	do
	{
		char pass[20];
		cout << "Password: ";
		cout << "\n";
		cin.get(pass[20]);

		if (pass = "test")
			main();
		else
		{
			cout << "\n\nError: Incorrect Password!";
			cin.ignore(80);
		}
	}
	while( pass != 'test');
}

i have 2 problems:

1. on the line "if (pass = "test") it's giving me an:
expression must be modifyable lvalue error.

2. i'm not entirely sure how to add this into the main program, so it will run Password() before continuing.

can somebody please help me? thanks.

Edited 5 Years Ago by altXerror: n/a

lines 1 and 10 -- you must use a pointer there, e.g. void Password(char* password) Since the character array is declared inside the Password() function, the function needs no parameters. Just void Password() Then in main() all you have to do is call it

int main()
{
    Password();
}

lines 19 and 27: you can not compare a character array with a string literal using = operator. you have to call strcmp() to do that. if( strcmp(password,"test") == 0) line 20: never ever call main() for any reason. That function should only be called from the operating system. That line should only be a simple return statement. return;

Edited 5 Years Ago by Ancient Dragon: n/a

ok. i edited the code. and it now compiles.

this is the new code:

static char* pass;

void Password();
 
int main()
{
	do
	{
		Password();
	}
	while( strcmp(pass, "test") != 0);
	cout << "test code here!"; 
}
 
void Password()
{
		char pass[20];
		cout << "Password: ";
		cout << "\n";
		cin.get(pass[20]);
 
		if (strcmp(pass, "test") == 0)
			return;
		else
		{
			cout << "\n\nError: Incorrect Password!";
			cin.ignore(80);
		}
	}
}

it works, but if the user enters the wrong password, it doesn't loop correctly. you can input to the screen but nothing else happens.

is there something i missed? thanks.


ALSO: tested entering correct password it gives an "unhandled exception...... access violation error"

woah woah woah, what's going on here?

If you want a function to modify a variable as an argument to the function you can either pass by pointer or by reference.

In this case you could also return the value.

pass by reference:

void GetChar(char &ch)
{
  cin >> ch;
}

Pass by pointer:

void GetChar( char *ch )
{
  cin >> *ch;
}

Returning the value:

char GetChar()
{
  char ch = 0;
  cin >> ch;
  return ch;
}

Also please note that the maximum index of an array of 20 elements is 19.

char pass[20];
cin.get(pass[20]);

thus:

cin.get(pass[19]);//<-- gets one character into the last element of the array
//I think you meant this, however:
cin.get(pass,20);//<-- gets an entire string of characters into the array.

http://www.cplusplus.com/reference/iostream/istream/get/

Edited 5 Years Ago by pseudorandom21: wrong documentation, added reference

ok i messed up on the array. i was supposed to put 21, becasue character 20 is not supposed to be EOF.

anyways, your confusing me. cause i was told something difrent by another person.

and that still sin't answering my problems. i need help with an error message that gets displayed when the correct password is typed, and the do while loop.

the wai i set up things with the

static char* pass

&

void Password()

was what i was taught in my programming class. so, either my teacher taught me wrong or my way is also correct?

I agree with pseudorandom21 on the coding, but yes, you can do it your way.

I'm not sure what your intention is with the code on line 27 in your adjusted code. After you print out that the password is incorrect, why not just return?

djarn

it's a do while loop. if i return it'll cause errors.

unless you know of way to use return as a loop such as using like:

return 1;

something like that. a kid in my programing class could probably do that but i have never used return for anything beside calling the main function, and "return 0"

EDIT: my mistake you meant the edit.

honestly idk why i didn't use return. i never have i guess.

hmm... is it at all possable to use catch, try and throw for this.

i.e. generate an exception then repeat the code aftwards?

Edited 5 Years Ago by altXerror: n/a

At line 27, you are not in a do while loop, you're in the function Password(). You don't really even need the return statement, my question is what is the intent of line 27

(cin.ignore(80);)

If you didn't have that, then you would return from the function Password() and fall back into the do while loop in your main. The while condition would be true and you would go back to the beginning of the do while loop.

Edited 5 Years Ago by djarn: n/a

ok. did you see my edit? i made a mistake with the post.

secondly i'll try that.

ok i have no idea if it worked becase it keeps giving me an erro after i type a value in for the password.

this is the error:

Run-Time Check Failure #2 - Stack around the variable 'pass' was corrupted.

if i hit break nothing happens, if i hit continue i get

"Unhandled exception at 0x57c7d440 (msvcr100d.dll) in Password.exe 0xC0000005: Access violation reading location 0x00000000."

Now you are running into a problem with initializations. This gets back to this issue where you want to use the static char* pass. That definition is global, but in the Password() you declare a local variable char pass[20]. This is what is used in the Password() method. Since you want to use the static char* pass, you should allocate memory for it before you call the Password() method and then you should not declare it again in the Password() method.

Part of the problem is that you are trying to use this same variable in both the main() and in Password(). There are many ways around this, but not when you also want to have the static char* and the prototype of void Password()

I don't expect to be wasting my time, so I'll try to teach you a bit.

In your original code:

void Password()
{
		char pass[20];//array of 20 char variables.
		cout << "Password: ";
		cout << "\n";
		cin.get(pass[20]);//<-- as mentioned before,
//that line is calling the single character overload of the get()
//function of the istream class, of which the class instance 
//(variable, if you must)
// "cin" is an instance of.
//To see the available overloads please refer to the reference
//I posted earlier: http://www.cplusplus.com/reference/iostream/istream/get/
//Near the top of the page, in green text is the overload you are calling:
// istream& get ( char& c );
//Note, that you are calling this overload by specifying an index
//of the array, being [20] (which doesn't exist, 19 is the last as previously noted).
//by subscripting, i.e., [ ] the array you are specifying that you want
//to access a single element!

//I hope you know how pointers work, if not I don't think you will
//understand this fully.
//Because of the way pointers work, and the fact that in memory an array is
//allocated contiguously (think of a bunch of boxes in a line next to each other)
//the name you have given the array is implicitly a pointer to it's base,
//meaning, the pointer name points to the first element of the array,
//which is index 0.
//Thus, aha!  Each consecutive element can be accessed by incrementing the base
//pointer, or adding a number to it. 
//specifying a subscript (the subscript operator is [ ] ), that gives you
//the base + 20, or 1 past the end of the array. Sinking in now?
//base + 0 
// = base[0]

//So now, calling the right overload with the pointer to the array's base
//and the size of the array, the function called can determine the
//size of the array, and how much is safe to use.
//thus:
cin.get(pass,20);//<-- pass = pointer to base of array, 20 elements is array size.

		if (strcmp(pass, "test") == 0)//<-- this is proper.
			return;
		else
		{
			cout << "\n\nError: Incorrect Password!";
			cin.ignore(80);//<-- leave that there, there could
//be something left in the input stream (like a newline ('\n').

		}
	}
}

As for the logic of that function, the way you have it set up, it will terminate regardless of if the password is correct or not.

So, you may wish to loop your logic.

Try for example:

void BlockForPassword()
{
  char password[20] = {0};
  while(true){// <-- infinite while loop.
  cin.get(password,20);
  if( strcmp(password,"test") == 0 ){
     cout << "Valid!"
     return;//<-- exits the infinite loop, and lets the program continue.
  }
  else
      cout << "Invalid password." << endl;
  cin.ignore();
 }
}

I certainly haven't tested that function, I wrote it in this tiny daniweb reply box.

http://www.cplusplus.com/reference/iostream/istream/get/

Edited 5 Years Ago by pseudorandom21: forgot a brace.

Comments
completely fixed my problem.

pseudorandom21, let me just say, that you are amazing.

it now works. the block function you typed out does work, and it fixed my errors too.

i'll make sure to, in my program, write a comment that you created the function code.

thanks very much.

Well I'm glad to know you appreciate it, and it's always good to include sources. But, the point was for you to learn. Anyway, keep up the good work and try to learn how pointers work.

This article has been dead for over six months. Start a new discussion instead.