Thread: masked with asterisk(*)?

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Quote Originally Posted by ShadeS_07 View Post
    ..so uhmm, what makes it evil? And what can I use to let my program input strings with spaces?
    If you refer to the analogy here: http://cboard.cprogramming.com/showp...3&postcount=31
    And read about buffer overflows here: http://cpwiki.sourceforge.net/buffer_overrun
    You should understand why it's so bad, because gets does not know the size of your buffer you provide, it can write more data than there is space.
    This is impossible with fgets, if you specify the correct size of the buffer.

    ...how do I use fgets? Can I use like I do with gets?
    Yes.
    A gets line like this:
    Code:
    char buf[50];
    gets(buf);
    Can be translated into fgets like this:
    Code:
    char buf[50];
    fgets(buf, sizeof(buf), stdin);
    ...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?
    Nope, as other have mentioned, it's still bad. You won't learn to understand others code and others won't understand your code, plus even if you feel comfortable with gotos, soon you will create a mess which you yourself cannot even read.
    That's why it's important to learn to use loops correctly from the beginning!

    Quote Originally Posted by ShadeS_07 View Post
    ..still confused, but I'm going to learn more about it soon... thanks... ^.^
    We can translate laserlight's loop code to 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()
    {
        loop:
        {
            char name[20] = "";
            char passcode[20] = "";
            readCredentials(name, sizeof(name) - 1, passcode, sizeof(passcode) - 1);
            analyzeCredentials(name, passcode);
        }
        if (readOption() == 'R')
            goto loop;
    
        return 0;
    }
    
    /* ... define functions ... */
    Perhaps that makes it clearer as to what the loop does and how they work.
    Note that you should not use this kind of code. You should use laserlight's code. This is only for demonstration purposes.

    ...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.... ^.^
    How to mask the password with "*" has been asked many times and there's no standard way. If you do a search, I'm sure you will find lots of info.
    As for sleep... if it's Linux, them no, it takes seconds. Which is kind of strange or silly, if you ask me.
    But Windows Sleep function takes milliseconds.
    I don't know if there is an alternative API for Linux.
    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.

  2. #17
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    As for sleep... if it's Linux, them no, it takes seconds. Which is kind of strange or silly, if you ask me.
    But Windows Sleep function takes milliseconds.
    I don't know if there is an alternative API for Linux.
    UNIX also has usleep() that takes microseconds.
    "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

  3. #18
    Registered User
    Join Date
    Jul 2008
    Posts
    44

    Cool

    Code:
    char buf[50];
    fgets(buf, sizeof(buf), stdin);
    ..what does sizeof(buf), stdin do? What does it mean?

    ..and this one? I don't get this one also...
    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()
    {

    Code:
    readCredentials(name, sizeof(name) - 1, passcode, sizeof(passcode) - 1);
    ...and that... what does it mean if we put -1 there?

    Code:
        loop:
        {
            char name[20] = "";
            char passcode[20] = "";
            readCredentials(name, sizeof(name) - 1, passcode, sizeof(passcode) - 1);
            analyzeCredentials(name, passcode);
        }
        if (readOption() == 'R')
            goto loop;
    ..is that the way how a while...do loop works?

    what's the difference between do...while and while...do? I know that they're not the same, am I right?

    How to mask the password with "*" has been asked many times and there's no standard way. If you do a search, I'm sure you will find lots of info.

    I found this one from:
    http://www.gohacking.com/2007/12/c-p...d-mask-it.html

    I made some modification... -_+

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    char pw[25], ch;
    int i;
    
    int main()
    {
        clrscr();
        puts("Enter password");
    
        while(1)
        {
            if(i<0)
            i=0;
            ch=getch();
            if(ch==13)
            break; /*13 is ASCII value of ENTER*/
    
            if(ch==8) /*ASCII value of BACKSPACE*/
           {
    
                putch('\b');
                putch(NULL);
                putch('\b');
                --i;
                continue;
            }
    
            pw[i++]=ch;
            ch='*';
            putch(ch);
        }
    
        pw[i]='\0';
        printf("\n\n%s",pw);
        getch();
    
        return 0;
    }
    ...can someone please explain this to me? Coz, I don't still get it...


    ...hmmm, anyways, thank you very much to all of you who have helped me... I really appreciate it... ^.^

  4. #19
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    >> ...can someone please explain this to me? Coz, I don't still get it...
    Perhaps you can be a little more descriptive? Start by explaining what you do understand. It's important that you try, because it's part of asking a smart question. And experience explaining an algorithm is a fundamental part of any programmer's skill set.

  5. #20
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ShadeS_07 View Post
    Code:
    char buf[50];
    fgets(buf, sizeof(buf), stdin);
    ..what does sizeof(buf), stdin do? What does it mean?
    Those are the arguments to fgets, buf is the char*, sizeof(buf) would be the space allocated to buf (50), stdin is the stream to read from (terminal input).

    Quote Originally Posted by ShadeS_07 View Post
    ..and this one? I don't get this one also...
    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()
    {
    They're function headers. One important point here is that in addition to being fulfilled in loops, goto is almost exactly like a function call, but considering how much more flexability you get by defining and calling a function, it's no wonder that goto never really seems like much of a choice (= no one uses it).

    Quote Originally Posted by ShadeS_07 View Post
    Code:
        loop:
        {
            char name[20] = "";
            char passcode[20] = "";
            readCredentials(name, sizeof(name) - 1, passcode, sizeof(passcode) - 1);
            analyzeCredentials(name, passcode);
        }
        if (readOption() == 'R')
            goto loop;

    ..is that the way how a while...do loop works?
    The same thing as a while loop:
    Code:
    while (1) {
            char name[20] = "";
            char passcode[20] = "";
            readCredentials(name, sizeof(name) - 1, passcode, sizeof(passcode) - 1);
            analyzeCredentials(name, passcode);
            if (readOption() != 'R') break;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by ShadeS_07
    what's the difference between do...while and while...do? I know that they're not the same, am I right?
    A do while loop loops at least once. A while loop might not even loop at all.
    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

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Examples of loops.
    While loop:
    Code:
    while (x)
    {
        // ...
    }
    Goto-version:
    Code:
    loop:
    if (!x) goto exit;
        // ...
    goto loop;
    exit:
    Do ... while loop:
    Code:
    do
    {
        // ...
    }
    while (x)
    Goto-version:
    Code:
    loop:
        // ...
    if (x) goto loop;
    For loop:
    Code:
    for (int i = 0; i < 10; i++)
    {
        // ...
    }
    Goto-version:
    Code:
    int i = 0;
    goto loop;
    inc:
    i++;
    loop:
        // ...
    if (i < 10) goto inc;
    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.

  8. #23
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    Actually, your for example lacks correctness. For loops should be more like while since the increment comes after the body, before the next test.
    Code:
    i = 0;
    loop: if ( i == 10 ) goto out;
    /** body **/
    i++;
    goto loop;
    out:
    But that doesn't really help does it?

  9. #24
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    If I may give a further example of why to not use "goto", it is this: Your program fits fairly neatly on one page. In the real world of programming, you aren't going to have programs that fit neatly into one page. As an old Fortran programmer who has seen code of 10,00 lines and more with "gotos', it's no fun to try and figure out where you are and what is going on when you try to debug a program. Trust me, if you don't use cuss words now, and if you continue to use "gotos", you will eventually learn and say those words. Because 6 months from whenever you finish your program, when you go back and look at your program that is longer than a few pages, YOU will have a hard time debugging your OWN work.

  10. #25
    Registered User
    Join Date
    Jul 2008
    Posts
    44
    ...ok, so here I is something for you who have helped me... tada! no more goto's,, -_+ hehehehe....

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
    	char name[20], passcode[20], option;
    	int x=0;
    
    	do
    	{
    		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++;
    		}
    
    		do
    		{
    			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);
    
    				if(option=='E') return 0;
    
    			}
    
    		} while(option=='B'); /*duh!*/
    
    	} while(option=='R');
    
    
    	return 0;
    }
    ..I have already tested it, and it's working! yeah! xD
    ...but do you see that line over there?

    Code:
    }while(option=='B'); /*duh!*/
    ....that's not actually what I want, I don't want the program to repeat the loop if the user enters B... what I actually want to put here is something that can repeat the loop if the user enters any characters other than E or R... You know what I'm saying? ...some sort of an error handler? (amiryt? error handler??) ...just like in the switch() statement... the default: thing.. yeah, that's what I mean... but, what should I put there? I have no idea what to put there, so I just put there while(option=='B');... If you can still remember the first source-code I've posted here (I think it's on the first page of this thread), the program was supposed to jump to pcode: when I was using the goto (btw, is goto a function?) ...how can I do the same using a do-while loop? ^.^

    ...uhmmm, one more thing, is there any such function as putch()? Will this help me make the program to "put" asterisk "characters" as the user types in the passcode?

    ....And btw, I'll share ya'll something about this asterisk thing... hehehe, lol.. xD
    While I was trying to figure out how the heck am I going to make the program ouput asterisks as the user types in the passcode, something came into my mind... the toupper() and tolower() functions... I was wondering, if toupper() changes the character entered by the user into an upper-case character, and tolower() changes the character into a lower-case character... I tried this: toasterisks()! hahaha, lol.. xD ...and what did get?? Tada! ~CoMpiLer ErroR.

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    		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++;
    		}
    can either be written without the while, or written differently using a loop:
    Code:
    		while(x < 7)
    		{
    			printf(".");
    			sleep(1);
    			x++;
                    }
    This one:
    Code:
    		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++;
    		}
    can also be written as a loop:
    Code:
    // At the beginning of the source.
         char spinstr[] = "|/-\\|";
    
    //
                 
    		while(x < 5)
    		{
    			gotoxy(42,4); printf("%c", spins[x]);
    			sleep(1);
    			x++;
    		}
    (or you could remove the while, as it does exactly the same thing either way).

    --
    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.

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

    Cool wow,, -_+

    Code:
    		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++;
    		}
    can either be written without the while, or written differently using a loop:
    Code:
    Code:
    		while(x < 7)
    		{
    			printf(".");
    			sleep(1);
    			x++;
                    }
    This one:

    Code:
    		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++;
    		}
    can also be written as a loop:


    Code:
    // At the beginning of the source.
         char spinstr[] = "|/-\\|";
    
    //
                 
    		while(x < 5)
    		{
    			gotoxy(42,4); printf("%c", spins[x]);
    			sleep(1);
    			x++;
    		}
    (or you could remove the while, as it does exactly the same thing either way).
    ...thanks man,, that is really useful man.... ^.^

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    You should still replace gets.
    There's no need for putch, a simple printf statement will do.
    You can replicate any goto behavior with a loop - including this "default" behavior. A loop loops so long as the condition is true. So you need to think really hard what that condition should be. That's all, really.
    Goto is not a function; it is a keyword.
    Last edited by Elysia; 10-24-2008 at 09:28 AM.
    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.

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

    awwww.. -_+

    You should still replace gets.
    ah, yes,, the gets()... lol, I forgot to change that...

    There's no need for putch, a simple printf statement will do.
    thankyou.. but how?

    You can replicate any goto behavior with a loop - including this "default" behavior. A loop loops so long as the condition is true. So you need to think really hard what that condition should be. That's all, really.
    awww, c'mon... I wouldn't post this if I have had already think of this really hard in the first place.. I just can't figure it out.. plus, my knowledge regarding C programming is limited, so I don't even have a clue... -_+

  15. #30
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by ShadeS_07 View Post
    ah, yes,, the gets()... lol, I forgot to change that...



    thankyou.. but how?
    Code:
    printf("*");
    would print an asterisk.
    awww, c'mon... I wouldn't post this if I have had already think of this really hard in the first place.. I just can't figure it out.. plus, my knowledge regarding C programming is limited, so I don't even have a clue... -_+
    You want not E or R, right? So that's what you do
    Code:
    while(not(option=='E' or option=='R'))
    (provided you include <iso646.h>. If you don't want to include that, you have translate not and or into C:
    Code:
    while(!(option=='E'||option=='R'))
    using the logical operators.)

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