John Matthews

Ranch Hand
+ Follow
since Jul 04, 2018
Was writing 6502 (8 bit) assembler fixed point arithmetic code (to draw circles) back in the early 80s, on an Acorn Atom if that means anything to anyone. Usually write embedded C, with the odd excursion into Java and C++.
Melksham, UK
Cows and Likes
Cows
Total received
3
In last 30 days
1
Total given
0
Likes
Total received
19
Received in last 30 days
2
Total given
11
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by John Matthews

The '$' is the linux console command prompt; I mentioned it in my previous post. I didn't enter it.
14 hours ago
Bear in mind that the last string, the URL, will include the \n added by fgets().
14 hours ago
(In case you're unfamiliar with a linux command line environment, the '$' is the linux prompt, and the './a.out' is what I entered to run the compiled program which is in file a.out (default for executables built by the gcc compiler).)
3 days ago
Not sure if I understand the question, but I think so - to clarify, this:
When I compile and run it:
You can see my input being echoed by the printf(), and it exits when I enter 'quit'. So I'm pretty sure it works; if it doesn't in zybooks then there's something we don't understand about the way zybooks is set up to handle input.
3 days ago
Hi - taking a break from C/C++...

So I've done bits of Java previously, but with long enough gaps between to ensure I can never remember how it works and basically start from scratch each time

This time around I'm trying my hand at Graphics2D. I've done the basics of overriding the JPanel's paintComponent(), and that's fine. But now I've added couple of buttons to change the Graphics2D.scale(). It sort of works - the problem is that the image is only redrawn when I manually resize the frame.

I've googled this, but have yet to find something that works. You'll see I've put JPanel.invalidate()s in the button handlers as an example of something that doesn't work (the way I'm using it anyway).

Also - the way I'm using static gMain to hold the MainFrame instance so I can call the non-static doDrawing() method in the JPanel's overridden paintComponent() seems less than optimal. What would be the best way to do this? The idea is that at some point in the future the MainFrame will contain data to be drawn, so doDrawing() needs to be non-static.

(Apologies for the blocks of NetBeans auto-generated code.)

Cheers
John

1 week ago
Hey, fgets() is one of the good guys

You usually see it being recommended as the function to use instead of bad-boy gets(). To quote the linux gets() man(ual) entry:

Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.


fegts() takes an argument telling it how big the buffer is where it will store the input, and it won't attempt to store more than will fit in the buffer. gets() has no such argument - given a big enough input it will happily write all over your memory for you.

The ! thing is a separate issue - fgets() returns a pointer pointing to where the input has been stored (which as you say is where you told fgets() to put it), or NULL on error/end of input. It is just the fact that it is a pointer that allows it to be used with !, because NULL is 0, and 0 is false (obviously ).

Interesting (I use the word loosely) note, just in case you hadn''t heard it before - the fact that number 0 is the NULL pointer in a pointer context *doesn't* necessarily mean that your computer's internal representation of NULL is the number 0
http://c-faq.com/null/varieties.html
1 week ago
In fact you could print out the input as lines rather than individual characters, as that's what appears to be coming in. That is, instead of the for() loop, just do
(You don't need to add a \n here because it's been added by fgets() to the end of the string in userStr.)

Then instead of the output you posted above I would expect to see:
Might give more of a clue as to where it's coming from. Something to read anyway
1 week ago
Those messages 'Program generated too much output' etc. are interesting; what environment are you running in? I wonder if that's something to do with it. Is it generating input for you or something weird like that?

The output you've posted shows that the program received inputs of 'Hello there' and 'Hey', with each one terminated by a \n (=10). Did you enter those? There should be some output somewhere corresponding to the 'quit' you (presumably) entered.

The fact that the count in the output keeps going back to 0 implies the code is looping around the outer 'while' loop, not just the print 'for' loop, and it must be calling fgets() each time. Ttrying changing the fgets() call to this:
But the output looks like 'real' input, as if it's just reading a file. Odd.

Sorry - please can you re-post your code again, just so we can double-check it?
1 week ago
Well it works for me - enter something other than one the 3 'quit' strings and they are output in reverse, and it quits if one of the strings is entered. Good work

But I don't know why it's not working for you; please could you try changing your print loop to the following, re-run, and show the output when you enter one of the quit strings?
1 week ago
Can you post the updated code? (Just about to go to bed now, will look tomorrow.)
1 week ago
Just adding "\n" would be one solution, yes (I assume the additional 'q' in your post was a typo).

Or a slightly neater option (although it actually requires more typing) - instead of adding the newline to the 3 strings you are testing for, you could remove it from the one string that has been input. Something like:
You could also then use len in your for() loop.

Another suggestion - at the moment you are doing an fgets() before the while() loop as well as inside it. There should only be the one, inside the loop - why not just do the fgets() before the for()?
1 week ago
Yep, I'd be happy to have that stuck to my bumper. Thanks.
3 weeks ago
Great! Let's go wild... how about same as C++, but without the ++? Never created any GIFs/icons before, but will have a go if necessary.
3 weeks ago
Agreed - I sort of implied that in my pseudo-code (2nd post in thread), but forgot to start with the input bit. Swap should just swap, although I could understand doing a printf() inside for debugging.
3 weeks ago
Hi - any chance of one for C?
3 weeks ago