Question
I need help with using the infile and outfile operations in C++... Here's my assignment requirements: Assignment: Cindy uses the services of a brokerage firm
I need help with using the infile and outfile operations in C++...
Here's my assignment requirements:
Assignment: Cindy uses the services of a brokerage firm to buy and sell stocks. The firm charges 1.5% service charges on the total amount for each transaction, buy or sell. When Cindy sells stocks, she would like to know if she gained or lost on a particular investment. Write a program that allows Cindy to read input from a file called Stock.txt: 1. The name of the stock.
2. The purchase price of each stock
3. The selling price of each stock
4. The number of stock sold
5. Assumption: Number of stocks bought and sold are equal.
The program writes the following output to a file called StockOutput.txt: 1. The name of the stock
2. The amount invested
3. The amount received after selling the stock.
4. The total service charges
5. Amount gained or lost
Here's the code I've got so far ... the math works but I'm stuck with the in/out files and formatting.
//Header files
#include
#include
#include
using namespace std;
//define constant variables
const double serviceCharge = 0.015;
//Begin main function
int main()
{
//declare variables
string stockName;
int sharesSold;
double sharePurchprice;
double shareSellprice;
double amtInvest;
double amtRet;
double buyService;
double sellService;
double totalService;
double gain_or_loss;
//declare filestream variables
ifstream infile;
ofstream outfile;
//Open the files Stock.txt and stockOutput.txt
infile.open("Stock.txt");
outfile.open("stockOutput.txt");
infile >>stockName >>amtInvest >>amtRet >>totalService >>gain_or_loss;
//Prompt user for name of stock
cout << "Please enter name of stock:";
getline(cin, stockName);
//Prompt for user input of shares sold sharesSold
cout << "Please enter the number of shares sold: ";
//read input from user sharesSold
cin >> sharesSold;
//Prompt for user input of purchase price of shares sharePurchprice
cout << "Please enter the price of share purchase price: ";
//read input from user SharesPurch
cin >> sharePurchprice;
//Prompt for user input of selling price of shares shareSellprice
cout << "Please enter the price of share selling price: ";
//read input from user shareSellprice
cin >> shareSellprice;
//Calculate Service Charge
buyService = serviceCharge * (sharesSold*sharePurchprice);
sellService = serviceCharge * (sharesSold*shareSellprice);
totalService = buyService + sellService;
//Calculate amount invested amtInvest
amtInvest = (sharesSold*sharePurchprice) + buyService;
//Calculate amount invested and total service charge and display to user
cout << "Amount Invested: " << amtInvest << endl;
cout << "Total Service Charge: " << totalService << endl;
//Calculate amount gain and/or lost and display to user
gain_or_loss = (sharesSold*(shareSellprice - sharePurchprice)) - totalService;
//gain or loss if / else loop
if (gain_or_loss > 0)
cout << "Your total gain is : " << gain_or_loss << endl;
else
cout << "You total loss is : " << gain_or_loss << endl;
//Calculate amount after stock sell and display to user
amtRet = (sharesSold*shareSellprice) - sellService;
cout << "Total amount received after sell: " << amtRet << endl;
//end
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started