#Infinite loop?

24 messages · Page 1 of 1 (latest)

barren basin
#

For some reason i'm getting an infinite loop, just trying to return the cost of an item

gaunt briarBOT
#

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

Screenshots!

Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!

mossy frigate
#

whats menu?

barren basin
#

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);
}```
gaunt briarBOT
#

@barren basin

It looks like you may have code formatting errors in your message

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

Markup

```c
int main() {}
```

Result
int main() {}
barren basin
#

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

mossy frigate
#

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 refactored
  • strcasecmp is 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
barren basin
#

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?

mossy frigate
#

i'm not saying its bad, no. i'm saying that if your libc isn't POSIX compliant strcasecmp will simply won't exist

barren basin
#

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 🙂

mossy frigate
#

i don't think its their problem either. it's the problem with the input you're giving the program

gaunt briarBOT
#

@barren basin Has your question been resolved? If so, type !solved :)

barren basin
#

anyways thanks aagain 🙂

#

!solved