Question

In: Computer Science

Description The purpose of this challenge is to use the IF statement to control program flow...

Description

The purpose of this challenge is to use the IF statement to control program flow and to use basic arithmetic expressions. This challenge simulates a rudimentary stock chart.

Requirements

Take a glance at the Sample Interaction below to get an idea of how the code runs
Declare a double variable called previous_price
Declare a double variable called current_price
Declare a string variable called symbol
Declare a string variable called company
Ask the user to enter a value for symbol
Ask the user to enter a value for company
Ask the user to enter a value for previous_price
Ask the user to enter a value for current_price
If the price of the stock has increased, show a message “It’s a bull market”
If the price of the stock has decreased, show a message “It’s a bear market”
if the price of the stock didn’t change, show a message “It’s a flat market”
Also, as part of the message for steps 10 and 11, indicate the percentage change. (see below)
Hints

Include the <iomanip> header file. This will allow you to use various options to format your output
#include <iomanip>
It’s good practice to initialize variables to default values that make sense (for example, set fee to 0.00)
Include the code below before any other cout lines. The setprecision(x) will display your decimal output with x decimal places.
cout << fixed << setprecision(2);
Sample Interaction / Output

-- Welcome to CSUB Brokerage --
What is the stock symbol? GOOG
What is the company name? Google
What was the previous stock price? 850
What is the current price? 1000

It's a bull market! Google (GOOG) went up 17.65%
RUN IT AGAIN:

-- Welcome to CSUB Brokerage --
What is the stock symbol? GOOG
What is the company name? Google
What was the previous stock price? 850
What is the current price? 425

It's a bear market! Google (GOOG) went down 50.00%.
RUN IT AGAIN:

-- Welcome to CSUB Brokerage --
What is the stock symbol? GOOG
What is the company name? Google
What was the previous stock price? 850
What is the current price? 850

It's a flat market! Google (GOOG) stock price didn't change.

Solutions

Expert Solution

  • The first part of the program declares the required variables and initializes them to their default values i.e.
    • string symbol
    • string company
    • double previous price
    • double current price
  • After that we take the user input for all these values.
  • Finally we reach the if-else conditions which are executed as follows:

if(condition A is true){
//run this}
else if(condition B is true){
//run this}
else if(condition C is true){
//run this}

First it checks if Condition A is true. If true it runs the code inside the curly braces and the program ends.

Only if 'A' is false it checks for Condition B to be true. If 'B' is true the code inside the curly braces of that condition is executed and the program ends.

If 'B' is also false it checks for Condition C to be true. If true it executes the code inside the curly braces and the program ends.

Note: In the code given below some of the output strings contain single quotes. You might have to type the single quotes again through your keyboard if they're not being displayed properly in the console window.

Please find attached the commented code below:

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
  
   /* declare the variables required in the program and initialize them to default values*/
   double previous_price = 0;
   double current_price = 0;
   double percentage = 0;
   string symbol;
   string company;
  
   /* Take the input from the user for 'symbol', 'company', 'previous_price', 'current_price'*/
   cout<<"--Welcome to CSUB Brokerage--"<<endl;
   cout<<"What is the stock symbol? ";
   cin>>symbol;
  
   cout<<"What is the company name? ";
   cin>>company;
  
   cout<<"What was the previous stock price? ";
   cin>>previous_price;
  
   cout<<"What is the current price? ";
   cin>>current_price;
  
   /*if the current price is greater then the previous price run this block of code*/
   if(current_price > previous_price){
       percentage = (current_price - previous_price)/previous_price*100;
       cout<<"It's a bull market! ";
       cout<<company << " (" << symbol << ") went up ";
       cout<<fixed<<setprecision(2)<<percentage<<"%";
   }
  
   /*if the current price is less than the previous price run this block of code*/
   else if(current_price < previous_price){
       percentage = (previous_price - current_price)/previous_price*100;
       cout<<"It's a bear market! ";
       cout<<company << " (" << symbol << ") went down ";
       cout<<fixed<<setprecision(2)<<percentage<<"%";
   }
  
   /* if current price and previous price are same, run this*/
   /* we can also use just the else condition for this part */
   else if(current_price == previous_price){
       cout<<"It's a flat market! ";
       cout<<company << " (" << symbol << ") stock price didn't change. ";
   }
  
   return 0;
}


Related Solutions

Description( IN C++)!! The purpose of this challenge is to implement a stack using a Linked...
Description( IN C++)!! The purpose of this challenge is to implement a stack using a Linked List as a backing data structure Requirements Write the following structs struct Location { string name; string address; }; struct VisitNode { Location loc; VisitNode * next; }; Create a class called Stack. In this class, create a private variable VisitNode * head. This will keep track of the location of the head node. Add the following private function. This function will be used...
Description( IN C++) NO TAIL POINTERS!! The purpose of this challenge is to implement a queue...
Description( IN C++) NO TAIL POINTERS!! The purpose of this challenge is to implement a queue using a linked list as a backing data structure Requirements Write the following struct struct JobNode { string name; JobNode * next; }; Create a class called Queue. In this class, create a private variable JobNode * head. This will keep track of the location of the head node. Add the following private function. This function will be used to dynamically create new nodes...
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy...
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy node. This challenge simulates an operating system’s window manager. Requirements Write the following struct struct Window { string appname; Window *next; Window *prev; }; Create a class called WindowManager. In this class, create a private variable Window * head. This will keep track of the location of the head node. Create private variables Window * current, * dummy. current will keep track of the...
Description: The purpose of the program is to create a Java ticket purchasing program that allows...
Description: The purpose of the program is to create a Java ticket purchasing program that allows for users to log in, purchase tickets, keep records of tickets purchased, and keep information about the user. Program Requirements: The following are the program requirements: Must be fully functioning including registration, log-in, view events, and purchase tickets Tickets should be purchased using “points”, no information should be provided via the user for payment method. Default each user to an allotted number of points...
Challenge: Number Stats 2 Description: Extend the program you wrote for Number Stats to determine the...
Challenge: Number Stats 2 Description: Extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. Purpose: The purpose of this challenge is to provide experience working with numerical data in a file and generating summary information including the determination of median and mode. It also provides experience adding new functionality to an existing program. Requirements: Extend the program you wrote for Number Stats to determine the median and mode...
Explain the purpose and use of the Statement of profit or loss, Statement of changes in...
Explain the purpose and use of the Statement of profit or loss, Statement of changes in equity, Statement of financial position (also known as the Balance Sheet) and of the Statement of cash flows. Explain why the Statement of cash flows differs from the Statement of profit or loss (125 words).
Pick the best control plan for the scenario or description provided. You may use a control...
Pick the best control plan for the scenario or description provided. You may use a control plan once, more than once, or not at all. A customer service representative was terminated, to "get even" during the notice period he created several fake sales orders with delivery addresses for his family members. May include financial background check performed on potential candidates for positions that perform cash deposits and execute bank wire transactions and other privileged activities. A snow storms knocks out...
What is the primary purpose of the statement of cash flow? elect one: a. To provide...
What is the primary purpose of the statement of cash flow? elect one: a. To provide information about the cash flow from operations for a period b. To provide information about firm profitability c. To provide pro forma information that helps users predict future cash flows d. To provide information about where a company's cash comes from and where it goes 2.Partial financial results are presented below for McDonald's Restaurant:    Operating profit     $435,000 Cash flow from:   Operations       115,000   Investing      125,600   Financing    (54,000) What...
Short description of your assigned challenge Describe the innovation you researched to address the challenge The...
Short description of your assigned challenge Describe the innovation you researched to address the challenge The result (what did it prove?) How/Why it impacts or supports the challenge the challenge is "make Solar energy economical"
c++ class Topics Branches if statement if/else statement Description Write a program that will determine whether...
c++ class Topics Branches if statement if/else statement Description Write a program that will determine whether the user passed or failed a test. The program will ask the user to input the following: Maximum Test Score - the maximum total scores in the test. Percent Pass Score - the minimum percent scores required to pass the test. User Test Score - the actual scores obtained by the user in the test. From the above input values, the program will compute...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT