Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************
- * Malin Fyhr *
- * mfr14003 *
- * 19930622-1608 *
- * 2014-10-10 *
- * Uppgift 5.1 *
- * Ett program som ber *
- * användaren skriva in en mat- *
- * lista och skriver sedan ut *
- * den. Inehåller felsökning på *
- * ord & siffror *
- *******************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <conio.h>
- #include <string.h>
- // Struct för de olika variablerna i listan
- typedef struct groceryList
- { char grocery[50];
- float amount;
- char unit[10];
- } shoppinglist;
- int error_handling(char str[30]);
- void listInput(shoppinglist **list, int i);
- int main()
- {
- int i, x , l;
- char restart;
- shoppinglist **list;
- list = malloc(sizeof(int)*l);
- for(i=0, l=1; restart!='x'; i++, l++)
- {
- if(i>=0)
- list=realloc(list, l*sizeof(int));
- listInput(&list[i],i);
- do
- {
- printf("\nDo you want to add another grocery? if so press 'v'") ;
- printf("\n else press 'x' print out the list!\n");
- fflush(stdin);
- restart = _getch();
- }while(restart!='v'&&restart!='x');
- printf("\n");
- if(restart=='x')
- {
- printf("\n*Shopping List*\n");
- for(x=0; x<=i; ++x)
- printf("%s\t\n%.1f\n%s\n\n",list[x]->grocery, list[x]->amount,list[x]->unit);
- getch();
- }
- }
- free(list);
- list=NULL;
- return 0;
- }
- int error_handling(char str[30]){ //Funktion som tar hand om felhantering vid inmatning av strängar(kollar bokstäver).
- int count;
- for(count=0; count<strlen(str); count++){
- if(!isalpha(str[count])){ //Om inmatningen inte är av bokstäver, skickas felmeddelande.
- printf("Error! Please use letters ONLY!\n");
- return 0;
- }
- }
- return 1;
- }
- void listInput(shoppinglist **list, int i)
- {
- int check, j;
- shoppinglist *list_temp;
- list_temp = malloc(sizeof(shoppinglist));
- check=0;
- while(check == 0)
- {
- fflush(stdin);
- printf("\nGrocery %d: ", i+1);
- scanf("%s", list_temp->grocery);
- check=error_handling(list_temp[i].grocery);
- }
- check = 0;
- while(check == 0)
- {
- printf("\nAmount: ");
- if(scanf("%f", &list_temp->amount)!=1)
- printf("Please enter numbers only!");
- else
- check=1;
- fflush(stdin);
- }
- check = 0;
- while(check == 0)
- {
- fflush(stdin);
- printf("\nUnit: ");
- scanf("%s", list_temp->unit);
- check=error_handling(list_temp[i].unit);
- }
- for(j=0;j<40;j++)
- printf("*");
- *list=list_temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement