Win a copy of Head First Android this week in the Android forum!

John Matthews

Rancher
+ 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
11
In last 30 days
0
Total given
0
Likes
Total received
62
Received in last 30 days
4
Total given
16
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by John Matthews

Well done if it works, but no, it wasn't quite what I had in mind.

My suggestion was based around this pattern:

(Or %s instead of %d for reading a string, with the string variable replacing &num.) Every time you want a string/number from buffer, you just use that.

Does that make sense?
2 days ago
BTW not sure if you are aware but if you sscanf() "   42" using "%d%n", the %n value includes the leading spaces.  So you only have to increase your buffer index by the %n value each time; it will always end up indexing the first character after the last number digit.
2 days ago
Yes

You need one variable, say idx, to be the index of the position in the buffer where you want to read the next number string from. That will initially be 0, and it will gradually increase as you read each number string at position idx.

You need another temporary variable to hold the length of the number string you have just read, say len, set by the %n in the sscanf.

Each time after reading a number string you need to increase idx by len.

Look at how you are using n and n2. Try giving them better names; it might help clarify what is going wrong.
2 days ago
I think the main problem is that you are reading the buffer from buffer+n, but n is only the length of the last string. You'll need another variable, say idx, and read/sscanf from buffer+idx, and add n to idx each time so you are progressing along the string.
3 days ago
You wouldn't need the sscanf() to extract the strings, but you'd need to use something like atoi() to convert strings to numbers instead of the sscanf() %d.

Have a look at the example on that web page to see how it works, and google should be able to find others.

strtok() on its own only lets you loop through split tokens as you say. But if you wanted to you could write your own split-like function which takes a line of input and an array of char pointers and uses strtok() to fill the array with pointers to the individual strings returned by strtok() (and you'd also need it to return a count). Then you could use the array (and count) to access the strings in any order you liked.
3 days ago
Hi Hanna

Yes, what you're suggesting would work. You might also consider using strtok() to break up the line into individual strings rather than having to use the %n in sscanf(), but I'm not sure whether the code would be any better/easier in the end. Might be worth trying it though.

John
3 days ago

Hanna Roberts wrote:I was just trying to make the numbers match up because i had added a zero to the end of 100.0 when i wrote the rand() line should have been , so it wasn't a huge deal i just wanted to make sure people weren't confused by that

But they would have been confused if they had read the references to 1000 in other posts, and there was no 1000 in your original post because you had edited it.
2 weeks ago
Hi Jesse - Hanna can correct me if I'm wrong, but based on this and previous threads they are learning how to program in C. I don't think the randomness of the numbers generated by rand() is an issue.
3 weeks ago
The 1000 would make sense if you divided the result by 10, to give values 1.0, 1.1, 1.2, 1.3 etc.
3 weeks ago
Yes - I was going to mention that The '%' operator needs integer operands eg. 1000 not 1000.0. Stephan was pointing out that the value of 1000(.0) is probably wrong.
3 weeks ago
Hi Hanna - is it ok if the results are doubles, but with 0 fractional part eg. 1.0, 2.0, 3.0, .. 100.0? Or do you want non-integer values as well?
3 weeks ago
...although I suspect sqrt(2.0) is just being optimised to a constant value. So yes, add -lm after the .c filename on the command line.
1 month ago

Campbell Ritchie wrote:JM: I tried myself last night, and kept getting errors about, 𠇍istance.c:(.text+0x87): undefined reference to `sqrt'”. I could however get sqrt(2.0) to compile and run. ny idea what I have done wrong? Should I reinstall gcc?

I had to lookup how to link with the math library - "-lm" on the end of the command line (after the .c). But then that doesn't explain how it compiled with the 2.0 arg. Pass.
1 month ago
Hi Denis

The mistakes are just 'typos'; missing semicolons on the ends of lines, missing commas, that sort of thing. The compiler will tell you were the mistakes are. There's nothing fundamentally 'wrong' with your code; I fixed the typos (identified by the compiler) and it worked.

Make sure you enable all warnings on the compiler command line - use -Wall if you are compiling with gcc.

John
1 month ago