Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c programming Lab 3 Selection Statements In this lab, you will further familiarize yourself with variable declaration and assignment, as well as output and input.

c programming Lab 3 Selection Statements

In this lab, you will further familiarize yourself with variable declaration and assignment, as well as output and input. In addition, you will use if statements to control the behavior of your program in response to user input.

EXAMPLE PROGRAM

The sample program for this simulates a restaurant ordering experience for a single user. To accomplish this, we use a series of if/else if statements.

Download the TheCafe.c source file, create a project in Visual Studio, and add the source file to your project. Take some time to explore it and understand what is going on, and I encourage you to make changes and see what happens. When you feel confident, proceed to your program below.

#include  int main() { int order; // For storing their entree order. int fries; // A flag denoting whether they want fries or not. // Later we'll use a bool for this, but for now, we'll use an int. 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 // The following block of statements displays the welcome message // and the menu in a nicely formatted manner. puts("Welcome to The Cafe @ STC!"); printf(" "); 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("***************************************"); printf(" "); // Now we will prompt the user to enter their choice, and store their input // in the order variable. printf("Enter the number of your Entree choice: "); scanf_s("%d", &order); getchar(); // Discards the new line left behind by pressing enter. printf(" "); // This line adds some blank lines to our output. // Now we will use if/else if statements to determine what the user ordered. if (order == 1) // Cheeseburger { cost += 5.99; // This line adds the item cost to the total cost. // The cheeseburger has the option of added bacon // Here we ask if they would like bacon or not. puts("Cool, Cheeseburger it is."); 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. // Now that their choice, Y or N, is stored in bacon, we can use an if // statement to decide what to do. if (bacon == 'Y') { printf(" "); puts("Great, we'll add bacon."); cost += 1.00; // Bacon costs a dollar extra, so we add a dollar to the total cost of the meal. } else if (bacon == 'N') { printf(" "); puts("Okay, no bacon."); } else { printf(" "); puts("Whoa! Wrong... that wasn't a valid choice."); puts("I'll assume that means you don't want bacon."); } } else if (order == 2) // Heap'o'Fried Chicken { // We add the cost of the menu item to the total cost. puts("Cool, Fried Chicken it is."); cost += 6.99; } else if (order == 3) // Garden Salad { puts("Cool, Garden Salad it is."); cost += 4.99; } else { puts("That wasn't a valid option. Guess you are going hungry."); } // Once we've calculated the cost of their entree (and any additional options), // now we can check if they want fries or not. printf(" "); 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(); if (fries == 1) { puts(" Awesome, fries it is."); cost += 1.50; } else { puts(" Okay, no fries. Good choice."); } // Finally, we can display the final cost. Note the use of the modified conversion specifier, // as in the last lab, to ensure the proper formatting of dollar values. printf(" "); puts("Great, your meal will be right out!"); printf("Your total today comes out to $%.2f ", cost); puts("Please drive forward, and have a nice day!!"); printf(" Press any key to continue..."); getchar(); // And our program ends below, as always. return 0; }

YOUR PROGRAM

For your lab, you will write a small utility to determine whether a store customer has exceeded the credit limit on their account. To accomplish this, your program will accept as input five values, listed below:

  1. Account Number (an integer value)
  2. The customers balance at the beginning of the month
  3. The total of all items charged by the customer this month
  4. The total of all credits applied to this customers account
  5. Allowed credit limit

You will need to declare a variable to store each of the above values. The account number is an integer value, but I will leave it to you to determine what data type should be used for the other four (hint: they are all currency amounts, so should support decimal points).

Once you have prompted for and stored the users input into the associated variables, you will need to calculate the customers new balance. This is achieved through the formula (new balance = original balance + charges credits). You may want to declare a new variable to store this total.

Finally, using an if statement, determine if the users new balance has exceeded their credit limit. If it has (and only if it has), print out the users account number, credit limit, and new balance, so that the information may be reviewed. If the user has not exceeded their limit, print out their account number and a message informing the user that they are in good standing. For an idea of how you might format this output, see the sample screenshots below.

When you are done, name the .c file according to the following convention: [First Initial][Last Name]_Lab3.cpp. Submit the completed .c file through the Blackboard assignment submission tool (if you have any difficulties submitting, please contact the instructor).

Lab Output Screenshot Example:

image text in transcribed

CAWINDOWS)system321cmd.exe ustomer Credit Utility Enter the user's account number: 10025 Enter the user's previous balance: 375.5 nter the user's total charges for the last month: 149.99 Enter the user's total credits for the last month: e.ee Enter the user's credit 1imit: 5ee.ee Customer has exceeded their credit limit Current balance and credit limit for Account #lee25 Credit Limit: 500.ee lew Balance: 525.49 Press any key to continue . .. - CAWINDOWS1system321cmd.exe ustomer Credit Utility Enter the user's account number: 10025 Enter the user's previous balance: 375.se Enter the user's total charges for the last month: 149.99 Enter the user's total credits for the last month: 100.e0 Enter the user's credit limit: 500.00 ustomer, Account #10025, is in good standing. ress any key to continue

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions