That means your computer is super weird 'cause that code works on my machines. I mean, unless something changed in C since then so maybe I should go try it. Can you do this for me:
Take my code.
Run it so you cause the error you see, but write down every tiny step you make.
Write those up in your reply so I can replicate it with my file.
bruda@cyborg ~/LCTHW/Lectures/ex24 (git)-[master] % ./ex24
What's your First Name? dddddddddddddddddddddddddddddddddddddddddddddddddddddd
[ERROR] (ex24.c:39: errno: None) You have to enter a number.
What's your Last Name? How old are you? %
bruda@cyborg ~/LCTHW/Lectures/ex24 (git)-[master] %
Yea it seems like one of those mysterious C things that remind us of why it’s such a terrible language lol. But we get better as we learn more ways it can mess up. I haven’t gotten to this exercise yet. But overall everything seems clean & clear in zeds code. I get the same bug you do it seems.
Yeah… I agree. On the other hand, C is super fast and cross-platform. It’s always interesting finding ways to mess it up, for me it’s the same with other programming languages.
That’s the results I get, so I have no idea why @archmaster is getting a crash. Sooooooooooooooo weird. I’m sure there’s some bizarre edge case I don’t know about (since that’s C), but I would suggest that you “fix” fgets by telling it to read MAX_DATA-1 or have your arrays sized at MAX_DATA+1 but tell fgets MAX_DATA. That way there’s always a final \0 (if you calloc or set to 0} and it’ll at least not overrun the end.
Now the next thing is if you tell fgets to read 10, and then you blast it with 100, then the 90 remaining get queued up. That’s why we’re seeing that @lee8oi has here. That’s not really a bug at all.
I’m on ex24 now and I can see this weird behavior. It seems that stdin from the linux terminals queue up the characters you type and continue to feed them into the the program so the subsequent reads with fgets continue to receive the input until limits for each one were filled. Even without waiting for an enter key I’m assuming BECAUSE the input was maxed. When it filled the number with text it caused a crash. Bad number…it’s text. Interesting?
I like it better too because it’s easy enough to add to the existing code. Simply __fpurge(stdio); after reading input with fgets/etc. It’ll purge the standard input stream and the next read starts fresh. I also don’t think it’s necessary to do MAX_DATA - 1 in fgets since it already does size - 1 and closes the stream with ‘\0’. My solution shows that it works.
On a side note I did the extra credit and scanf didn’t need the stdin flush. And "%s%n" format seems to grab a nice tidy string without the \n at the end.
ex24.c:Scan_string()
int Scan_string(char *dest)
{
int nothing;
int n = scanf("%s%n", dest, ¬hing);
check(n > 0, "Failed to scan string.");
dest[MAX_DATA - 1] = '\0';
return 0;
error:
return -1;
}
ex24.c:Scan_integer()
int Scan_integer(int *dest)
{
int nothing;
int n = scanf("%d%n", dest, ¬hing);
check(n > 0, "Failed to scan integer.");
return 0;
error:
return -1;
}
ex24.c:Scan_float()
int Scan_float(float *dest)
{
int nothing;
int n = scanf("%f%n", dest, ¬hing);
check(n > 0, "Failed to scan float.");
return 0;
error:
return -1;
}
Talk to discobot about the advanced user tutorial. If you haven’t already covered the basic one. He shows you how to work with the forum’s features. Including that ‘expandable thingie’. I discovered that last night. Even got a badge for completing it
A few useful discobot commands to try:
@discobot display help @discobot start new user @discobot start advanced user
Actually I believe fgets is doing exactly what it’s supposed to. It reads up to so many characters and moves on. I don’t think it’s part of the job to flush the stdin. When the shell is programmed to keep feeding the input into the program, fgets programmed to read up to so many bytes and just leaves the stream as-is for the program to continue handling the provided input, the additional characters continue to push into the program while the subsequent calls to fgets continue to read the input. Thus filling up the variables similar to a input loop that keeps reading things in until the program itself somehow determines that enough input has been obtained. Which in the case of the original ex24.c code the number check was the first thing to realize something was amiss when it was receiving string input instead of numbers.