0

I have a program in C to stdin products and according prices, and the program calculates and applies user input discount to all of the standard prices, however, I have 2 problems with my codebelow: ``` int ReadArray(float arr[]) { int size = sizeof arr[MAX_SIZE] / sizeof *arr; return size; }

int GetIntInRange(int min, int max)
{
    int num;
    do
    {
        printf("> ");
        scanf("%d", &num);
        if (num < min || num > max)
            printf("\nRetry! Input must be in between %d and %d\n", min, max);
    }
    while (num < min || num > max);
    getchar();
    return num;
}

float DiscountCalculation(float discountPercent, float regPrice)
{
    float salesPrice;

    salesPrice = regPrice * (1 - discountPercent / HUNDRED);

    return salesPrice;
}

void ShowBrandsAndPrices(char *brands[], float prices[], int len)
{
    int i;

    for (i = 0; i < len; i++)
    {
        printf("%s\t%.2f\n", brands[i], prices[i]);
    }
}
```

My problem #1: my ReadArray function does not calculate the length of array properly, it only returns the length of 1 array member.

Problem #2: the calculated discounts do not show up. What am I doing wrong here? Any help will be much appreciated.

soundsfierce
  • 45
  • 2
  • 9
  • 1
    Here is the trick about finding out the array size in a function: You can't. – Yunnosch Nov 22 '20 at 20:07
  • Does this answer your question? [Finding length of array inside a function](https://stackoverflow.com/questions/17590226/finding-length-of-array-inside-a-function) – Yunnosch Nov 22 '20 at 20:08
  • OK, but how then I could create a function to read in the arrays from stdin any text file with product names and prices? – soundsfierce Nov 22 '20 at 20:24
  • @soundsfierce You just read in the data and reallocate as necessary. `man realloc` – William Pursell Nov 24 '20 at 21:51
  • Here's a simple example that just reads a file into memory, reallocating as needed: https://github.com/wrp/examples/blob/main/c/dynamic-array.c – William Pursell Nov 24 '20 at 21:53

0 Answers0