mercredi 12 août 2020

I am not getting any output from this code

QUESTION

/* 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 */

#include <stdio.h>
#include <stdlib.h>

#define PIZZA 6.00
#define NACHOS 6.00
#define CHEESEBURGER 10
#define WATER 4.00
#define COKE 5.00

int Comparison(char *, char *);

int main() {

  float Total = 0;

  char ordr[][15] = {"Nachos", "Pizza", "Cheeseburger", "Water"};

  char d = (char)malloc(sizeof(char) * 100);

  // space included input
  fgets(d, 100, stdin);

  for (int i = 0; i < 4; i++) {

    // sending one assigned string at a time and whole user input string
    int n = Comparison(ordr[i], d);

    // To calculate TOTAL
    switch (i) {
    case 0:
      if (n == 0)
        Total = Total + NACHOS;

      else
        Total = Total + COKE;
    case 1:
      if (n == 0)
        Total = Total + PIZZA;

      else
        Total = Total + COKE;
    case 2:
      if (n == 0)
        Total = Total + CHEESEBURGER;

      else
        Total = Total + COKE;
    case 3:
      if (n == 0)
        Total = Total + WATER;

      else
        Total = Total + COKE;

    default:
      Total = Total + COKE;
    }
  }
  printf("%f", Total);

  free(d);

  return 0;
}

int Comparison(char *a, char *b) {
  int cnt = 0, i = 0, j = 0;

  while (*b != ' ' || *b != '\0') {

    // checking if any one character of the input string equal to assigned
    // string
    if ((b + i) != (a + j)) {
      cnt = 1;
    }

    // if not equal I'll move to next word after the space of the input string
    if (cnt == 1) {
      j = 0;
      while (*b != '\0' || *b != ' ') {
        i++;
      }
      i++;
    } else {
      i++;
      j++;
    }
  }
  return cnt;
}

I KNOW C PROGRAM IS SO COMPLEX WHEREAS PYTHON WILL BE SO SIMPLE FOR THIS TYPE OF QUESTION BUT I WANT CLEAR MY CONCEPT IN C. AND I DID NOT ADDED TAXES IN THIS CODE BECAUSE I WANTED TO CHECK IF THIS CODE IS WORKING OR NOT

THANK YOU IN ADVANCE

Aucun commentaire:

Enregistrer un commentaire