0

I have created a user input.

I want to display the inputs I have taken from the for loop.

i dont know how to arrays tho (edited) This is all I have. My code explains more:

Item Priority Financed Cost
---- -------- -------- -----------
 1      1        n       39030.15
 2      3        y     1200000.00
 3      2        n      350500.25
---- -------- -------- -----------
                     $ 1589530.40

something like this

here is my code

#define _CRT_SECURE_NO_WARNINGS

#define minINCOME 500.00
#define maxINCOME 400000.00

#include <stdio.h>

int main(void)
{
    float userIncome ;
    int wishlistItems;
    float itemCost;
    const float minitemCost = 100.00;
    int itemImp;
    char finance;
    
    printf("+--------------------------+\n"
           "+   Wish List Forecaster   |\n"
           "+--------------------------+\n\n");
   
    do
    {
        printf("Enter your monthly NET income: $");
        scanf(" %f", &userIncome);

        if (userIncome < minINCOME)
        {
            printf("ERROR: You must have a consistent monthly income of at least $500.00\n\n");
        }

        else if (userIncome > maxINCOME)
        {
            printf("ERROR: Liar! I'll believe you if you enter a value no more than $400000.00\n\n");
        }
    } while( (userIncome > maxINCOME || userIncome < minINCOME));

    do
    {
        printf("How many wish list items do you want to forecast?: ");
        scanf(" %d", &wishlistItems);
        if (wishlistItems > 10 || wishlistItems < 1)
        {
            printf("ERROR: List is restricted to between 1 and 10 items.\n\n");
        }
    } while (wishlistItems > 10 || wishlistItems < 1);

    for (size_t i = 1; i <= wishlistItems; i++ )
    {
        do {

            printf("Item-%d Details:\n", i);
            printf("   Item cost: $");
            scanf(" %f", &itemCost);

            if (itemCost < minitemCost)
            {
                printf("      ERROR: Cost must be at least $100.00\n");
            }
        } while (itemCost < minitemCost);

        do {
            printf("   How important is it to you? [1=must have, 2=important, 3=want]: ");
            scanf(" %d", &itemImp);

            if (itemImp  < 1 || itemImp >3 )
            {
                printf("      ERROR: Value must be between 1 and 3\n");
            }
        } while (itemImp < 1 || itemImp >3);
    
        do {
            printf("   Does this item have financing options? [y/n]: ");
            scanf(" %c", &finance);
            if (finance != 'y' || finance != 'n')
            {
                printf("      ERROR: Must be a lowercase 'y' or 'n'\n");
            }
        } while (finance != 'y' || finance != 'n');
    }

    return 0;
} 
ran
  • 23
  • 4
  • 2
    "*i dont wanna use arrays*". Why not? How else do you expect to store the user data and display it afterwards? – kaylum Feb 15 '22 at 19:45
  • `finance != 'y' || finance != 'n'` should use `&&` instead of `||`. See https://stackoverflow.com/questions/26337003/why-non-equality-check-of-one-variable-against-many-values-always-returns-true – Barmar Feb 15 '22 at 19:48
  • I suppose you could have the loop that reads user input write to a file, and then when the user input is complete, read that file (without arrays) and display the information. But it is hardly a sensible approach unless you think the input will be so voluminous that arrays are really not practical (and you would have said if you were dealing with hundreds of millions of rows of data, or more, would you not?). – Jonathan Leffler Feb 15 '22 at 19:51
  • You don't want to use arrays because of a sincerely held religious conviction, or because your instructor imposed this restriction out of pedagogy? Are you able to use pointers? Call `malloc`? – Steve Summit Feb 15 '22 at 19:55
  • @kaylum becuz i dont know how to – ran Feb 15 '22 at 21:40
  • Then you should ask about how to use arrays instead of stating that artificial constraint. It changes the question completely with and without that requirement. – kaylum Feb 15 '22 at 21:51
  • @kaylum can you guide me, i mean whenever i make `itemCost[10]` and then compare it with `itemCost < minitemCost` it shows me error saying expected an expression – ran Feb 15 '22 at 21:58

1 Answers1

0

Given that wishlistItems is limited to 10, there shouldn't be any issue using an array. Even if the user doesn't fill it up fully, 10 items is not that much memory to be concerned about.

On the other hand, given that theoretically you want to build a scalable program, and wishlistItems can change, I would consider a linked list here. It will require pointers and dynamic memory allocation (as pointed by Steve), but allows you to maintain all the data in memory, while allocating the exact memory size required here.

Each item on the list will look approximately like this:

    struct wish_list_item {
    float cost;
    int impact;
    char finance;
    struct wish_list_item *next;
};

Where next points to the next item on the list, or is NULL if it is the last item. Then I would suggest to implement a couple of helper functions to work with the list more comfortably (add_element, del_element, print_list, find_element, insert_element), all according to your needs. Should be easy to find example implementations, given that Linked list is the second most-used data structure after array.

  • i asked my instructor and he has allowed me to use arrays, but i dont know how to do that either, can you help me with tthat? – ran Feb 15 '22 at 21:27
  • Same idea, except you don't need the last element in the struct. `#define WISH_LIST_MAX_SIZE 10` `struct wish_list_item wish_list[WISH_LIST_MAX_SIZE];` Then just fill the array in the for loop. – Nachum Barcohen Feb 27 '22 at 08:43