Question

C LANGUAGE. PLEASE INCLUDE COMMENTS :)

In this lab, you will use a do-while loop and a switch to play around with the concept of a menu-driven program, and familiarsrand(unsigned int)time(NULL) Note: In order to use srand), rand, and time), you need to include the proper header statements


>>>>TheCafe V2.c<<<<

#include <stdio.h>

int main()
{
        int fries;                      // A flag denoting whether they want fries or not.
        char bacon;                     // A character for storing their bacon preference.
        double cost = 0.0;      // The total cost of their meal, initialized to start at 0.0

        int choice;                     // A variable new to version 2, choice is an int that will store the
                                                // user's menu choice. It will also serve as our loop control variable.
                                                // It replaces variable order from version 1 of TheCafe.c.

        // A do-while loop is perfect for displaying a menu in a menu-driven program.
        do
        {
                cost = 0.0;             // While we initialized the value of cost in it's declaration above, we
                                                // reinitialize it here as well. This ensures that each iteration of the loop
                                                // is starting from scratch, instead of building on the previous iteration's cost.

                // The following block of statements displays the welcome message
                // and the menu in a nicely formatted manner. We've added a final 
                // option to this version - 4 Exit - to allow for the user to
                // choose to end the program when they are done.
                puts("Welcome to The Cafe @ STC!");
                printf("\n");
                puts("***************************************");
                puts("*  #  Entree                   Price  *");
                puts("*  1  Double Cheeseburger      $5.99  *");
                puts("*  2  Heap'o'Fried Chicken     $6.99  *");
                puts("*  3  Garden Salad             $4.99  *");
                puts("*                                     *");
                puts("*  4  Exit                     -----  *");
                puts("***************************************");
                printf("\n");

                // Now we will prompt the user to make their order, storing the result in choice.
                printf("What would you like to order: ");
                scanf_s("%d", &choice);             
                getchar();                                      // Discards the new line left behind by pressing enter.
                printf("\n\n");                         // This line adds some blank lines to our output.


                // We use a switch to handle the logic of each individual choice. In version 1, we used
                // an if...else if structure instead. The switch replaces and simplifies that structure.
                switch (choice)
                {
                case 1: // Menu option 1 represents a cheeseburger.

                                puts("Cool, Cheeseburger it is.");
                                cost += 5.99;                   // This line adds the cost of a cheeseburger to the total cost.

                                // The cheeseburger has the option of added bacon, so we ask if they would like bacon or not.
                                printf("Would you like bacon on that for $1.00 more (Y/N)? ");
                                bacon = getchar();
                                getchar();                      // This line removes the newline that results from the user pressing enter.


                                // In version 1, we treated all input other than 'Y' or 'N' as no.
                                // Now, we'll use a while loop to make sure we get a correct input choice.
                                // In our loop, we check for incorrect data, which is when bacon is not either 'Y' or 'N'
                                // We use logical AND to chain conditions together, and check for both upper and lowercase.
                                // Only when bacon is NONE of these options will this loop be true.
                                while (bacon != 'Y' && bacon != 'y' && bacon != 'N' && bacon != 'n')
                                {
                                        // We display an error message, and then prompt the user to make their choice again.
                                        puts("\nI'm sorry, I don't understand that... Let's try again.");
                                        printf("Would you like bacon on that for $1.00 more (Y/N)? ");
                                        bacon = getchar();
                                        getchar();              
                                }
                                                                        
                                // Now that their choice, Y or N, is correctly stored in bacon, we can use an if...else statement to proceed.
                                // We use logical OR to allow our program to work with either upper or lowercase values.
                                // When bacon is either of these options, the if statement will be true.
                                if (bacon == 'Y' || bacon == 'y')
                                {
                                        printf("\n");
                                        puts("Great, we'll add bacon.");
                                        cost += 1.00;
                                }
                                else     // We don't use an if here, because if bacon isn't Y or y, it MUST be 'N' or 'n'. Our validation loop ensures that.
                                {
                                        printf("\n");
                                        puts("Okay, no bacon.");
                                }
                                
                                break;  // At the end of the logic for the cheeseburger choice, we use a break statement to exit the switch early.

                case 2: // Menu option 2 represents fried chicken.
                                
                                puts("Cool, Fried Chicken it is.");
                                cost += 6.99;

                                break;

                case 3: // Menu option 3 represents a garden salad
                                        
                                puts("Cool, Garden Salad it is.");
                                cost += 4.99;

                                break;

                case 4: // Menu option 4 represents the choice to exit the program.
                                // If we don't want to do anything when they choose to exit, we can remove this case,
                                // but for now, we'll use it to thank them for using the program.

                                // The continue statement terminates the current iteration of the loop, and starts the next one
                                // Using it here skips straight to check the condition, and skips the code below the switch where
                                // we ask the user if they want fries and display their final total (which we don't want to do
                                // if they chose to exit the program or didn't make a valid menu choice)

                                puts("Ok, exiting program...");
                                continue;

                default:        // Default is the case that will match if the user chooses anything other than 1 - 4.
                                        // We don't need to redisplay the menu here, because the do-while loop will handle that,
                                        // so we just need to display an error message and then restart the loop.
                                
                                puts("I'm sorry, that's not a valid menu option! Please make a new selection.");
                                continue;
                }

                // Once we've calculated the cost of their entree (and any additional options), now we can check if they want fries or not.
                // Because we want to do this for every entree, so we don't put it inside of the switch cases.
                // If you find yourself repeating the same code in every case, we can often move that code outside of the switch to be a bit
                // more efficient with our code reuse.

                printf("\n\n");
                puts("One last question --> Do you want fries with that?!");
                puts("Press 1 for yes, or 0 for no. Its only a $1.50 more.");
                printf("Choice: ");
                scanf_s("%d", &fries);
                getchar();

                // Another input validation while loop will ensure they properly answer the question.
                while (fries != 1 && fries != 0)
                {
                        puts("\nSorry, what was that? Do you want fries or not?");
                        puts("Press 1 for yes, or 0 for no. Its only a $1.50 more.");
                        printf("Choice: ");
                        scanf_s("%d", &fries);
                        getchar();
                }

                if (fries == 1)
                {
                        puts("\nAwesome, fries it is.");
                        cost += 1.50;
                }
                else
                {
                        puts("\nOkay, no fries. Good choice.");
                }

                // Finally, we can display the final cost. Note the use of the modified conversion specifier,
                // to ensure the proper formatting of dollar values.
                printf("\n\n");
                puts("Great, your meal will be right out!");
                printf("Your total today comes out to $%.2f\n", cost);
                puts("Please drive forward, and have a nice day!!");
                printf("\n\n\n\n\n");

        } while (choice != 4);  // While the user did not choose to exit (menu option 4), we will now restart the loop from the beginning

        // And our program ends below, as always.
        return 0;
}
In this lab, you will use a do-while loop and a switch to play around with the concept of a menu-driven program, and familiarize you with the operation of these structures. Additionally, you will get to use the rand() function to experiment with pseudorandom number generation. EXAMPLE PROGRAM The sample program this week revisits The Café STC from Lab 2. We modify the behavior of that program to use a do-while loop to allow for multiple customers, exiting the program only when the user makes the choice to exit. We also use a switch instead of an if...else if for handling the logic of each case and some while loops for input validation as well. Download TheCafe v2.c, add it to a Visual Studio project, and take some time to review the code and familiarize yourself with how it works. When you are ready to proceed, follow the instructions in Your Program below YOUR PROGRAM For this week's lab, you will write a dice roller program, allowing the user to choose from a menu what type of die to roll. As in the sample lab, you should use a do-while loop and a switch to handle the logic of the program, allowing the user to roll multiple dice before choosing to exit the program. You will be working with random numbers in this lab, which are not used in the sample program, but are discussed in the PowerPoint slides for Lecture 8 To begin, before the loop, declare your variables. You should use two variables for this lab - one to store the user's menu choice, and another to store the random number that is generated from the die roll Additionally, still before the loop, you should seed the random number generator. We typically do not want the seed statement to execute more than once. This is done via a call to srand). The exact syntax is discussed in the PowerPoints, and is included below for your convenience. srand (unsigned int)time(NULL)
srand(unsigned int)time(NULL) Note: In order to use srand), rand, and time), you need to include the proper header statements. Inside of the do-while loop, display the menu, giving the user the options seen in the sample screenshots below. You do not need to use my exact design, but should support, at least, all of the options seen in the menu. After displaying the menu, get the user's input, and use a switch to decide what to do Note that the output statements are all the same regardless of what type of die they choose to roll. The only thing that changes is the random number generated by the "roll." Try to minimize the repetition of code in your program by using the switch to determine the value of the random number, but handle the actual output outside of the switch. You can use a continue statement, as seen in the sample lab, to avoid displaying that output when there should not be any (the user makes an invalid choice or chooses to exit the program) When you are done, submit the.c file through the Blackboard assignment submission tool. As usual please name your file according to the usual convention: [First Initial][Last Name]_Lab8.c Challenge For the challenge component this week, modify the program to display not only the value rolled, but also the number of sides on the dice they rolled, as seen in the challenge sample output. Additionally, if the
0 0
Add a comment Improve this question Transcribed image text
Answer #1

saved clang version 7.0.0-3-ubuntuo.18.04.1 (tags/RELEASE_700/final) Welcome to The Cafe STC! 1 #1nclude <stdio.h> 2 #includesaved Moncy you spent till is4.99 Welcome to The Cafe STC! #include <stdio.h> tinc lude <stdlib.h> 3 #include <time.h> 4 intWelcome to The Cafe @ STC! *Entree * 1 Four-sided dice *2 Eight sided dice +3 Ten sided dice Price $4.99 $8.99 $10.99 * * 4 E

#include <stdio.h>

//libraries required for using rand function

#include <stdlib.h>

#include <time.h>

// function to generate Random side for every dice

int get_A_Random_side(int lower, int upper) {

int num = (rand() % (upper - lower + 1)) + lower;

return num;

}

int main()

{

double cost = 0.0; // The total cost of their meal, initialized to start at 0.0

srand( (unsigned int)time(NULL) ); //srand is simply setting rand function

int choice;

int user_roll,random_roll; //help in stroring users guess dice side and system generated dice side

// A do-while loop is perfect for displaying a menu in a menu-driven program.

do

{

// The following block of statements displays the welcome message

// and the menu in a nicely formatted manner. We've added a final

// option to this version - 4 Exit - to allow for the user to

// choose to end the program when they are done.

puts("Welcome to The Cafe @ STC!");

printf("\n");

puts("***************************************");

puts("* # Entree Price *");

puts("* 1 Four-sided dice $4.99 *");

puts("* 2 Eight sided dice $8.99 *");

puts("* 3 Ten sided dice $10.99 *");

puts("* *");

puts("* 4 Exit ----- *");

puts("***************************************");

printf("\n");

// Now we will prompt the user to make their order, storing the result in choice.

printf("What would you like to roll: ");

scanf("%d", &choice);

getchar(); // Discards the new line left behind by pressing enter.

printf("\n\n"); // This line adds some blank lines to our output.


// We use a switch to handle the logic of each individual choice. In version 1, we used

// an if...else if structure instead. The switch replaces and simplifies that structure.

switch (choice)

{

case 1: // Menu option 1 represents a cheeseburger.

puts("Cool, Four-sided dice it is.");

cost += 4.99; // This line adds the cost of a cheeseburger to the total cost.

printf("Guess a side from 1 to 4: ");

scanf("%d", &user_roll);

random_roll=get_A_Random_side(1,4);

printf("Random Roll generated is : %d\n",random_roll);

//priting users game result

if(user_roll==random_roll)

printf("You Win !!!");

else

printf("You looose !,Better luck next time\n");

break; // At the end of the logic for the cheeseburger choice, we use a break statement to exit the switch early.

case 2: // Menu option 2 represents fried chicken.

puts("Cool, Eight-sided dice it is.");

cost += 8.99; // This line adds the cost of a cheeseburger to the total cost.

printf("Guess a side from 1 to 8: ");

scanf("%d", &user_roll);

random_roll=get_A_Random_side(1,8);

printf("Random Roll generated is : %d\n",random_roll);

if(user_roll==random_roll)

printf("You Win !!!");

else

printf("You looose !,Better luck next time\n");

break; // At the end of the logic for the cheeseburger choice, we use a break statement to exit the switch early.

case 3: // Menu option 3 represents a garden salad

puts("Cool, Ten-sided dice it is.");

cost += 10.99; // This line adds the cost of a cheeseburger to the total cost.

printf("Guess a side from 1 to 10: ");

scanf("%d", &user_roll);

random_roll=get_A_Random_side(1,10);

printf("Random Roll generated is : %d\n",random_roll);

if(user_roll==random_roll)

printf("You Win !!!");

else

printf("You looose !,Better luck next time\n");

break; // At the end of the logic for the cheeseburger choice, we use a break statement to exit the switch early.

case 4: // Menu option 4 represents the choice to exit the program.

// If we don't want to do anything when they choose to exit, we can remove this case,

// but for now, we'll use it to thank them for using the program.

// The continue statement terminates the current iteration of the loop, and starts the next one

// Using it here skips straight to check the condition, and skips the code below the switch where

// we ask the user if they want fries and display their final total (which we don't want to do

// if they chose to exit the program or didn't make a valid menu choice)

puts("Ok, exiting program...");

continue;

default: // Default is the case that will match if the user chooses anything other than 1 - 4.

// We don't need to redisplay the menu here, because the do-while loop will handle that,

// so we just need to display an error message and then restart the loop.

puts("I'm sorry, that's not a valid menu option! Please make a new selection.");

continue;

}

printf("\n\n");

// Finally, we can display the final cost. Note the use of the modified conversion specifier,

// to ensure the proper formatting of dollar values.

printf("Money you spent till is : %0.2f \n",cost);

} while (choice != 4); // While the user did not choose to exit (menu option 4), we will now restart the loop from the beginning

// And our program ends below, as always.

return 0;

}

Add a comment
Know the answer?
Add Answer to:
C LANGUAGE. PLEASE INCLUDE COMMENTS :) >>>>TheCafe V2.c<<<< #include ...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The code snippet below is part of the restaurant menu program you previously used in the...

    The code snippet below is part of the restaurant menu program you previously used in the lab. Add a choice for a drink. Add the necessary code to allow the program to calculate the total price of order for the user. Assume the following price list: Hamburger $5 Hotdog $4 Fries $3 Drink $2 The program should allow the user to keep entering order until choosing to exit. At the end the program prints an order summary like this: You...

  • Language: C# In this assignment we are going to convert weight and height. So, the user...

    Language: C# In this assignment we are going to convert weight and height. So, the user will have the ability to convert either weight or height and as many times as they want. There conversions will only be one way. By that I mean that you will only convert Pounds to Kilograms and Feet and Inches to Centimeters. NOT the other direction (i.e. to Pounds). There will be 3 options that do the conversion, one for each type of loop....

  • This is done in c programming and i have the code for the programs that it wants at the bottom i ...

    This is done in c programming and i have the code for the programs that it wants at the bottom i jut dont know how to call the functions Program 2:Tip,Tax,Total int main(void) {    // Constant and Variable Declarations    double costTotal= 0;    double taxTotal = 0;    double totalBill = 0;    double tipPercent = 0;    // *** Your program goes here ***    printf("Enter amount of the bill: $");    scanf("%lf", &costTotal);    printf("\n");    // *** processing ***    taxTotal = 0.07 * costTotal;    totalBill...

  • create case 4 do Method 1:copy item by item and skip the item you want to...

    create case 4 do Method 1:copy item by item and skip the item you want to delete after you are done, delete the old file and rename the new one to the old name. #include int main(void) { int counter; int choice; FILE *fp; char item[100]; while(1) { printf("Welcome to my shopping list\n\n"); printf("Main Menu:\n"); printf("1. Add to list\n"); printf("2. Print List\n"); printf("3. Delete List\n"); printf("4. Remove an item from the List\n"); printf("5. Exit\n\n"); scanf("%i", &choice); switch(choice) { case 1:...

  • CSC 130 Lab Assignment 8 – Program Menu Create a C source code file named lab8.c...

    CSC 130 Lab Assignment 8 – Program Menu Create a C source code file named lab8.c that implements the following features. Implement this program in stages using stepwise refinement to ensure that it will compile and run as you go. This makes it much easier to debug and understand. This program presents the user a menu of operations that it can perform. The choices are listed and a prompt waits for the user to select a choice by entering a...

  • I need a basic program in C to modify my program with the following instructions: Create...

    I need a basic program in C to modify my program with the following instructions: Create a program in C that will: Add an option 4 to your menu for "Play Bingo" -read in a bingo call (e,g, B6, I17, G57, G65) -checks to see if the bingo call read in is valid (i.e., G65 is not valid) -marks all the boards that have the bingo call -checks to see if there is a winner, for our purposes winning means...

  • Please design, write the code and create the requisite submission files in C++ for the following...

    Please design, write the code and create the requisite submission files in C++ for the following problem: A menu-driven program that offers the user 5 options as follows: Menu: 1. Display array 2. Add up all elements in the array and display that number. 3. Add 1 to each element in the array. 4. Swap the values in row one with the values in row three. 5. Exit In main(), declare and initialize a 2-dimensional array that contains 5 x...

  • NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions...

    NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions Work with files Overview This lab will have you store the birthdays for a group of people. This program will store the information with the focus on the date of the month and not on the individual. The point of this program is to be able to see who has a birthday on a given day or to create a table of birthdays, in...

  • NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions...

    NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions Work with files Overview This lab will have you store the birthdays for a group of people. This program will store the information with the focus on the date of the month and not on the individual. The point of this program is to be able to see who has a birthday on a given day or to create a table of birthdays, in...

  • How do i finish this code: #include <stdio.h> int main() { long long decimal, tempDecimal, binary;...

    How do i finish this code: #include <stdio.h> int main() { long long decimal, tempDecimal, binary; int reminder, weight = 1; binary = 0.0; //Input decimal number from user printf("Enter any decimal number: "); scanf("%lld", &decimal); // Since we do not want change the value of decimal in the code // let us use tempDecimal to store the value of decimal tempDecimal = decimal; printf("\n\n"); // Let us use while loop first to implement printf("while loop part:\n"); /**************START FROM HERE...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT