Thread: strings inputs

  1. #1
    Registered User
    Join Date
    Oct 2010

    Question strings inputs

    guys this is where i got problem.I have written a programe using i want to take passwords using asterik(*) .i have made a function to take inputs to avoid ESC pressed or ENTER pressed. when i take 1st input via getche() it write data in string but after 1st input when i take next input using this function it take inputs but did'nt write in is the function...
    Last edited by aitzaz_cheema; 10-27-2010 at 12:29 AM.

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    United Kingdom
    I presume that you have just posted the function and not the whole program since there is no main() function.

    The two biggest problems I can see with the code are:

    1) You pass a char string of undefined length to the function and then attempt to access an arbitrary element within that array? You should define an array of a certain size and then pass a pointer to that array and the size of the array into the function.

    2) You establish an infinite loop and then use 'goto' statements to get out of it (has no one ever told you that 'goto' statements are terrible programming practice? If not, then I am telling you now).

    Instead of using 'goto' statements and infinite loops try doing something like this:
           ch = getch();
           /* Check for things like backspaces and new line characters (so you can NULL terminate your C-string) */
           /* Put the character into the buffer */
    } while( ( (ch != '\n')||(ch != '\r') )&&(i < str_len) );
    Also, a few tips for the future:
    1) When you submit code use code tags instead of posting your source code as a file - people are more inclined to read text from a web browser than they are to download a file and open it. If you don't know how to use code tags, read the sticky.

    2) Make sure your code is properly indented. The code you posted is difficult to read because it is more difficult to pick out the beginning and end of loops, if statements, switch statements, functions etc.

    3) More people will be willing to help you if you show time and care over your post. That means that you should try to formalise english and make your sentences more coherent. You can use slang and abbreviations but when describing your problem try to be as precise as possible.

    4) Try to get your spelling right in both your post and your program. Spelling mistakes in either puts people off trying to help you because unless you show the appropriate level of care and consideration, what is the point?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. Very odd segmentation fault/core dump.
    By ChristianTool in forum C Programming
    Replies: 19
    Last Post: 04-26-2004, 06:38 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM