- 
/*C program that lets the user choose if he wants to add a item to the shopping list or print it out.*/
- 
  
- 
  
- 
#include <stdio.h> 
- 
#include <stdlib.h> 
- 
#include <ctype.h> 
- 
#include <string.h> 
- 
#include <windows.h> 
- 
  
- 
typedef struct{ 
- 
  
- 
    char name[20]; 
- 
    char unit[20];          // SKAPAR EN STRUCT OCH DÖPER DEN TILL SHOPPINGLIST MED HJÄLP AV TYPEDEF FÖR ATT ANVÄNDA DEN SMIDIGARE 
- 
    float amount; 
- 
    int uniqueid; 
- 
  
- 
  
- 
}shoppingList; 
- 
  
- 
  
- 
int error1(char*string) 
- 
{ 
- 
    int i=0;                //SKAPAR FUNTIONEN ERROR 1 SOM TAR EN STRÄNG SOM ARGUMENT 
- 
    int length=0; 
- 
    length=strlen(string); 
- 
  
- 
    for(i=0;i<length;i++) 
- 
    {                                       //ANVÄNDER MIG AV ISALPHA OCH TITTAR OM DET INTE ÄR BOKSTÄVER A-Z RETURNAR VI -1 SOM ÄR VÅRAN FELHANTERING 
- 
            if(!isalpha(string[i])) 
- 
            { 
- 
                return -1; 
- 
            } 
- 
    } 
- 
  
- 
    return 0;                    // ANNARS RETURNEAR VI 0 OCH DÅ STÄMMER ALLT 
- 
} 
- 
  
- 
int error2(char*str) 
- 
{ 
- 
    int i; 
- 
    int length;                 // SKAPAR FUNKTIONEN ERROR 2 SOM OCKSÅ TAR EN STRÄNG SOM ARGUMENT 
- 
    int count = 0; 
- 
    length=strlen(str); 
- 
  
- 
    for(i=0;i<length;i++) 
- 
    { 
- 
        if (str[i] == '.')      // TITTAR OM DET FINNS EN PUNKT I STRÄNGEN, OM DET FINNS SÅ PLUSSAR VI PÅ COUNT MED 1 FÖR VARJE PUNKT SOM FINNS 
- 
            { 
- 
                count++; 
- 
  
- 
                if (str[0] == '.' || str[length-1] == '.' || count > 1) 
- 
                { 
- 
                                                                                    /* TITTAR OM FÖRSTA ELLER SISTA TECKNET I STRÄNGEN ÄR EN PUNKT ELLER OM DET FINNS MER ÄN EN PUNKT */ 
- 
                {                                                                   /*  OCH DÅ RETURNERAR VI -1 SOM ÄR VÅRAN FELHANTERING */ 
- 
                    return -1; 
- 
                } 
- 
            } 
- 
            else if(!isdigit(str[i]))   // ANVÄNDER MIG AV ISDIGIT OCH TITTAR OM DET INTE ÄR SIFFROR MELLAN 0-9 SÅ RETURNERAR VI OCKSÅ -1 
- 
            { 
- 
                return -1; 
- 
            } 
- 
    } 
- 
  
- 
    return 0;                           // ANNARS RETUNERAR VI O DÅ ALLT STÄMMER 
- 
  
- 
} 
- 
  
- 
  
- 
shoppingList *additems(shoppingList*list,int count) 
- 
{ 
- 
    char temparray[20];                             // SKAPAR FUNKTIONEN ADDITEMS OCH DEN TAR EN STRUCTPEKARE OCH EN INT SOM ARGUMENT 
- 
    int i; 
- 
    HANDLE hConsole; 
- 
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE); 
- 
  
- 
    list = realloc(list, count*sizeof(shoppingList));       // SÄTTER STRUCT PEKAREN TILL ATT VARA LIKA MED REALLOC FÖR ATT ALLOKERA 
- 
  
- 
    i=count; 
- 
  
- 
    list[i-1].uniqueid=i; 
- 
  
- 
        do 
- 
        { 
- 
  
- 
                printf("\nName on item %d:",count); 
- 
                fflush(stdin); 
- 
                gets(list[i-1].name); 
- 
  
- 
                if(error1(list[i-1].name) == -1) 
- 
                { 
- 
                    SetConsoleTextAttribute(hConsole,12); 
- 
                    printf("-> Invalid input, try again! <-\n"); 
- 
                    SetConsoleTextAttribute(hConsole,7); 
- 
  
- 
                } 
- 
  
- 
        }while(error1(list[i-1].name)); 
- 
  
- 
  
- 
                    do 
- 
                    { 
- 
  
- 
  
- 
                                printf("Amount:"); 
- 
                                fflush(stdin); 
- 
                                gets(temparray); 
- 
  
- 
                                if(error2(temparray) == -1) 
- 
                                { 
- 
                                    SetConsoleTextAttribute(hConsole,12); 
- 
                                    printf("-> Invalid input, try again! <-\n"); 
- 
                                    SetConsoleTextAttribute(hConsole,7); 
- 
                                } 
- 
  
- 
                    }while(error2(temparray) == -1); 
- 
  
- 
                    list[i-1].amount=atof(temparray); 
- 
  
- 
  
- 
                                    do 
- 
                                    { 
- 
                                                printf("Unit:"); 
- 
                                                fflush(stdin); 
- 
                                                gets(list[i-1].unit); 
- 
  
- 
                                                if(error1(list[i-1].unit) == -1) 
- 
                                                { 
- 
                                                    SetConsoleTextAttribute(hConsole,12); 
- 
                                                    printf("-> Invalid input try again! <-\n"); 
- 
                                                    SetConsoleTextAttribute(hConsole,7); 
- 
                                                } 
- 
  
- 
                                    }while(error1(list[i-1].unit) == -1); 
- 
  
- 
    return list; 
- 
} 
- 
  
- 
  
- 
void printOut(shoppingList*list) 
- 
{ 
- 
    int i=0; 
- 
  
- 
  
- 
    printf("\n%-2d%-10s %-12.2g %-8s",list[i].uniqueid,list[i].name,list[i].amount,list[i].unit); 
- 
  
- 
  
- 
  
- 
} 
- 
  
- 
  
- 
void main(void) 
- 
  
- 
{ 
- 
    int choice=0; 
- 
    int count=0; 
- 
    int i=0; 
- 
    shoppingList*thelist; 
- 
  
- 
    thelist=(shoppingList*)malloc(sizeof(shoppingList)); 
- 
  
- 
  
- 
    do 
- 
    { 
- 
            printf("\n"); 
- 
            printf("Menu\n1 - Add a item to the shopping list\n2 - Print out the shopping list\n3 - End the program\n"); 
- 
            scanf("%d",&choice); 
- 
  
- 
            if(choice==1) 
- 
            { 
- 
                    count++; 
- 
                    thelist = additems(thelist,count); 
- 
  
- 
            } 
- 
  
- 
  
- 
                    else if(choice==2) 
- 
                    { 
- 
  
- 
                            system("cls"); 
- 
                            printf("Shopping list:"); 
- 
  
- 
                            for(i=0;i<count;i++) 
- 
                            { 
- 
  
- 
                                printOut(&thelist[i]); 
- 
  
- 
                            } 
- 
  
- 
                            printf("\n"); 
- 
                    } 
- 
  
- 
  
- 
                            else if(choice == 3) 
- 
                            { 
- 
                                    break; 
- 
  
- 
                            } 
- 
  
- 
  
- 
  
- 
    }while(choice == 1 || 2); 
- 
  
- 
  
- 
    free(thelist); 
- 
    thelist=NULL; 
- 
  
- 
  
- 
}