make text appear as an asterix

This is a discussion on make text appear as an asterix within the C++ Programming forums, part of the General Programming Boards category; The title says it all; please tell me how to make text appear as an asterix....

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    25

    Question make text appear as an asterix

    The title says it all; please tell me how to make text appear as an asterix.

  2. #2
    Registered User combatdave's Avatar
    Join Date
    Nov 2005
    Posts
    20
    cout << "*****" << endl; ???

    what text?
    ( )

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    text inputed by the user

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    You can do that with getch() as well. Remember the last post I told you how to do it? Just do getch() in a loop, echo an asterisk, and concatenate the character onto a string.

    Code:
    #include <iostream>	
    #include <conio.h>
    
    using namespace std;
    		
    int main()
                             
    {
                              
    char keyPress;
    string password = "";
    
    cout << "Please enter your password: ";
    do {
       keyPress = getch();
       if (keyPress != char(13)) { // Value of Enter
          password += keyPress;
          cout << "*";
       }
    } while (keyPress != char(13));
       
       cout << endl << password;
    
    return 0;
    }
    Last edited by SlyMaelstrom; 11-10-2005 at 07:21 PM.
    Sent from my iPadŽ

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Hm. You'd think there might be an FAQ entry...
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Heh, wow... it's right there, isn't it.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    Thank you once again for your informative answers!

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    SlyMaelstrom, I would like to ask you about an earlier post-what if you want to input more that 1 character into getch(), but perhaps less than the limit in the program?

  9. #9
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    You're better of using the prog outlined in the FAQ since it handles backspace as well...

    Why would you need to input more than one character into
    getch() anyway?

    Snippet taken from the FAQ...
    Code:
    /*
     *  An example of how to obtain unbuffered
     *  input using the conio.h library and getch()
     */
    #include <stdio.h> 
    #include <conio.h> 
    #include <ctype.h>
    #include <iostream>
    
    using namespace std; 
    
    int main()
    {
      int ch;
      char pword[100];
      int i = 0;
      
      cout<<"Enter your password\n>>";
      //fflush(stdout);
      
      while ((ch = getch()) != EOF 
              && ch != '\n' 
              && ch != '\r' 
              && i < sizeof(pword) - 1)
      {
        if (ch == '\b' && i > 0) 
        {
          cout<<"\b \b";
          //fflush(stdout);
          i--;
          pword[i] = '\0';
        }
        else if (isalnum(ch))
        {
          cout<<"*";
          pword[i++] = (char)ch;
        }
      }
    
      pword[i] = '\0';
      
      cout<<"\nYou entered >>"<< pword;
      
      cin.get();
      cin.get();
      return 0;
    }

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    I agree with treenef, my example was intentionally very trite and direct. The FAQ overs a more full program for your problem. It also gives some good tips.

    getch() is a function that returns a character from your stdin. It doesn't hold anything and nothing from your end goes into it. As treenef said, as well, there is no need to. Just read in a character and concatenate to a string. It's simple.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text adventure engine idea. thoughts?
    By suzakugaiden in forum Game Programming
    Replies: 16
    Last Post: 01-15-2006, 04:13 AM
  2. Prolog - Text adventure
    By Jeremy G in forum Game Programming
    Replies: 0
    Last Post: 06-29-2004, 06:19 PM
  3. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 06:23 PM
  4. Accessing a Specific Text Line Inside CEditView :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 04-14-2002, 08:12 PM
  5. How do I make a text box??
    By BubbleMan in forum Windows Programming
    Replies: 12
    Last Post: 08-24-2001, 02:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21