0

I am writing a code that asks the user to enter a present name and it's price and then stores this data in two arrays PresentArray_name and PresentArray_price. The PresentArray_name is generated randomly, and it's price is presented. I can get it to print the random present name but I can't the presents price to be printed. Should I be using a 2d array, and if so how would I do that? The code is below:

void add_presents()
{
    int i=0;
    char PresentArray_name[10][30];//A 2D array to store names of presents, 
which can only be 30 characters long
    int PresentArray_price[10];
    printf("This area is a little difficult to navigate as the answer for 
the question is stored before the question is displayed! Simply type in the 
toy hit the enter key, and then input it's price then hit the enter key! 
Don't pay attention to the headings!\n");
    for (i=0;i<10;i++)
    {
        printf("Enter present %d:\n", i+1);
        scanf("%s", &PresentArray_name[i]);//Stores the presents in the 
PresentArray_name
        printf("Enter price for present %d:\n", i+1);
        scanf("%d", &PresentArray_price[i]);//Stores the presents in the 
PresentArray_price
        if (PresentArray_price[i]<5||PresentArray_price[i]>15)
        {
            printf("Invalid! Enter a price between 5 and 15:\n");
            scanf("%d", &PresentArray_price[i]);
        }

    }
    for (i=0;i<10;i++)
    {
        printf("Present %s costs %d\n", PresentArray_name[i], 
PresentArray_price[i]);//Prints the names and the costs of each present
    }
    srand(time(NULL));
    time_t t;
    srand((unsigned)(&t));
    for (i=0;i<total_number_of_presents_required;i++)//Loop counter form 
another part of the code
    {
        printf("Kid: %d gets %s, which costs: %d\n",i+1, 
PresentArray_name[rand()%10], PresentArray_price[i]);
    }
}
24
  • Post sample data use as input, output seen and output expected. Commented Jan 5, 2018 at 19:47
  • 1
    Legendary Assassin Hit Your comment describes the input and output. Posting the true input used and output seen is more useful. Commented Jan 5, 2018 at 19:59
  • 1
    BTW, Drop the & in scanf("%s", &PresentArray_name[i]); Commented Jan 5, 2018 at 20:02
  • 1
    "The program displays the price inputted for the first present as the price for a random present. For example Kid 1 gets G which costs 5" --> Of course it does with PresentArray_name[rand()%10], PresentArray_price[i] - they use different indexes. Commented Jan 5, 2018 at 20:04
  • 1
    I have kind of lost myslef in mhy own program 😂 Commented Jan 5, 2018 at 20:23

2 Answers 2

1

You can save the return value from the rand function, so you can use it both with the name and the price.

void add_presents()
{
    int PresentArray_price[10];
    char PresentArray_name[10][30];

    printf("This area...\n");

    for(int i = 0; i < 10; ++i)
    {
        printf("Enter present %d:\n", i + 1);
        scanf("%s", &PresentArray_name[i]);

        printf("Enter price for present %d:\n", i + 1);
        scanf("%d", &PresentArray_price[i]);

        if (PresentArray_price[i] < 5 || PresentArray_price[i] > 15)
        {
            printf("Invalid! Enter a price between 5 and 15:\n");
            scanf("%d", &PresentArray_price[i]);
        }
    }

    for(int i = 0; i < 10; ++i)
        printf("Present %s costs %d\n", PresentArray_name[i], PresentArray_price[i]);

    srand(time(NULL));

    for(int i = 0; i < total_number_of_presents_required; ++i)
    {
        int n = rand() % 10;
        printf("Kid: %d gets %s, which costs: %d\n", i + 1, PresentArray_name[n], PresentArray_price[n]);
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

I tried your code but the compiler comes out with an eeror of saying something like variable intialisation is only available in C11 or C99, which I don't really understand. The error appears in the first for loop
@LegendaryAssassinHit That is because you are not using this code. You are using your own edited version of this code which you broke in the process of changing.
Jango, drop the & from scanf("%d", &PresentArray_price[i]);
Oh ok, but I did copy and paste this exact code, but I'll try it again
The compiler errors, and I quote: " 'for' loop initial declarations are only allowed in only allowed in C99 or C11 mode"
|
0

If you know about structs, I recommend using those, then you only have 1 data structure to keep track of:

#include <stdio.h>
#include <time.h>

struct Present
{
  char name[30];  // note this will only hold a name 29 long to ensure room for NUL terminator
  int price;
};

int main(void)
{
  srand(time(NULL));
  // now, you can simply create an array of this struct, and the name
  // and price will always be together
  struct Present presents[10];

  int i;
  for (i=0; i<10; i++)
  {
    // get user input. You should also check the return value of scanf which I'm omitting here for brevity
    // get the present name
    scanf("%s", presents[i].name);
    // get the price
    scanf("%d", &(presents[i].price));
  }

  // now when you randomly select a present, you'll get it's name and price
  i = rand()%10;
  printf("present name: %s\n", presents[i].name);
  printf("present price: %d\n", presents[i].price);

  return 0;
}

Using separate raw arrays for each data field doesn't scale well. What if you also want to keep track of the present manufacturer's name? Where it was bought? The date it was bought? etc.. You can just add those fields to struct Present, and you still only have to deal with presents throughout the code, instead of a different array (and array name).

4 Comments

I tried your code, and seems to break the program, i don't really know why, but when the code is done making the total_number_of_presents_required, and then goes to this function the code breaks
@LegendaryAssassinHit Not sure what "break the program" means,, your code presented doesn't initialize total_number_of_presents_required (or even declare it), so if you're looping over that without initialization, that is UB and most likely going way beyond the bounds of the array (10). I thought you were primarily asking for advice on how to organize your data,, if that's not the case I should delete this answer.
when i say break I mean that the ouput does not appear on screen after the total_number_of_presents_required is entered. I didn't really ask for structuring my data, but the struct was really helpful in helping me learn, I just asked for how to print the price of the randomly generated present that is stored beforehand
@LegendaryAssassinHit whether you use a struct or 2 arrays, you have to use the same index to access the data. This is what chux was talking about in the comments. rand%10 is not equal to i, unless by chance. If you want to see the 3rd present's name and it's price, then you need to access [2] of both your arrays or my struct. If you want to see a random index, then you'll need save the result of rand()%10 and use that to index your arrays: int n = rand()%10; presentName[n]; presentPrice[n];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.