0

i was asked to do a simple programming but i dont know how to make it simple.. i just learn c programming and i used all the basic things about it. i must create a coding with 3 functions. it is about a cafe menu, first function is to display only, 2nd is to calculate and return the prices, 3rd is to print receipt. im stuck at 2nd function :( here is my code..

#include <stdio.h>
#include <string.h>

void displaymenu(void);
double calculate(char* code1[], char* code2[], char* code3[], int quantity1, int quantity2, int quantity3);
double payment(double total);

int main () {
    
    char name[20];
    int tablenum;
    char ord1[3];
    char ord2[3];
    char ord3[3];
    int quant1, quant2, quant3;
    
    printf("~~~~~ Sunny Cafe Order Form ~~~~~ \n");
    printf("Customer Name : ");
    scanf("%s", &name);
    printf(" Table Number : ");
    scanf("%d", &tablenum);
    
    displaymenu();
    
    printf("Order 1 : ");
    scanf("%s", &ord1);
    printf("Order1 quantity: ");
    scanf("%d", &quant1);
    printf("Order 2 : ");
    scanf("%s", &ord2);
    printf("Order2 quantity: ");
    scanf("%d", &quant2);
    printf("Order 3 : ");
    scanf("%s", &ord3);
    printf("Order3 quantity: ");
    scanf("%d", &quant3);
    
    double total_price, total_bill;
    total_price = calculate(ord1, ord2, ord3, quant1, quant2, quant3);
    total_bill = payment(total_price);
    
    printf(" Please Come Again!");
    
    return 0;
}

void displaymenu() {

printf("~~~~~ Sunny Cafe Menu ~~~~~ \n");
printf("Food Menu:\nf1 - Burger (RM13.50)\nf2 - Sandwich(RM10.50)\nf3 - Hotdog(RM10.00)\nf4 - Pasta(RM12.50)\nf5 - Mushroom Soup(RM8.90)\n\n");
printf("Drinks Menu:\nd1 - Coffee(RM5.90)\nd2 - Tea(RM4.90)\nd3 - Juice(RM6.50)\nd4 - Sky Juice(RM0.50)\nd5 - Fizzy(RM2.00)\n\n");

}

double calculate(char* code1[], char* code2[], char* code3[], int quantity1, int quantity2, int quantity3) {
    
    double price1, price2, price3;
    
    if (code1 = 'f1') {
        price1 = 13.50;
    }
    else if (code1 = 'f2') {
        price1 = 10.50;
    }
    else if (code1 = 'f3') {
        price1 = 10.00;
    }
    else if (code1 = 'f4') {
        price1 = 12.50;
    }
    else if (code1 = 'f5') {
        price1 = 8.90;
    }
    else if (code1 = 'd1') {
        price1 = 5.90;
    }
    else if (code1 = 'd2') {
        price1 = 4.90;
    }
    else if (code1 = 'd3') {
        price1 = 6.50;
    }
    else if (code1 = 'd4') {
        price1 = 0.50;
    }
    else {
        price1 = 2.00;
    }
    
    if (code2 = 'f1') {
        price2 = 13.50;
    }
    else if (code2 = 'f2') {
        price2 = 10.50;
    }
    else if (code2 = 'f3') {
        price2 = 10.00;
    }
    else if (code2 = 'f4') {
        price2 = 12.50;
    }
    else if (code2 = 'f5') {
        price2 = 8.90;
    }
    else if (code2 = 'd1') {
        price2 = 5.90;
    }
    else if (code2 = 'd2') {
        price2 = 4.90;
    }
    else if (code2 = 'd3') {
        price2 = 6.50;
    }
    else if (code2 = 'd4') {
        price2 = 0.50;
    }
    else {
        price2 = 2.00;
    }
    
    if (code3 = 'f1') {
        price3 = 13.50;
    }
    else if (code3 = 'f2') {
        price3 = 10.50;
    }
    else if (code3 = 'f3') {
        price3 = 10.00;
    }
    else if (code3 = 'f4') {
        price3 = 12.50;
    }
    else if (code3 = 'f5') {
        price3 = 8.90;
    }
    else if (code3 = 'd1') {
        price3 = 5.90;
    }
    else if (code3 = 'd2') {
        price3 = 4.90;
    }
    else if (code3 = 'd3') {
        price3 = 6.50;
    }
    else if (code3 = 'd4') {
        price3 = 0.50;
    }
    else {
        price3 = 2.00;
    }
    
    double totprice1 = price1 * quantity1;
    double totprice2 = price2 * quantity2;
    double totprice3 = price3 * quantity3;
    
    printf("%s x %d = %f", code1, quantity1, totprice1);
    printf("%s x %d = %f", code2, quantity2, totprice2);
    printf("%s x %d = %f", code3, quantity3, totprice3);
    
    double result = totprice1 + totprice2 + totprice3;
    
    return result;
}

double payment(double total) {
    
    double amountgiven;
    
    printf(" Total bill : RM %.2f \n", total);
    printf("Customer paid : \n");
    scanf(" %lf", &amountgiven);
    double change = amountgiven - total;
    printf("Balance : RM %.2f", change);
    
}
kaylum
  • 13,833
  • 2
  • 22
  • 31
Ben75
  • 1
  • 1
    `if (code1 = 'f1')` Wrong operator. `=` is assignment. `==` is compare. And even that is incorrect as strings cannot be compare like that. Need to use `strcpy`. – kaylum Apr 29 '21 at 03:04
  • Does this answer your question? [How do I properly compare strings in C?](https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c) – kaylum Apr 29 '21 at 03:05
  • If you still have problems after fixing those then please describe the specific problem. "I'm stuck" is neither a question nor a description of a specific issue. – kaylum Apr 29 '21 at 03:06
  • I think the whole if-else if-else part works, but is kinda unreadable. If you want to make it easier, you can store the prices in an array, parse the number in the string and just use this index to get the price. This will be a little easier. – Bananenkönig Apr 29 '21 at 06:59

0 Answers0