Coffee Order Structure Assignment
Write a program that uses a structure for a coffee order. A coffee order consists of a double price, a string for flavor, and characters for cream (Y/N), sugar (Y/N), and coffee size (S/M/L).
Your main() function will call an order function that will ask the user for the flavor, size and whether or not they want cream or sugar.
Your main() function will then create a coffee order structure and assign all of the values for the coffee order.
Your main() function will then call a display order function that will display the coffee order and the price. A small is $3.00, medium is $4.50, and large is $6.00.
Your order coffee function will contain an array of strings for the available flavors:
char *selectedFlavors[5] = {"House", "Iced", "Vanilla", "Hazelnut", "Mocha"};
Your order coffee function will then ask the user what size of coffee they would like (S/M/L) and validate the data allowing them to continue until they answer with an S, M, or L.
Using your array of strings, display the various available flavors and use an fgets() to get the flavor from the user. You do not need to validate this!
Lastly, ask the user if they would like cream (Y/N) and ask if they would like sugar (Y/N). These do not need to be validated either!
Your display order function must display like one of the following ways based on the order.
In order to make the display order function display the flavor correctly (since you are using the fgets() ), you will have to call this function in your program to remove the enter key from your string:
#include <stdio.h>
#include<stdbool.h>
struct coffee_order
{
double price; // variable for price
char flavor[10]; // string variable to store flavor of coffee
char cream; // to store user preference for cream
char sugar; // to store user preference for sugar
char coffee_size; // to store user choice for size of coffee
};
void order(double *p,char *f,char *c,char *s,char *ch) // function to take udser input
{
/* we are using pointers so out choices stick even when our function has completed */
char *selectedFlavors[5] = {"House", "Iced", "Vanilla", "Hazelnut", "Mocha"}; // to store flavors available
// below do-while will continue to loop until user enters S,M or L
while(true)
{
printf("Enter S for small,M for medium and L for large:");
scanf(" %c",ch);
if(*ch=='S' || *ch=='M' || *ch=='L')
{
break; // break the loop if user inputs correct choice
}
}
if(*ch=='S')
{
*p=3.0;
}
else if(*ch=='M')
{
*p=4.5;
}
else
{
*p=6.0;
}
// below for loop will display various flavors
printf("Choose a flavor and type its name:");
for(int i=0;i<5;i++)
{
printf(" %s",selectedFlavors[i]); // to display different flavors
}
printf(":");
scanf(" "); // to ignore newline
fgets(f, 10, stdin); // to take input from the user and stores it in f
printf("\nWould you like cream(Y/N): "); // to output to the screen
scanf(" %c",c); // to take user input
printf("\nWould you like sugar(Y/N): "); // to output to the screen
scanf(" %c",s); // to take user input
}
void display(struct coffee_order c)
{
// displaying all details of coffee order
printf("Your coffee order is:\n");
printf("The price for coffee order is $%lf\n",c.price);
printf("The flavor for coffee order is %s\n",c.flavor);
printf("The cream for coffee order is %c\n",c.cream);
printf("The sugar for coffee order is %c\n",c.sugar);
printf("The coffee size for coffee order is %c\n",c.coffee_size);
}
int main()
{
double p; // variable for price
char f[10]; // variable to store flavor of coffee
char c; // to store user preference for cream
char s; // to store user preference for sugar
char ch; // to store user choice for size of coffee
// calling the order function
order(&p,f,&c,&s,&ch);
struct coffee_order userOrder; // declaring variable of type coffee_order
// populating coffee_order
userOrder.price=p;
for(int i=0;i<10;i++)
{
userOrder.flavor[i]=f[i];
}
userOrder.cream=c;
userOrder.sugar=s;
userOrder.coffee_size=ch;
// calling the display function
display(userOrder);
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.