[help] writing function

This is a discussion on [help] writing function within the C Programming forums, part of the General Programming Boards category; I want to write a function that get password and save it in a variable in main function. I dont ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103

    [help] writing function

    I want to write a function that get password and save it in a variable in main function. I dont know what should I do!

    I know maybe a function like this exist in standard library, but I should do it by myself. couse it's a part of my first university project

    I just don't know what should be function Input and output?

    -----------------------------------
    please forgive me. I know my English is bad

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    If you just want to get a string from the user, that's pretty easy -- just use fgets() or scanf() or something.

    However, I'm guessing that you want a password with asterisks ('*'s) in place of characters. In that case, there's no standard way to do this. However, you can achieve it with Dev-C++'s getche().
    [edit]http://guesswhere/viewtopic.php?t=113[/edit]
    (You'll have to modify that code slightly to use getche() instead of getch(). The only difference between the functions is that getch() does not echo the characters that were typed, while getche() does. I'm pretty sure that Dev-C++ doesn't have getch().)

    Again, however, that is non-standard, and you should consider carefully whether you really need starred-out passwords.

    [edit] I suppose I shouldn't have gone and posted a link to an existing solution.

    Once you read tabstop's post below, here is an algorithm you might use to read a password from the user. Read a single character from the user. (This has to be using a non-standard function like getche() that doesn't wait for enter to be pressed before registering a keypress, but rather returns it instantly.) If the character is enter, return from the function with the saved password. If it's backspace, print "\b \b" -- going back one character, printing a space to erase the character that was printed, and then going back again. If it's anything else, store the character and print a '*'.
    [/edit]
    Last edited by dwks; 05-30-2008 at 10:36 PM. Reason: Removed link
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by behzad_shabani View Post

    I just don't know what should be function Input and output?
    Do you mean that you were instructed/advised to write two functions, input and output? For your description of the problem involves no output.

    But: you need to write a function that gets a password and puts it in a variable. So you need to know things:
    (1) How to write a function in general
    (2) Where the password is (in a file, typed from the keyboard, other)
    (3) How to get the password from your answer in (2)
    (4) What kind of variable you want to put it in
    (5) How you're going to communicate back to the main program (pass-by-reference, return value, other)

    That should be a start.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    I know maybe a function like this exist in standard library
    There are functions to read input but not specifically to read a 'password'. What does it mean exactly? you want to display '*' instead of the actual characters entered by the user?

    I just don't know what should be function Input and output
    Your specifications are not really precise, so I would say you gives a pointer to an allocated string plus the size of the allocated string (which should correspond to the variable in main you're speaking of) as inputs and the function could return an int indicating how many characters it has read for instance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

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