0

Can anyone help me to solve this ball park order code?

#include <stdio.h> #include<string.h> int main() { char *menu[]={"Nachos","Pizza","Cheeseburger","Water","Coke"}; char item[300]; float price=0; float tax; fgets(item,300,stdin); for(int i=0;i<5;i++){ if(strncmp(menu[i],item,6)==0) { price+=6; } if (strncmp(menu[i],item,5)==0){ price+=6; } if (strncmp(menu[i],item,12)==0){ price+=10; } if (strncmp(menu[i],item,5)==0){ price+=4; } if (strncmp(menu[i],item,4)==0){ price+=5; } } tax=price*0.07; price+=tax; printf("%.2f",price); return 0; }

5th Mar 2023, 7:27 AM
Nida Noushad
Nida Noushad - avatar
1 Antwort
+ 1
Use strtok() fgets(item, 100, stdin); item[strlen(item)-1] = '\0'; // fgets includes the newline character, // so replace it with a null character. char *p = strtok(item, " "); while(p){ if(!strcmp(p, menu[0])) price+=6.0f; else if ..... ............. ............... p = strtok(NULL, " "); } https://cplusplus.com/reference/cstring/strtok/
5th Mar 2023, 10:07 AM
rodwynnejones
rodwynnejones - avatar