i need to input a password but it should be printed as a special character..how can i possibly do that? thanks in advance for the answer and im sorry for the disturbance..im just starting studying this kinds of stuff..again..thank you in advance..good day..

In general you need to write your own input mechanism that reads a character without echoing and prints a suitable mask character as a manual echo. C++ doesn't support raw input like that, so you'll have to drop to a non-portable solution such as getch:

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

int main()
{
  std::string password;
  int ch;

  while ( ( ch = getch() ) != '\r' ) {
    password += (char)ch;
    std::cout.put ( '*' );
  }

  std::cout<<'\n'<< password <<'\n';
}

thank you for the reply..but i need it for c programming only..
and i'm sorry for the wrong question..i'm just a student trying to figure out some problem.. i'm just asking a question..but thank you for the correction..it is well appreciated..

>but i need it for c programming only..
Normally I'd tell you to do the conversion yourself because it's trivial, but you'd probably get it wrong and introduce buffer overflows everywhere. So I'll be nice and do the conversion for you:

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

int main ( void )
{
  char password[BUFSIZ] = {0};
  int i = 0;
  int ch;

  while ( i < BUFSIZ - 1 && ( ch = getch() ) != '\r' ) {
    password[i++] = (char)ch;
    putchar ( '*' );
  }

  printf ( "\n%s\n", password );

  return 0;
}

In the future, we have a separate forum for C questions, seeing as how C and C++ are two different languages.

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