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:/*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;
- }
Learn More :
Add
- C Program To Add Digits Of Entered Number
- C Program To Add n Numbers
- C program to add first seven terms of the following series using a for loop: (1/1!) + (2/2!) + (3/3!) + .....
- C Program to Adds literal to list of literals
- C Program to Add-Subtract Series 5-10+15
- C Program to Add Series 1+3+5
- C Program that prompts the user to input a string, (whitespaces allowed)
- C Program to : add/sub/multi/divi works
- Adding two polynomial functions C Program Using Structure
- Program to Add Two Polynomials Using Linked List C Program
List
Enter by User
- C Program to Enter an ODD number between 1 and 49.
- C Program to Calculation of One Number Raised to Another
- C Program to Count Vowels, Consonants and Spaces in a string
- C Program to calculate Area, Perimeter of Rectangle; Area, Circumference of Circle.
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- C Program to find result where value and power are user given
- C Program That in a Given Natural Number x Insert Figure c at Position p
- C Program To Find Prime Number And If Number Is Not Prime Then Find Factors
- C Program that prompts the user to input a string, (whitespaces allowed)
- C Program to Input Number by the user between 1-100
- How to Gets User Input For Height in C Programming
- Loudness Program: Gets loudness level from user; chooses a response And prints it using logical and operative statements
- C Program Exchange User Entered Text Between 2 Modules
- C Program to accept n numbers from user & find out the maximum element out of them by using dynamic memory allocation
- C Program to accept string from user & convert all lower case letters into upper case letters & vice versa
- C Program to accept m*n matrix from user and display the elements of given matrix using function
- C Program to accept n numbers from user store these numbers into an array & calculate average of n numbers
- C program to accept n number from user,store these numbers into an array & count the no of occurrences of each number
- C Program to accept n numbers from user store these numbers into an array & reverse an array elements using function
- C Program to accept a string from user, delete all vowels from that string & display the result
- Write a c program to accept a string from user & generate following pattern (e.g, input "abcd")
- C Program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order
- C Program to accept n numbers from user,store these numbers into an array & sort the number of an array
- C Program to accept n numbers from user, store these numbers into an array. Find out Maximum & Minimum number from an Array
print
- PRINT PRIME NUMBERS BETWEEN 1-300 USING BREAK AND CONTINUE IN C
- Write a c program to print Pascal triangle.
- C or C++ Program To Print Prime Number
- C Program print fill zero & mod/div
- C Program to printf & scanf Example
- C Program to Print a String Without Use Semicolon.
- C Program To Input & Print More Than One Words In Single Line
- C Program To Print Alphabets Like {Aa Bb Cc...}
- C Program To Print Alphabets Like {Az By Cx...}
- C Program To Print Lines Of A Paragraph Having More Than 15 Characters
- C Program To Print Prime Numbers Upto The Number You Want
- C Program To Print String In Formatted Output Form
- C Program To Print Sum Of n Digit Number
- C Program To Print Table Horizontally
- C Program To Print Tridiagonal Matrix
- C Program To Print Text Into Uppercase
- Printing ASCII Table with Numbers and corresponding characters
- C Program to print all prime numbers from 1 to 300.
- C Program to print table vertically
- Loudness Program: Gets loudness level from user; chooses a response And prints it using logical and operative statements
- C program that receives 10 float numbers from the console and sort them in non-ascending order, and prints the result
- Function should print the cartesian coordinates of the struct argument
- C Program Read a char & print next char ( file to file )
- Primul meu program in C Program
Shopping
Out