Function Problem

This is a discussion on Function Problem within the C Programming forums, part of the General Programming Boards category; Code: void passgets(char pword[],int maxlength){ int counter=0; char ch; while((ch = getch())!='\r'&&counter<maxlength-1){ if(ch =='\b'&&counter>0){ printf("\b \b"); counter--; } else{ pword[counter++]=ch; ...

  1. #1
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127

    Function Problem

    Code:
    void passgets(char pword[],int maxlength){
         int counter=0;
         char ch;
         while((ch = getch())!='\r'&&counter<maxlength-1){
                                                          if(ch =='\b'&&counter>0){
                                                                                   printf("\b \b");
                                                                                   counter--;
                                                          }
                                                          else{
                                                               pword[counter++]=ch;
                                                               putchar('*');
                                                          }
         }
    }
    I have put that together from combining code from a few of the previous threads about masking passwords. The problem is that when I test it it ends up with a bunch of random characters at the end. How do I fix this?
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Null terminate the string.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    You need to append the \0 yourself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    OK I tried adding
    Code:
    strcat(pword,'\0');
    to the end of the function but then once it hits that the progam crashes. How else should I do it?
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    pword[counter] = '\0';
    Easy innit?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    strcat(pword,'\0');
    1) strcat needs the first to be null terminated in the first place
    2) It expects a null terminated character pointer as the second

  7. #7
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Thanks Salem I wasn't thinking that it was that simple.
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 01:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM

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