#Infinite loop?
24 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
@barren basin
Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!
whats menu?
sorry it's an array of structs, string being the item, and int being the price
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
// Number of menu items
// Adjust this value (10) to number of items input below
#define NUM_ITEMS 10
// Menu itmes have item name and price
typedef struct
{
string item;
float price;
} menu_item;
// Array of menu items
menu_item menu[NUM_ITEMS];
// Add items to menu
void add_items(void);
// Calculate total cost
float get_cost(string item);
int main(void)
{
add_items();
printf("\nWelcome to Beach Burger Shack!\n");
printf("Choose from the following menu to order. Press enter when done.\n\n");
for (int i = 0; i < NUM_ITEMS; i++)
{
printf("%s: $%.2f\n", menu[i].item, menu[i].price);
}
printf("\n");
float total = 0;
while (true)
{
string item = get_string("Enter a food item: ");
if (strlen(item) == 0)
{
printf("\n");
break;
}
total += get_cost(item);
}
printf("Your total cost is: $%.2f\n", total);
}```
@barren basin
Note: Make sure to use back-ticks (`) and not quotes (')
Note: Make sure to specify a highlighting language, e.g. `cpp`, after the back-ticks
```c
int main() {}
```
int main() {}
It looks like it's the while loop in main
but the course i'm taking says i don't mess with the main function they gave me..... So either they made a mistake or it's in the "get_cost" function
your function is fine (well... fine in the sense there's no infinite loop there). this loop however
while (true)
{
string item = get_string("Enter a food item: ");
if (strlen(item) == 0)
{
printf("\n");
break;
}
total += get_cost(item);
}```
will only terminate if `item` happens to be an empty string
couple things to note regardless:
- global variables are frowned upon. it makes it hard to reason about your code
void foo(void)is a code smell (outside of testing) and should likely be refactoredstrcasecmpis a POSIX function (i.e. its a non standard function)
i'm aware cs50 doesn't teach C but rather teach the concept of programming (the fact they chose C for this is beyond me) - but still, one should at least try to write C correctly
Yes sorry, it's the code they gave me :"(
I just had to fill in the 2 functions
add_items and get_cost
they provided the prototype/signature
You're saying strcasecmp is bad?
i'm not saying its bad, no. i'm saying that if your libc isn't POSIX compliant strcasecmp will simply won't exist
idk what that means, but it's probably not important lol
for now anyway
as we switch from C ot something else in later weeks
anwyays thanks again man!
That helped, now isee it's their problem, not my functions 🙂
i don't think its their problem either. it's the problem with the input you're giving the program
@barren basin Has your question been resolved? If so, type !solved :)