Advertisement
Guest User

6-1

a guest
Oct 31st, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1.  
  2.  
  3. /*******************************
  4. * Malin Fyhr *
  5. * mfr14003 *
  6. * 19930622-1608 *
  7. * 2014-10-10 *
  8. * Uppgift 5.1 *
  9. * Ett program som ber *
  10. * användaren skriva in en mat- *
  11. * lista och skriver sedan ut *
  12. * den. Inehåller felsökning på *
  13. * ord & siffror *
  14. *******************************/
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <ctype.h>
  18. #include <conio.h>
  19. #include <string.h>
  20.  
  21. // Struct för de olika variablerna i listan
  22. typedef struct groceryList
  23. { char grocery[50];
  24. float amount;
  25. char unit[10];
  26. } shoppinglist;
  27.  
  28.  
  29.  
  30. int error_handling(char str[30]);
  31. void listInput(shoppinglist **list, int i);
  32.  
  33.  
  34. int main()
  35. {
  36. int i, x , l;
  37. char restart;
  38. shoppinglist **list;
  39. list = malloc(sizeof(int)*l);
  40.  
  41. for(i=0, l=1; restart!='x'; i++, l++)
  42. {
  43. if(i>=0)
  44. list=realloc(list, l*sizeof(int));
  45. listInput(&list[i],i);
  46. do
  47. {
  48. printf("\nDo you want to add another grocery? if so press 'v'") ;
  49. printf("\n else press 'x' print out the list!\n");
  50. fflush(stdin);
  51. restart = _getch();
  52. }while(restart!='v'&&restart!='x');
  53. printf("\n");
  54. if(restart=='x')
  55. {
  56. printf("\n*Shopping List*\n");
  57. for(x=0; x<=i; ++x)
  58. printf("%s\t\n%.1f\n%s\n\n",list[x]->grocery, list[x]->amount,list[x]->unit);
  59. getch();
  60. }
  61.  
  62. }
  63. free(list);
  64. list=NULL;
  65. return 0;
  66. }
  67.  
  68. int error_handling(char str[30]){ //Funktion som tar hand om felhantering vid inmatning av strängar(kollar bokstäver).
  69. int count;
  70. for(count=0; count<strlen(str); count++){
  71. if(!isalpha(str[count])){ //Om inmatningen inte är av bokstäver, skickas felmeddelande.
  72. printf("Error! Please use letters ONLY!\n");
  73. return 0;
  74. }
  75. }
  76. return 1;
  77. }
  78.  
  79. void listInput(shoppinglist **list, int i)
  80. {
  81. int check, j;
  82. shoppinglist *list_temp;
  83. list_temp = malloc(sizeof(shoppinglist));
  84.  
  85. check=0;
  86. while(check == 0)
  87. {
  88. fflush(stdin);
  89. printf("\nGrocery %d: ", i+1);
  90. scanf("%s", list_temp->grocery);
  91. check=error_handling(list_temp[i].grocery);
  92. }
  93.  
  94. check = 0;
  95. while(check == 0)
  96. {
  97. printf("\nAmount: ");
  98. if(scanf("%f", &list_temp->amount)!=1)
  99. printf("Please enter numbers only!");
  100. else
  101. check=1;
  102. fflush(stdin);
  103. }
  104.  
  105. check = 0;
  106. while(check == 0)
  107. {
  108. fflush(stdin);
  109. printf("\nUnit: ");
  110. scanf("%s", list_temp->unit);
  111. check=error_handling(list_temp[i].unit);
  112. }
  113. for(j=0;j<40;j++)
  114. printf("*");
  115. *list=list_temp;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement