Question

Coffee Order Structure Assignment Write a program that uses a structure for a coffee order. A...

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:

Homework Answers

Answer #1

#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;
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Ice Cream Program Assignment Write a program in c# that uses a function to ask the...
Ice Cream Program Assignment Write a program in c# that uses a function to ask the user to choose an ice cream flavor from a menu (see output below.) You must validate the users input for the flavor of ice cream accounting for both upper and lower-case letters. You must give them an appropriate error message and allow them to try again.   Once you have a valid flavor, your function will return the flavor back to the main() function.   ...
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
Write a C program Design a program that uses an array to store 10 randomly generated...
Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
Instructions Write a Java code Your goal is to take N integer inputs from the user...
Instructions Write a Java code Your goal is to take N integer inputs from the user -- N's value will be given by the user as well. You can assume the user provides a valid value for N, i.e., >0. Store the input integers in an array of size N in the order they are provided. These tasks should be done in the main() method. Create a new method called checkArray() that will take the previously created array as input...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
Please write it in c# program please and if possible no arrays only loops Loop Introduction...
Please write it in c# program please and if possible no arrays only loops Loop Introduction Assignment Using the conditions below, write one program that calculates a person’s BMI. Your main() function will call functions 1, 2, and 3. Your program will contain three functions: Function #1: Will ask the user for their weight in pounds and their height in inches.   Your function will convert the weight and height into Body Mass Index (BMI). The formula for converting weight into...
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
COMPLETE IN C++ Declare and initialize a global constant named SIZE with the value 50. Write...
COMPLETE IN C++ Declare and initialize a global constant named SIZE with the value 50. Write a void function called count that accepts two parameters-- a C-string called str representing a C-string passed to the function and an array of integers called alphabets to store the count of the letters of the C-string. Note that the alphabets array stores 26 counters – one for each letter of the alphabet. Use the same counter for lowercase and uppercase characters. The first...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT