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.
Yes.Quote:
...how do I use fgets? Can I use like I do with gets?
A gets line like this:
Can be translated into fgets like this:Code:char buf[50];
gets(buf);
Code:char buf[50];
fgets(buf, sizeof(buf), stdin);
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.Quote:
...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? ;)
That's why it's important to learn to use loops correctly from the beginning!
We can translate laserlight's loop code to something like this:
Perhaps that makes it clearer as to what the loop does and how they work.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 ... */
Note that you should not use this kind of code. You should use laserlight's code. This is only for demonstration purposes.
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.Quote:
...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.... ^.^
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.

