+ 5

Izzy the Iguana Challenge in C

Hi. It worked for all of the tests but one and I cannot figure out what is the problem. Can you help me? Here is my code: #include <stdio.h> #include <string.h> int main () { char str [200]; int snack = 0; scanf ("%[^\n]*c", str); char *ptr = strtok(str, " "); while (ptr != NULL) { if (strcmp(ptr, "Letuce")==0) { snack += 5; } else if (strcmp(ptr, "Carrot")==0) { snack += 4; } else if (strcmp(ptr, "Mango")==0) { snack += 9; } else if (strcmp(ptr, "Cheeseburger")==0) { snack += 0; } ptr = strtok (NULL, " "); } if (snack >= 10) { printf ("Come on down!"); } else { printf ("Time to wait"); } return 0; }

12th Oct 2021, 1:03 PM
Madalina Peter
Madalina Peter - avatar
3 Respuestas
+ 4
You misspelled "Lettuce" in the comparison, and the phrase should be "Come on Down!" with a big 'D' instead of a small one.
12th Oct 2021, 1:09 PM
Shadow
Shadow - avatar
+ 1
Thank you very much. I have seen the problem with the "d", but not the lettuce problem.
12th Oct 2021, 1:32 PM
Madalina Peter
Madalina Peter - avatar
0
[Solved] using JAVA: 😏 import java.util.Scanner; import java.util.HashMap; public class Program { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String txt = scan.nextLine(); HashMap<String,Integer> snacks = new HashMap<String,Integer>(); snacks.put("Lettuce",5); snacks.put("Carrot",4); snacks.put("Mango",9); snacks.put("Cheeseburger",0); int sum = 0; for(String i: txt.split(" ")){ if(snacks.containsKey(i)){ sum+= snacks.get(i); }else { sum =0; } } if(sum>10){ System.out.println("Come on Down!"); }else{ System.out.println("Time to wait"); } } }
24th Jan 2022, 11:58 PM
Moussa Diallo
Moussa Diallo - avatar