hello guys.. i hope you can help me for this codes..
im trying to make a program that will accept username and password in c++.
im using turbo c++ ide 3.0
"not dev c++".

here is my code..
but the problem is.., i want to use * to appear as password character in my codes.
but everytime i press enter key, the program consider it as *.
so i need to press enter key again to proceed to the program yet it will generate error because
the first time i pressed enter was consider as part of the password.

#include<iostream.h>
#include<conio.h>
main()

clrscr();
char a[30], b[30], c=' ' ;
cout<<"Enter Username:  ";
cin.getline(a,29);
cout<<"Enter Password:  ";
while(c!=13)
{
c=getch();
cout<<"*";
}
cin.getline(b,30);
if (a[0]=='l' && a[1]=='u' && a[2]=='f' && a[3]=='f' && a[4]=='y' && b[0]=='g' && b[1]=='a' && b[2]=='r' && b[3]=='p')
{
cout<<"password correct:";
goto tama;
}
else
{
cout<<"Password Failed!!";
getch();
return 0;
}
tama:
clrscr();
cout<<"username and password correct"<<endl;
getch();
return 0;
}

thank you guys.. i hope you will consider to help

The characters that you get from getch() should be put into the "b" array, like so:

// ...
cout << "Enter Password:  ";
int i = 0;
while((i < 29) && (c != '\n'))
{
  b[i] = getch();
  cout << "*";
  ++i;
}
b[i] = '\0';
if (a[0]=='l' && a[1]=='u' && a[2]=='f' && a[3]=='f' && a[4]=='y' && b[0]=='g' && b[1]=='a' && b[2]=='r' && b[3]=='p')
{
  // ...

im using turbo c++ ide 3.0 "not dev c++".

I hope that you are aware that you are learning to use an antiquated pre-standard variant of C++ by relying on such an ancient compiler, right? Turbo C++ 3.0 came out in 1991, and the first standard for C++ came out in 1998. Turbo C++ is literally a pre-historic compiler. Are you aware that much of what you are learning by using this compiler will have to be unlearned before you can start to program in the real world? The language and practices have evolved significantly since then, including 2 standards (in 1998 and 2011), 2 revisions to the standards (2003 and 2014), and a few technical reports / specifications (2007, 2010, 2014, and more to come). Not to mention that most people consider the real birth of C++ to be around the 2001-2003 years when real modern practices were established. You are basically learning to knock stones together to make fire, while most people are driving Formula 1 cars.

Of course, your code is riddled with non-standard stuff (and you need to learn to indent your code correctly). But if you have to persist with a pre-historic compiler, then there isn't much I can do about that. If this is required for a course (then they should update their curriculums!!), then you'll have to suffer this, but if you seriously want to learn C++, you'll have to learn modern standard C++ in addition to learning the pre-historic language that your course is teaching you.

thank you so much for your advice... it will help a lot specially in developing my skills.. i just want to learn even the prehistoric programming language to advance.. for me to understand a lot of codes and how it works.. again thank you so much .. =)

but still the codes you provided won't work. the only problem here is the enter key was considered as * and acccept as part of the password. it needs tp press enter key two times to proceed through the programs.

Since you're working with raw keyboard data, there's more work to be done than simply calling getch and printing the mask character. You need to recognize a line feed starter as well as any backspacing. Off the top of my head, something like this:

#include <iostream>
#include <cstddef>
#include <conio.h>

using namespace std;

char *read_masked_data(char buf[], size_t n, char mask, bool send_unconditional_newline = true)
{
    int size = 0;
    int ch;

    while ((ch = getch()) != '\r' && size < n - 1)
    {
        if (ch == '\b')
        {
            if (size > 0)
            {
                cout << "\b \b";
                --size;
            }
        }
        else
        {
            cout.put(mask);
            buf[size++] = (char)ch;
        }
    }

    if (ch == '\r' || send_unconditional_newline)
    {
        putch('\n');
    }

    buf[size] = '\0';

    return buf;
}

int main()
{
    char password[4];

    cout << read_masked_data(password, sizeof password, '*') << '\n';
}
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.