+ 1

[SOLVED] Ballpark orders code coach

Here's my code: https://code.sololearn.com/c0XRZR287ZDj/?ref=app It fails only the last test. I dunno why.

18th Nov 2020, 4:24 PM
Davide
Davide - avatar
8 odpowiedzi
+ 3
Apparently it works if you change the amount of characters compared for water from 5 to 6, or if you use strcmp() altogether. Most likely the last test case contains an order like "Waterbottle" that throws it off.
18th Nov 2020, 5:33 PM
Shadow
Shadow - avatar
+ 4
Both strcmp() and strncmp() are specifically designed to compare C-style strings, so they will stop at the null character. If you want to compare those arrays as buffers, you should look at memcmp() instead, which will treat the null character only as a character like any other: https://en.cppreference.com/w/c/string/byte/memcmp See the answers in this thread for a comparison between the functions: https://stackoverflow.com/questions/13095513/what-is-the-difference-between-memcmp-strcmp-and-strncmp-in-c
18th Nov 2020, 6:12 PM
Shadow
Shadow - avatar
+ 2
Here's my thinking.(my thinking may be wrong) I think your code is right but you used %.2f for printing which will print 2 decimal point and it will make the number in approx but in the question they didn't say to convert 2 decimal point which may be the reason. Also if you give the input 4 Cheeseburger(writing 4 times Cheeseburger), the answer will be 42.8 but your code will print 42.80 (same but a extra zero) .This may be a reason. There is one solution . You can use %g instead of %f .This might work. And also thanks for giving the description of that code coach .
18th Nov 2020, 5:14 PM
The future is now thanks to science
The future is now thanks to science - avatar
+ 2
I solved it, here's how (might not be the best or a good solution), just use %s 4 times, set sum (or any name of the variable) to 0, loop the inputted strings from 0 to 3, strcmp each of them, if it is pizza or nachos, sum+=6, cheese sum+=10, water sum+=4, coke +=5, after that just multiply it by 0.07. Note: I also use %.2f
19th Nov 2020, 10:56 PM
LastSecond959
LastSecond959 - avatar
+ 1
BeegCat @notapythoninja (I can't tag you properly 😭) I deleted the previous question because I noticed I just forgot about nachos😅 but then even adding Nachos the program doesn't work.
18th Nov 2020, 4:25 PM
Davide
Davide - avatar
+ 1
Zatch bell thank you for the answer! 🤗 I used %.2f because I saw that in tests they show only 2 dcimals. But yes they don't say nothing about that. I didn't know about %g thank you 🤗 I tried it and the program still failing the last test😔
18th Nov 2020, 5:33 PM
Davide
Davide - avatar
+ 1
Shadow I didn't use strcmp because I had a doubt on whether it would have compared the array even after \0 and I was to lazy to Google it 🤭 And I thought it was worthless to check even \0 with strncmp. But it wasn't 😅 Thank you very much!!🤗🤗🤗🤗🤗
18th Nov 2020, 5:42 PM
Davide
Davide - avatar
+ 1
Oh I see, strncmp always stops at the null characters even if I specify a greater length. So I could have done strncmp(word, input, 50) ; And the program would have worked fine even if the array word has only 13 subscripts. (As long as it contains a \0) However that's exactly what strcmp does. My doubts were more in line with the way how memcmp works. Thank you so much once again!🤗🤗🤗
18th Nov 2020, 6:36 PM
Davide
Davide - avatar