basic password program

I want to create a password checking program (which i will later use as a function in a bigger program)

I typed the following code, but there seems to be a problem with the while loop.
If u can, plz copy it and compile it to check the error (it's a logical error)

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

void main()
{
char pwd[10];
int i = 0;

cout<<"Enter password: ";
while (pwd[i]!='\n')
{
pwd[i]=getch();
cout<<"*";
++i;
}

puts(pwd);
if (!strcmp(pwd, "abc"))
cout<<"Correct";
else
cout<<"Access Denied!";
}
closed account (S6k9GNh0)
1) This is a C program.
2) Its suggested to not use conio.h, especially for the reasons your using it.
3) Use [CODE] tagging please.
4) Your method has problems. There are other ways to do this and a hundred times has it been asked. Try to google.
You never exit the loop because your string wasn't initialized to anything. So, you never hit any NULLs to terminate.
@computerquip
1) Your point of view...I am not a professional programmer(I am just a high school student), and just because I didn't use classes, inheritance (or any other C++ feature) doesn't mean it is strictly a C program.
2) OK
3) This was my first post, so I didn't knew of it. Will adhere to it from now on.
4) That's what I wanted to know, what is the problem?

@oghmaosiris
I tried to initialize it, but it didn't work...u can perhaps just reply the correction in the above code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main(int argc, char* argv[]) {
    
    char pwd[10] = {0};
    char temp;
    int i = 0;

    cout << "Enter password: ";
    while ( (temp=getch())!=13) {
        pwd[i++] = temp;
        cout << "*";
    }

    if (!strcmp(pwd, "abc"))
        cout<<"Correct";
    else
        cout<<"Access Denied!";
    return 0;
}
thanks man!

ps: u taught me a new thing (comparing with the ASCII code, rather than the escape sequence) :)..so again thanks for that
that's ok, just remember to search before you post next time.
http://www.cplusplus.com/forum/general/3570/
ok...will do so
closed account (jwC5fSEw)
1) Your point of view...I am not a professional programmer(I am just a high school student), and just because I didn't use classes, inheritance (or any other C++ feature) doesn't mean it is strictly a C program.


It isn't a C program, but it's not C++ either. main() always returns int, and I believe iostream.h is deprecated (in any case, it shouldn't be used).
Last edited on
ok..
Relax, fellas. C is virtually indistinguishable from simple C++.

-Albatross
Topic archived. No new replies allowed.