OK, as you can probably tell from my code, I am trying to design a program that will keep requesting a password until the right password is entered (using a Do While Loop with char instead of int).

this is my code: (this is one of my first solo pieces of coding, so go easy on me if I have done something completely wrong, as I am quite knew to this.)

#include<iostream.h>
using namespace std;

int main()
{
char pass[6];
pass[0] = 'p';
pass[1] = 'a';
pass[2] = 's';
pass[3] = 'q';
pass[4] = 'w';
pass[5] = 'e';

	do
	{
	cout<< "Enter Password:" <<endl;
	cin>>pass;
	}
	

while(pass!=pass[0],pass[1],pass[2],pass[3],pass[4],pass[5]);

return 0;
}

I am not to sure about the:

(pass!=pass[0],pass[1],pass[2],pass[3],pass[4],pass[5]);

as this was just me fiddling around with arrays etc.

When i try and compile it as it is, i get the error message:[/B]
Error E2034 cannot convert 'char' to 'char*' in function main ()

Any suggestions? :confused:

Thank you in advance :)

Recommended Answers

All 9 Replies

You're reading into the same variable that you're checking against.

Also, the comma operator doesn't mean "also check".

perhaps these snippets

char pass[] = "pasque";
char guess[100];
cin >> guess;
if ( strcmp( pass, guess ) == 0 )

> #include<iostream.h>
> using namespace std;
If your compiler supports namespaces, then it should be
#include <iostream>

Also, to support strcmp, you need
#include <cstring>

ok, thanks for that info, I will give that a try.

Although, am I able to add that snippet in and still maintain my loop?

First, you need to make variable pass one byte larger so that is can contain the normall c string null terminator. Then lines 7-13 can be simplified like this

char pass[] = "pasqwe";

Notice you don't have to specify the size of the array when initializing it with a string like I did above. The compiler will figure that out itself.

Now in line 18 you probably should use a different variable so that it can be compared to the original. Line 22 can use strcmp() function to compare the two strings instead of comparing them one character at a time. Here, variable new_password is another variable I mentioned previously that is entered on line 18.

while( strcmp(new_password, pass) != 0);

> Although, am I able to add that snippet in and still maintain my loop?
Sure, just replace "if" with "while"

Thanks very much to the help given so far, I will try and fiddle with my code with the new information I now have.

Salem, when i tried to replace if with while, after i recieved your post it didnt work, that may be because i took a couple of other things out of the code at the same time before testing it, that may be the problem ;)

So, yeh, thanks for the help, if the polished off code works fine, then I will mark this as solved, but if it messes me about again then I will post the new code and the error message(s) again.

Thanks again.

ok, all that is good, the code now works, however I have searched all over the net to find a way to some how 'lock-out' the rest of the functions of computer until the password has been entered. As everyone knows that a password program is useless if you can just close the executable batch file that is requesting the password.

any suggestions how i can go about sorting this out?

If it is unclear what it is I am asking advice for, please let me know. Otherwise, sink your teeth into this problem please :)

Thanks in advance.

If you're talking about the kind of user/pass login that windows presents you with when you first switch on the machine, then you need a lot more than this simple command line program which lives in user space.

What you have is OK for protecting the rest of that program, not for protecting the whole machine.

OK, that sounds fair, ok, how about this next situation; making the program lock say a file, or another executable program from eing run until the loop has been completed (the password has been entered) I tried to search for this after reading your post, but couldn't seem to find any useful and relevant advice, any ideas?

Thanks.

> making the program lock say a file, or another executable program from eing run
What's to stop them from using the file or running the program before even running this program?

I've no idea what you should do. It would seem on the face of it to require some pretty heavy work with the Win32 API (assuming windows), and that is something I just don't do (my experience is in embedded systems).

If you're only guarding a few other files, then perhaps your program can encrypt / decrypt them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.