- 1
How bad am I messing up functions.
i decided to.rework it without the function. Thanks to those who helped for the knowledge on how to make it work. https://code.sololearn.com/ctW3qJ83g7Ic/?ref=app
26 odpowiedzi
- 7
Print("I don't know how to code")
+ 4
So what I have found: the best way to learn this is practice practice practice.
Debug your own code. Have some prints within your program that show you what is happening, eg within a loop, or printing as you += your total. Send some values, observe the logic do it's thing, adjust if it's not right.
My very limited experience when working as a Building Management System (= large building HVAC) coder/programmer: take an existing code/function from your library, modify it to suit the application.
We would then implement it with some variables as fixed values, change one thing at a time and observe the system, unlock a variable and prove it "plays nice" from its input, repeat for others.
As an improvement: check what happens if a sensor goes faulty or a device fails to respond to an output, eg fan has power but isn't running.
Basically it becomes cut+paste from your library, then modify to suit.
+ 2
is it passed
"water water water water"
or
{"water", "water", "water", "water"}
or
"water" //1st loop
"water" // 2nd loop
"water" // 3rd loop
"water" // 4th loop?
+ 2
I was honestly looking into strcmp haha gimme a sec, and thx btw
+ 1
I think you are going at this in a way to difficult manner:
Is this a simple cash register, where:
"pizza" & "nachos" cost & 6.00
"cheeseburger" cost $10.00
"water" cost $4.00
and "coke" cost $5.00?
+ 1
Is there a space between the items in the string?
+ 1
I think no space and possibly 4 different inputs. Because if I do a printf("%s%s%s%s", orders[1-4]) it prints all together
+ 1
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
+ 1
I figured that if I separated the string into an array of 4 items, and then compared those items one at a time with a series of else if statements in a function returning each ones value to store into another array I could then easily use the last array given any combination of items to easily do the math
+ 1
Your
printf("%s%s%s%s", orders[1-4]);
Is printing them without the space. They are separate words.
+ 1
Yes, I suppose that is true. I was only printing them to check if my first array was working :P
+ 1
Lots of changes, I've got it working but maybe we can talk you through it first?
Your 2dimension array doesn't need to be a pointer.
Your value definitely doesn't need to be pointer(pointer) and really can just be a float total.
Ok so far?
{edit: these changes.}
char orders[4][50]; //dont need pointer when it is an array
/* unsure what you were trying here.
orders[0];
orders[1];
orders[2];
orders[3];
*/
/* lets just store it as a number, ok?
double **value[4][6];
value[0];
value[1];
value[2];
value[3];
*/
double value; //HT
+ 1
Those changes make your total loop change to something like this:
for (int j = 0; j < 4; j++){
//*value[j] = cost(orders[j]); //HT changed as below
value += cost(orders[j]);
//printf ("%.2f", *value[j]); //HT changed as below
printf ("%.2f ", value); //debugging
}
+ 1
Ok I removed the pointer from the orders array and it still worked ( printing out water 4 times)
Not sure what you mean by float total.
I don't understand how you take the order word like "cheeseburger" and get 10.00 without comparing the word to itself and if true, assigning the true value
+ 1
Ok I made those changes
+ 1
ok think I got it all
+ 1
I am still working with what you have me to understand it. My brain is fried now though haha. Thanks again!
+ 1
Was out for a bit: I would have went at it differently. If the input is a single line string separated by a space.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LONGESTWORD 13
#define ORDERSIZE 4
#define TAX .07
float getvalue(char* single_item){
if(strcmp(single_item, "water") == 0){
return 4.0;
} else if(strcmp(single_item, "nachos") == 0 || strcmp(single_item, "pizza") == 0){
return 6.0;
} else if(strcmp(single_item, "cheeseburger") == 0){
return 10.0;
} else {
return 5.0;
}
}
float subtotal(char* item, float (*getvalue)(char*)){
float subtot = 0;
char *item_order;
const char delim[] = "\n ";
item_order = strtok(item, delim);
while(item_order != NULL){
subtot += getvalue(item_order);
item_order = strtok(NULL, delim);
}
return subtot;
}
int main(void){
float sub = 0.0, tax = 0.0;
char* order;
order = malloc(sizeof *order * LONGESTWORD * ORDERSIZE);
puts("Please order up to 4 items:");
fgets(order, LONGESTWORD * ORDERSIZE, stdin);
sub += subtotal(order, getvalue);
printf("Your subtotal is $%.2f\n", sub);
tax = (sub * TAX);
printf("Your tax is $%.2f\n", tax);
printf("Your order is $%.2f\n", sub + tax);
free(order);
}
https://code.sololearn.com/cPLs7LjxWZFN
+ 1
This is my 5th puzzle, and I have gotten the others. This was just my first time using functions. My problem I think was that I couldn't in my head figure out how to compare strings from I put to string literals.
Based on the two codes of help.you guys posted, I wasn't too far off haha.
+ 1
Thanks everyone for your help. Finally got it to work without the function.