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]);
}
}
&inscanf("%s", &PresentArray_name[i]);PresentArray_name[rand()%10], PresentArray_price[i]- they use different indexes.