Question

I need help with using the infile and outfile operations in C++... Here's my assignment requirements:...

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 <iostream>

#include <string>

#include <fstream>

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;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Program:

//Header files
#include <iostream>
#include <string>
#include <fstream>
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");

//Checking input file
if(infile.fail())
{
cout << endl << "Error!!! Opening input file.... " << endl;
return 0;
}

//Reading data from input file
infile >> stockName >> sharePurchprice >> shareSellprice >> sharesSold;

//Calculate Service Charge
buyService = serviceCharge * (sharesSold*sharePurchprice);
sellService = serviceCharge * (sharesSold*shareSellprice);
totalService = buyService + sellService;

//Calculate amount invested amtInvest
amtInvest = (sharesSold*sharePurchprice) + buyService;

//Writing results to file
outfile << "Name of the stock: " << stockName << endl;
outfile << "Amount Invested: " << amtInvest << endl;

//Calculate amount after stock sell and display to user
amtRet = (sharesSold*shareSellprice) - sellService;

outfile << "Total amount received after sell: " << amtRet << endl;

//Calculate amount invested and total service charge and display to user
outfile << "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)
outfile << "Your total gain is : " << gain_or_loss << endl;
else
outfile << "You total loss is : " << gain_or_loss << endl;

//Closing files
outfile.close();
infile.close();
//end
return 0;
}
_____________________________________________________________________________________________________

Sample Run:

CATCStockExchangebinDebuglStockExchange.exe Process returned 0 (0x0) execution time : 0.208 s Press any key to continue. stoc

Add a comment
Know the answer?
Add Answer to:
I need help with using the infile and outfile operations in C++... Here's my assignment requirements:...
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
  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • Microsoft Visual Studios 2017 Write a C++ program that computes a student’s grade for an assignment...

    Microsoft Visual Studios 2017 Write a C++ program that computes a student’s grade for an assignment as a percentage given the student’s score and total points. The final score must be rounded up to the nearest whole value using the ceil function in the <cmath> header file. You must also display the floating-point result up to 5 decimal places. The input to the program must come from a file containing a single line with the score and total separated by...

  • I need help with my coding for C++! The goal of this is to calculate the...

    I need help with my coding for C++! The goal of this is to calculate the total cost of a product by combining the products price and the amount of tax, then display it to the user. The numbers we have to test out is 100 for the price and 8.25 for the percentage. When I test is out, the formula works but it isn't rounding the 108.25 to 108.3, which is the total price we are supposed to display...

  • I need help modifying this code please ASAP using C++ Here is what I missed on this code below Here are the instruction...

    I need help modifying this code please ASAP using C++ Here is what I missed on this code below Here are the instructions Here is the code Produce correct70 pts O pts Full Marks No Marks results and statisfy requirements view longer Comments 1. Your display amount is not readable 2. I withdraw more than my balance, but I didn't see any error message description Documentations10 pts 0 pts Full Marks No Marks : comment i code and block comment...

  • c++ When the students started selling cookies, they were told that the students who sell the...

    c++ When the students started selling cookies, they were told that the students who sell the maximum number of bóxes will have 10% of the money they generate donated to their favorite charitable organization. So, in addition to the output your program generated in Exercise 18, your program should output: 1. The names of all the students selling the maximum number of boxes 2. The amount that will be donated to their favorite charitable organization. Ch5_Ex19Data.txt 1 3.50 2 Sara...

  • I need help with this assignment, can someone HELP ? This is the assignment: Online shopping...

    I need help with this assignment, can someone HELP ? This is the assignment: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt) Public member functions SetDescription() mutator & GetDescription() accessor (2 pts) PrintItemCost() - Outputs the item name followed by...

  • I need to get this two last parts of my project done by tonight. If you...

    I need to get this two last parts of my project done by tonight. If you see something wrong with the current code feel free to fix it. Thank you! Project 3 Description In this project, use project 1 and 2 as a starting point and complete the following tasks. Create a C++ project for a daycare. In this project, create a class called child which is defined as follows: private members: string First name string Last name integer Child...

  • I need to make a few changes to this C++ program,first of all it should read...

    I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...

  • IP requirements: Load an exam Take an exam Show exam results Quit Choice 1: No functionality...

    IP requirements: Load an exam Take an exam Show exam results Quit Choice 1: No functionality change. Load the exam based upon the user's prompt for an exam file. Choice 2: The program should display a single question at a time and prompt the user for an answer. Based upon the answer, it should track the score based upon a successful answer. Once a user answers the question, it should also display the correct answer with an appropriate message (e.g.,...

  • im writing a c++ code and getting stuck on two parts. The first part, when I...

    im writing a c++ code and getting stuck on two parts. The first part, when I go to write the customer order out to the file, it writes everything but the price out right. I'll get a weird number like 5.95828e-039 or nan. When I printout to the console it works fine so not sure what's wrong. Second After I write the order out to the file, I then have to go in and read the file and calculate the...

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