C program that lets the user choose if he wants to add a item to the shopping list or print it out

How to write a C program that lets the user choose if he wants to add a item to the shopping list or print it out in C Programming Language ?

Solution:

  1. /*C program that lets the user choose if he wants to add a item to the shopping list or print it out.*/

  2.  
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <string.h>
  8. #include <windows.h>
  9.  
  10. typedef struct{
  11.  
  12.     char name[20];
  13.     char unit[20];          // SKAPAR EN STRUCT OCH DÖPER DEN TILL SHOPPINGLIST MED HJÄLP AV TYPEDEF FÖR ATT ANVÄNDA DEN SMIDIGARE
  14.     float amount;
  15.     int uniqueid;
  16.  
  17.  
  18. }shoppingList;
  19.  
  20.  
  21. int error1(char*string)
  22. {
  23.     int i=0;                //SKAPAR FUNTIONEN ERROR 1 SOM TAR EN STRÄNG SOM ARGUMENT
  24.     int length=0;
  25.     length=strlen(string);
  26.  
  27.     for(i=0;i<length;i++)
  28.     {                                       //ANVÄNDER MIG AV ISALPHA OCH TITTAR OM DET INTE ÄR BOKSTÄVER A-Z RETURNAR VI -1 SOM ÄR VÅRAN FELHANTERING
  29.             if(!isalpha(string[i]))
  30.             {
  31.                 return -1;
  32.             }
  33.     }
  34.  
  35.     return 0;                    // ANNARS RETURNEAR VI 0 OCH DÅ STÄMMER ALLT
  36. }
  37.  
  38. int error2(char*str)
  39. {
  40.     int i;
  41.     int length;                 // SKAPAR FUNKTIONEN ERROR 2 SOM OCKSÅ TAR EN STRÄNG SOM ARGUMENT
  42.     int count = 0;
  43.     length=strlen(str);
  44.  
  45.     for(i=0;i<length;i++)
  46.     {
  47.         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
  48.             {
  49.                 count++;
  50.  
  51.                 if (str[0] == '.' || str[length-1] == '.' || count > 1)
  52.                 {
  53.                                                                                     /* TITTAR OM FÖRSTA ELLER SISTA TECKNET I STRÄNGEN ÄR EN PUNKT ELLER OM DET FINNS MER ÄN EN PUNKT */
  54.                 {                                                                   /*  OCH DÅ RETURNERAR VI -1 SOM ÄR VÅRAN FELHANTERING */
  55.                     return -1;
  56.                 }
  57.             }
  58.             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
  59.             {
  60.                 return -1;
  61.             }
  62.     }
  63.  
  64.     return 0;                           // ANNARS RETUNERAR VI O DÅ ALLT STÄMMER
  65.  
  66. }
  67.  
  68.  
  69. shoppingList *additems(shoppingList*list,int count)
  70. {
  71.     char temparray[20];                             // SKAPAR FUNKTIONEN ADDITEMS OCH DEN TAR EN STRUCTPEKARE OCH EN INT SOM ARGUMENT
  72.     int i;
  73.     HANDLE hConsole;
  74.     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  75.  
  76.     list = realloc(list, count*sizeof(shoppingList));       // SÄTTER STRUCT PEKAREN TILL ATT VARA LIKA MED REALLOC FÖR ATT ALLOKERA
  77.  
  78.     i=count;
  79.  
  80.     list[i-1].uniqueid=i;
  81.  
  82.         do
  83.         {
  84.  
  85.                 printf("\nName on item %d:",count);
  86.                 fflush(stdin);
  87.                 gets(list[i-1].name);
  88.  
  89.                 if(error1(list[i-1].name) == -1)
  90.                 {
  91.                     SetConsoleTextAttribute(hConsole,12);
  92.                     printf("-> Invalid input, try again! <-\n");
  93.                     SetConsoleTextAttribute(hConsole,7);
  94.  
  95.                 }
  96.  
  97.         }while(error1(list[i-1].name));
  98.  
  99.  
  100.                     do
  101.                     {
  102.  
  103.  
  104.                                 printf("Amount:");
  105.                                 fflush(stdin);
  106.                                 gets(temparray);
  107.  
  108.                                 if(error2(temparray) == -1)
  109.                                 {
  110.                                     SetConsoleTextAttribute(hConsole,12);
  111.                                     printf("-> Invalid input, try again! <-\n");
  112.                                     SetConsoleTextAttribute(hConsole,7);
  113.                                 }
  114.  
  115.                     }while(error2(temparray) == -1);
  116.  
  117.                     list[i-1].amount=atof(temparray);
  118.  
  119.  
  120.                                     do
  121.                                     {
  122.                                                 printf("Unit:");
  123.                                                 fflush(stdin);
  124.                                                 gets(list[i-1].unit);
  125.  
  126.                                                 if(error1(list[i-1].unit) == -1)
  127.                                                 {
  128.                                                     SetConsoleTextAttribute(hConsole,12);
  129.                                                     printf("-> Invalid input try again! <-\n");
  130.                                                     SetConsoleTextAttribute(hConsole,7);
  131.                                                 }
  132.  
  133.                                     }while(error1(list[i-1].unit) == -1);
  134.  
  135.     return list;
  136. }
  137.  
  138.  
  139. void printOut(shoppingList*list)
  140. {
  141.     int i=0;
  142.  
  143.  
  144.     printf("\n%-2d%-10s %-12.2g %-8s",list[i].uniqueid,list[i].name,list[i].amount,list[i].unit);
  145.  
  146.  
  147.  
  148. }
  149.  
  150.  
  151. void main(void)
  152.  
  153. {
  154.     int choice=0;
  155.     int count=0;
  156.     int i=0;
  157.     shoppingList*thelist;
  158.  
  159.     thelist=(shoppingList*)malloc(sizeof(shoppingList));
  160.  
  161.  
  162.     do
  163.     {
  164.             printf("\n");
  165.             printf("Menu\n1 - Add a item to the shopping list\n2 - Print out the shopping list\n3 - End the program\n");
  166.             scanf("%d",&choice);
  167.  
  168.             if(choice==1)
  169.             {
  170.                     count++;
  171.                     thelist = additems(thelist,count);
  172.  
  173.             }
  174.  
  175.  
  176.                     else if(choice==2)
  177.                     {
  178.  
  179.                             system("cls");
  180.                             printf("Shopping list:");
  181.  
  182.                             for(i=0;i<count;i++)
  183.                             {
  184.  
  185.                                 printOut(&thelist[i]);
  186.  
  187.                             }
  188.  
  189.                             printf("\n");
  190.                     }
  191.  
  192.  
  193.                             else if(choice == 3)
  194.                             {
  195.                                     break;
  196.  
  197.                             }
  198.  
  199.  
  200.  
  201.     }while(choice == 1 || 2);
  202.  
  203.  
  204.     free(thelist);
  205.     thelist=NULL;
  206.  
  207.  
  208. }


Learn More :