Thread: masked with asterisk(*)?

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    44

    Cool masked with asterisk(*)?

    This is a program I made with C. Check it out.

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
    	char name[20], passcode[20], option;
    	int x=0;
    
    	codesegment_start:
    	clrscr();
    
    	printf("\n\n\tEnter your name: ");
    	gets(name);
    
    	printf("\n\tEnter the passcode: ");
    	gets(passcode);
    
    	printf("\n\n\n\tPress \"Enter\" to proceed...");
    
    	clrscr();
    
    	printf("\n\n\n\tPlease wait. Analyzing data...");
    
    	while(x < 5)
    	{
    		gotoxy(42,4); printf("|");
    		sleep(1);
    		x++;
    		gotoxy(42,4); printf("/");
    		sleep(1);
    		x++;
    		gotoxy(42,4); printf("-");
    		sleep(1);
    		x++;
    		gotoxy(42,4); printf("\\");
    		sleep(1);
    		x++;
    		gotoxy(42,4); printf("|");
    		sleep(1);
    		x++;
    	}
    
    	clrscr();
    
    	printf("\n\n\n\tYour name is: ");
    	textcolor(LIGHTBLUE); cprintf("%s", name);
    	printf("\n\n\t");
    	textcolor(LIGHTGRAY); cprintf("Verifying passcode");
    	x=0;
    
    	while(x < 7)
    	{
    		printf(".");
    		sleep(1);
    		x++;
    		printf(".");
    		sleep(1);
    		x++;
    		printf(".");
    		sleep(1);
    		x++;
                    printf(".");
    		sleep(1);
    		x++;
                    printf(".");
    		sleep(1);
    		x++;
                    printf(".");
    		sleep(1);
    		x++;
                    printf(".");
    		sleep(1);
    		x++;
    	}
    
    	pcode:
    	clrscr();
    
    	printf("\n\n\n\tThe passcode that you have entered is: ");
    
    	if(!strcmp(passcode, "XMod749"))
    	{
    		textcolor(LIGHTBLUE+128); cprintf("ACCEPTED.");
    		getch();
    		return 0;
    	}
    	else
    	{
    		textcolor(LIGHTRED+128); cprintf("INVALID!");
    		getch();
    		printf("\n\n\t");
    		textcolor(LIGHTGRAY); cprintf("What do you want to do?");
    		printf("\n\n\t\tExit [E] or Retry [R]? ");
    		option = getche();
    		option = toupper(option);
    
    		switch(option)
    		{
    			case 'E':
    			return 0;
    
    			case 'R':
    			goto codesegment_start;
    
    			default:
    			printf("\n\t\tInvalid command entered. Please try again.");
    			getch();
    			goto pcode;
    		}
    	}
    
    	return 0;
    }
    ..as you can see from the above source-code, the program will ask the user to enter his/her name, then the passcode... The question is, how can I make the program echo asterisks(*) as he/she types in the passcode? Also, is there any way that I can make the sleep() function work in milliseconds? I mean, I want to make it faster, so that when I try to animate something, it won't be so slow... Thanks in advance... -_+

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Your question is answered in the FAQ section.

    Also in the FAQ section are numerous reasons why the best thing you could do would be alt-tabbing to your IDE, pressing ctrl+a and then backspace.

    Goto is bad - never use it. Everything you could possibly do with goto could be done without it.
    Conio.h is not standard C. Use the current operating system's API to do the same thing.
    Last edited by IceDane; 10-20-2008 at 04:37 AM.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by IceDane View Post
    Your question is answered in the FAQ section.
    I don't doubt you, but without clicking on every link in the FAQ section, I don't see it there. If I can't find it, I'm sure the OP and many others must have a hard time finding stuff in the FAQ.

    I'm not sure who controls the FAQ section, but would it be possible to re-organize it so that it's easier for people to find what they're looking for? Headings like "How do I (level x)" don't seem very helpful to me.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cpjust
    I don't doubt you, but without clicking on every link in the FAQ section, I don't see it there. If I can't find it, I'm sure the OP and many others must have a hard time finding stuff in the FAQ.
    I believe it is How can I get input without having the user hit [Enter]?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    That title doesn't mention anything about masking passwords, which is exactly why I didn't find it.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by cpjust View Post
    That title doesn't mention anything about masking passwords, which is exactly why I didn't find it.
    We call that reusability.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    44

    Cool

    Quote Originally Posted by IceDane View Post

    Goto is bad - never use it. Everything you could possibly do with goto could be done without it.
    Conio.h is not standard C. Use the current operating system's API to do the same thing.
    ..uhmm, what's wrong with goto? What would be the risk if it became my habit to use goto? And why is it that some people discourage me to use conio.h?

    ...some people are curious why am I using the clrscr( ) function which I believe, is part of conio.h??(correct me if I'm wrong)..

    ....well, anyways, thanks for the response, but still, my questions haven't been answered yet... I guess that I should check it up later.... -_+

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    [QUOTE=ShadeS_07;798541]..uhmm, what's wrong with goto? What would be the risk if it became my habit to use goto? And why is it that some people discourage me to use conio.h?
    Goto is bad in a sense that it creates code mess. Code can jump back and forth and you'll lose track.
    Loops are much more easier to follow, and therefore recommended over goto.
    Conio.h is a non-standard header, which means some compilers have it, some do not. If you want your code to be portable over all compilers and platforms, you should not use it (and this forum is about teaching just those things).

    ...some people are curious why am I using the clrscr( ) function which I believe, is part of conio.h??(correct me if I'm wrong)..
    There's another entry in the faq on how to clear the screen.

    And I'm surprised no one mentioned gets! Gets is very, very evil and should never be used. Under no circumstance.
    http://cpwiki.sourceforge.net/Gets
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by ShadeS_07 View Post
    ..uhmm, what's wrong with goto? What would be the risk if it became my habit to use goto? And why is it that some people discourage me to use conio.h?

    ...some people are curious why am I using the clrscr( ) function which I believe, is part of conio.h??(correct me if I'm wrong)..

    ....well, anyways, thanks for the response, but still, my questions haven't been answered yet... I guess that I should check it up later.... -_+
    Goto is the easy way out. It can create spaghetti code which will be way too hard to follow, edit and debug. Everything you can do with goto can be done another way, that's more friendly to debug(A loop, for example).
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    44

    Cool

    And I'm surprised no one mentioned gets! Gets is very, very evil and should never be used. Under no circumstance.
    Woah! And why is that? Oh, it's a good thing that I've joined this forum, I am learning lots of things here,, lol.. xD

    ..so uhmm, what makes it evil? And what can I use to let my program input strings with spaces?

    ...I've read the details about gets and fgets on cpwiki from the link you've provided, thanks.. -_+

    ...how do I use fgets? Can I use like I do with gets?


    Goto is the easy way out. It can create spaghetti code which will be way too hard to follow, edit and debug. Everything you can do with goto can be done another way, that's more friendly to debug(A loop, for example).
    Goto is bad in a sense that it creates code mess. Code can jump back and forth and you'll lose track.
    Loops are much more easier to follow, and therefore recommended over goto.
    ...well, I used to use loops all the times, but I'm not so good at it, so I used goto instead.. And, if goto is bad in a sense that it creates code mess, can I say that it's not bad at all if I'm comfortable with it and that I can follow or be in track with the flow of the program? I mean, ofcourse I agree with you guys, loops are better... but this is a small program, so therefore, we can actually follow the code easily, right?

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by ShadeS_07 View Post
    ...well, I used to use loops all the times, but I'm not so good at it, so I used goto instead.. And, if goto is bad in a sense that it creates code mess, can I say that it's not bad at all if I'm comfortable with it and that I can follow or be in track with the flow of the program? I mean, ofcourse I agree with you guys, loops are better... but this is a small program, so therefore, we can actually follow the code easily, right?
    No, you need to learn how to write proper code with loops, otherwise you're just hurting yourself in the long run (and annoying other people that will eventually work with you and use your spagetti code). When I first taught myself Basic a long time ago, I used GOTO's all over the place. Thankfully I learned a better way with loops.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  12. #12
    Registered User
    Join Date
    Jul 2008
    Posts
    44

    Cool

    No, you need to learn how to write proper code with loops, otherwise you're just hurting yourself in the long run (and annoying other people that will eventually work with you and use your spagetti code). When I first taught myself Basic a long time ago, I used GOTO's all over the place. Thankfully I learned a better way with loops.
    oh, I see... nice one... thanks man... I better start learning more about loops... but uhmmm, how can I use loops with this one?

    Code:
    . . .
    		switch(option)
    		{
    			case 'E':
    			return 0;
    
    			case 'R':
    			goto codesegment_start;
    
    			default:
    			printf("\n\t\tInvalid command entered. Please try again.");
    			getch();
    			goto pcode;
    		}
    . . .
    ...how can I make the program let the user to "try again" using loops and not goto? thanks.. ^.^

    ....uhmmm,, my questions are still, unanswered yet.... still hoping from you guys... thanks in advance... -_+

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ShadeS_07
    ...how can I make the program let the user to "try again" using loops and not goto? thanks.. ^.^
    One possibility is to change your code into something like this:
    Code:
    /* ... include headers ... */
    
    void readCredentials(char *name, size_t name_maxlen, char *passcode, size_t passcode_maxlen);
    void analyzeCredentials(const char *name, const char *passcode);
    char readOption();
    
    int main()
    {
        do
        {
            char name[20] = "";
            char passcode[20] = "";
            readCredentials(name, sizeof(name) - 1, passcode, sizeof(passcode) - 1);
            analyzeCredentials(name, passcode);
        }
        while (readOption() == 'R');
    
        return 0;
    }
    
    /* ... define functions ... */
    There would be another loop in readOption() that performs what your current goto pcode does. You might also notice that in your current code the pcode label is probably a little too early: if the user entered an invalid command, you would likely want to print "What do you want to do?" again, not "INVALID!", which really refers to the passcode.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Jul 2008
    Posts
    44

    Cool

    One possibility is to change your code into something like this:
    Code:

    Code:
    /* ... include headers ... */
    
    void readCredentials(char *name, size_t name_maxlen, char *passcode, size_t passcode_maxlen);
    void analyzeCredentials(const char *name, const char *passcode);
    char readOption();
    
    int main()
    {
        do
        {
            char name[20] = "";
            char passcode[20] = "";
            readCredentials(name, sizeof(name) - 1, passcode, sizeof(passcode) - 1);
            analyzeCredentials(name, passcode);
        }
        while (readOption() == 'R');
    
        return 0;
    }
    
    /* ... define functions ... */
    There would be another loop in readOption() that performs what your current goto pcode does. You might also notice that in your current code the pcode label is probably a little too early: if the user entered an invalid command, you would likely want to print "What do you want to do?" again, not "INVALID!", which really refers to the passcode.
    ..still confused, but I'm going to learn more about it soon... thanks... ^.^

    ....btw, I think we're getting off-topic here, so I guess I should make another thread regarding loops... -_+


    ...and uhmmm, my questions,, please... This is the only place for me that could give an easy-to-understand answers and not-so-confusing answers to my questions.. So I'm still hoping some answers from you guys... thanks in advance.... ^.^

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Replacing goto's with loops is USUALLY trivial, but of course, your example has essentially two nested loops, and you iterate both from the same switch-statement. This is EXACTLY what you'd call "spagetti programming" (because when you draw a flow-chart, it looks like a plate of spagetti).

    I think Laserlight got a bit further and refactored the whole program into several functions, which is probably the RIGHT thing to do eventually, but if we ignore that bit for just now, you need to add two loops, one to do the outer loop, and one to do the inner loop. When the inner loop is terminated, the outer loop would still continue the program.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c masked password?
    By ShadeS_07 in forum C Programming
    Replies: 2
    Last Post: 10-09-2008, 10:04 AM
  2. Masked Avatar
    By Travis Dane in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-05-2003, 11:54 AM
  3. can the word enter in dos be masked?
    By Jasonymk in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2003, 07:47 AM