+ 3

Ballpark Orders[Solved]

I tried to solve this code coach problem but my output is not correct, kindly correct my code and explain to me. Problem: You and three friends go to a baseball game and you offer to go to the concession stand for everyone. They each order one thing, and you do as well. Nachos and Pizza both cost $6.00. A Cheeseburger meal costs $10. Water is $4.00 and Coke is $5.00. Tax is 7%. Task Determine the total cost of ordering four items from the concession stand. If one of your friend’s orders something that isn't on the menu, you will order a Coke for them instead. Input Format You are given a string of the four items that you've been asked to order that are separated by spaces. Output Format You will output a number of the total cost of the food and drinks. Sample Input 'Pizza Cheeseburger Water Popcorn' Sample Output 26.75 https://code.sololearn.com/csaDCF26c7do/?ref=app

24th Sep 2023, 5:08 PM
Tarun Gautam
Tarun Gautam - avatar
12 ответов
+ 4
Tarun Gautam the only problem is with scanf(). It stops reading the string when it encounters a space. You can get around the problem by changing the format specifier from "%s" to "%[^\n]". Another way is to replace scanf() with fgets(). Otherwise, the code looks very good and it should work. Edit: As a matter of security, it's a good idea to limit the number of characters that are allowed during input. I'd suggest using "%99[^\n]" as a format specifier. And instead of fgets(), it is better to use fngets(). Consider also that scanf already breaks up the input by spaces. You could replace strtok() by using something like while(scanf("%99s", word)!=EOF), after declaring word as a char array.
24th Sep 2023, 6:35 PM
Brian
Brian - avatar
+ 4
Tarun Gautam where is your menu and price array ? Oh nevermind it's structured in your if else if else...
24th Sep 2023, 6:09 PM
BroFar
BroFar - avatar
+ 4
Tarun Gautam Define macro #define MAX 300 THEN instead of using scanf use fgets for input fgets(str, MAX, stdin); This should resolve the problem. When I ran the following Pizza Cheeseburger Water Popcorn Output was 26.75
24th Sep 2023, 6:39 PM
BroFar
BroFar - avatar
+ 4
Brian I figured it out as he didn't use fgets and missed using the define macro I missed it too on initial viewing ..
24th Sep 2023, 6:45 PM
BroFar
BroFar - avatar
+ 4
Tarun Gautam We use when we want to repeatedly use a single value or a piece of code in c programs, in general. Other languages use the macros as well but quite common in c. Just using a max size will not buffer correctly If you fail to define macro In this case and just : fgets(str,300,stdin); the answer will result in zero.zero zero
24th Sep 2023, 7:09 PM
BroFar
BroFar - avatar
+ 3
You need to iterate over the input words and add the price for each one. In your code you only check the first word. strtok example https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm
24th Sep 2023, 6:20 PM
Kamen Studentov
Kamen Studentov - avatar
+ 3
Tarun Gautam if you go with the scanf while loop, then remove fgets, as it would eat the input before scanf gets it. [Optional: keep fgets, and use sscanf() to parse from str and put it into word. It is something like sscanf(str, "%99s", word)]
24th Sep 2023, 7:59 PM
Brian
Brian - avatar
+ 2
Tarun Gautam I'm not in competition with brother Brian
24th Sep 2023, 7:31 PM
BroFar
BroFar - avatar
+ 1
Brian Got it bro Thank you very much
25th Sep 2023, 1:33 AM
Tarun Gautam
Tarun Gautam - avatar
0
BroFar If I'm not wrong here we have no need to declare Macro we can also write directly fgets(str,300,stdin); Am I right or is it important to declare Macro for any reason?
24th Sep 2023, 7:03 PM
Tarun Gautam
Tarun Gautam - avatar
0
Brian Your explanation is very accurate but at last you said I can replace strtok() with while(scanf("%99s",word)!= EOF) but it's not working Can you look at the updated code
24th Sep 2023, 7:13 PM
Tarun Gautam
Tarun Gautam - avatar
0
BroFar Brian Both of you gave me correct and detailed explanations but now I am confused as to whose answer I should mark as best.
24th Sep 2023, 7:19 PM
Tarun Gautam
Tarun Gautam - avatar