Please can someone help me write a simple password program for my program which prints out in stars(*).Thank you.

Recommended Answers

All 15 Replies

We will need to see your attempt as Daniweb is not a homework website. However, my reply in this might give you a hint :)

You will have to change '?' to '*'

mridul.ahuja: You, and the OP, need to be made aware that getch() (and the rest of conio.h) is not a standard C++ function, but rather is one specific to Turbo C++; some later compilers and IDEs have equivalents, but that doesn't change the basic fact that it will only work for DOS-based systems or their derivatives, and only with the specific libraries that support it.

That having been said, I should also add that Standard C++ has no support for raw console input; all standard C++ I/O is by means of buffered streams, and there are no ways to enter characters without displaying them, move the cursor around the screen or alter characters displayed on previous lines. All such operations are system-specifc; there are portable libraries to do this, such as ncurses. but not even those are universally available. If you know that you are going to be writing the program for a specific platform (e.g., Windows, Linux, MacOS), then the preferred approach is to use that system's native library.

Thanks Schol-R-LEA, I almost forgot that my code was using getch(). Also thanks for brushing up my knowledge. I always thought I had been programming in C++ :/

I always thought I had been programming in C++ :/

You have (probably). But it gets interesting when extensions and libraries are thrown into the mix. Here are the high level options:

  • Standard C++: Only uses libraries and features clearly defined by the ISO standard document. It's highly portable, but quite limited.

  • Compiler-dependent C++: Only uses libraries and features supported by the compiler. This often includes standard C++, but also has extensions to the language as well as extra libraries to make your life easier. The potential problem is you're limited to that specific compiler, and sometimes even that version of the compiler.

  • Platform-dependent C++: Uses standard or compiler-dependent C++ coupled with the APIs offered by the operating system. You may or may not be limited to the compiler, depending on what OS libraries it supports, but in general platform-dependent code is more portable than compiler-dependent code.

  • Third party libraries: Any of the above can be used with them, but you're limited by what the library itself supports.

In the case of getch, it's a compiler-dependent function. So while your code might otherwise be standard C++, using a function that isn't defined by the standard document means your code's portability is affected.

However, since the standard is limited, there are things you simply can't do with standard C++. Raw input is one of them. If you need it, you have no choice but to find a non-portable solution. But in those cases, determining how portable you need to be, picking the solution that best fits that need, and documenting it is the way to go.

Actually, in C++, you can use C functions, or C++ stream functions, including doing things like turning off echo in input streams, so you can do the mapping of input characters to things like '*'. The program can still get the real data and process it appropriately.

Actually, in C++, you can use C functions, or C++ stream functions, including doing things like turning off echo in input streams, so you can do the mapping of input characters to things like '*'.

Do tell. Unless something has changed that I'm not yet aware of, this couldn't be done without depending on behavior provided by whatever shell is active.

Deceptikon is correct; text echo is a function of the shell itself, not of the stream I/O (which is the same basic model for both the C I/O functions and the C++ iostream classes). The standard C/C++ I/O operations don't know or care where the text comes from, as it always reads it from the input buffer regardless of whether it is from a console, a file, or a pipe. By the time the C/C++ operations even know that there is data to process, the shell operations have already been completed.

It is true that there are functions to communicate with the shell and turn echo on or off, but those functions are always system-specific (and in some cases, shell-specific), and not a part of the standard library.

Think of a Character in a series of books you like or a book in a series make your password the name of the Character or book or if you the same with your favourite Film, the name of the Character and then the number of Films the person playing that Character has been in.yes

DoctorH, what was this all about ? I doubt it was related to the discussion above.

#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
int main()
{
    cout<<"Please Enter Password To Continue:"<<endl;
    string pub_pass="";
    char ch;
     ch = getch();
   while(ch != 13)
   {//character 13 is enter
       pub_pass.push_back(ch);

      cout << '*';
      ch=getch();
   }

   if (pub_pass=="oop")
       cout<<"correct password"<<endl;
   else 
       cout<<"wrong password"<<endl;

    system("pause");
}

sigh basit_3, did you read the other replies in this thread? This is essentially the same as the code posted by mridul.ahuja, and has the same problem of using getch() that his code did.

sorry whats the problem with getch()

Well, I explained this already in the earlier posts, but once again: the standard C++ stream I/O library has no support for console handling. The conio.h header and library are specific to Turbo C++ (and a few libraries that copied it) and MS-DOS (or Windows console), and while that system specificity will be the case for any console handling library to some extent (there are some portable ones such as ncurses, but they aren't universally applicable), it is something that you need to at least be aware of.

please try this............................

char pass[10];
for (int i=0;i<10;i++)
{
    pass[i]=getch();
    cout<<"*";
}

@Syed Ammar

please read the previous posts in this thread,
especially the patient explanations by ...

@Schol-R-LEA & @deceptikon

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.