Guest User

Untitled

a guest
Jan 5th, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. void add_presents()
  2. {
  3. int i=0;
  4. char PresentArray_name[10][30];//A 2D array to store names of presents,
  5. which can only be 30 characters long
  6. int PresentArray_price[10];
  7. printf("This area is a little difficult to navigate as the answer for
  8. the question is stored before the question is displayed! Simply type in the
  9. toy hit the enter key, and then input it's price then hit the enter key!
  10. Don't pay attention to the headings!n");
  11. for (i=0;i<10;i++)
  12. {
  13. printf("Enter present %d:n", i+1);
  14. scanf("%s", &PresentArray_name[i]);//Stores the presents in the
  15. PresentArray_name
  16. printf("Enter price for present %d:n", i+1);
  17. scanf("%d", &PresentArray_price[i]);//Stores the presents in the
  18. PresentArray_price
  19. if (PresentArray_price[i]<5||PresentArray_price[i]>15)
  20. {
  21. printf("Invalid! Enter a price between 5 and 15:n");
  22. scanf("%d", &PresentArray_price[i]);
  23. }
  24.  
  25. }
  26. for (i=0;i<10;i++)
  27. {
  28. printf("Present %s costs %dn", PresentArray_name[i],
  29. PresentArray_price[i]);//Prints the names and the costs of each present
  30. }
  31. srand(time(NULL));
  32. time_t t;
  33. srand((unsigned)(&t));
  34. for (i=0;i<total_number_of_presents_required;i++)//Loop counter form
  35. another part of the code
  36. {
  37. printf("Kid: %d gets %s, which costs: %dn",i+1,
  38. PresentArray_name[rand()%10], PresentArray_price[i]);
  39. }
  40. }
  41.  
  42. #include <stdio.h>
  43. #include <time.h>
  44.  
  45. struct Present
  46. {
  47. char name[30]; // note this will only hold a name 29 long to ensure room for NUL terminator
  48. int price;
  49. };
  50.  
  51. int main(void)
  52. {
  53. srand(time(NULL));
  54. // now, you can simply create an array of this struct, and the name
  55. // and price will always be together
  56. struct Present presents[10];
  57.  
  58. int i;
  59. for (i=0; i<10; i++)
  60. {
  61. // get user input. You should also check the return value of scanf which I'm omitting here for brevity
  62. // get the present name
  63. scanf("%s", presents[i].name);
  64. // get the price
  65. scanf("%d", &(presents[i].price));
  66. }
  67.  
  68. // now when you randomly select a present, you'll get it's name and price
  69. i = rand()%10;
  70. printf("present name: %sn", presents[i].name);
  71. printf("present price: %dn", presents[i].price);
  72.  
  73. return 0;
  74. }
Add Comment
Please, Sign In to add comment