Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Programmer: Daniel B. Papp
- //CS150 SP18
- //TA:
- //DATE: 03/18/2018
- ///The following program takes input from an investment portfolio and displays
- ///the content to the output device (monitor/outfile). It also calculates the
- ///total invested, Possible annual holdings for maximum loss and gain and whether
- ///the portfolio is a sound investment or not. Outputting the information to the output
- ///device as well.
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <math.h>
- using namespace std;
- int main()
- {
- ///PART A Declare and Initialize____________________________________________________________________________
- ///1. Declare and INITAILIZE the variables
- string Symbol;
- double Invested_amount = 0;
- float currentP, maxR = 0, maxL = 0, totalInv = 0, maxRov = 0, maxLov = 0, soundness, maxRusd = 0, maxLusd = 0;
- int resp;
- bool is_sound;
- bool choice = true;
- string inFile_name;
- string outFile_name;
- ///2.Declare the input file
- ifstream inFile;
- ///3.Declare the output file
- ofstream outFile;
- ///4.OUTER WHILE LOOP GOES HERE
- while(choice == true){
- ///5. Set output fixed, showpoint, setprecision of two for all decimals
- outFile<<setprecision(2)<<showpoint<<fixed;
- ///6. Prompt user for the input file(Ex. folio1.txt):
- cout<< "Enter input file name: ";
- cin>>inFile_name;
- ///a. Open the input file using the users input, remember to convert string to a c string (ex. filename.c_str())
- inFile.open(inFile_name.c_str());
- if(!inFile){
- ///b. Include error handling: if file cannot be found -> exit program
- cout<<"File could not be found. Exiting program."<<endl;
- return 0;
- }
- ///6. Prompt user for the output file
- ///a. Use a DO-WHILE to avoid overwriting the input file
- do{
- cout<<"Under what file name would you like to save the output data (ex: folio1out.txt): ";
- cout<<"\nWARNING: name cannot be the same as the input file."<<endl;
- cin>>outFile_name;
- }
- while(outFile_name == inFile_name);
- ///b. Open the output file using the users input, remember to convert to a c string (ex. filename.c-str())
- outFile.open(outFile_name.c_str());
- ///PART B Input and computations____________________________________________________________________________
- ///7. Output the table heading to the monitor and output file here (EX. SYMBOL CURRENT PRICE(USD) INVESTED AMOUNT(USD) MAX Return(%) Max Loss(%))
- ///Remember to use the iomanipulator class to format the output
- cout << setfill(' ') << left << setw(7) << "Symbol" << setw(20) << "Current Price (USD)"
- <<setw(22) << "Invested Amount (USD)" << setw(15) << "Max Return (%)" << setw(15) << "Max Loss (%)";
- outFile << setfill(' ') << left << setw(7) << "Symbol" << setw(20) << "Current Price (USD)"
- <<setw(22) << "Invested Amount (USD)" << setw(15) << "Max Return (%)" << setw(15) << "Max Loss (%)";
- cout<<setprecision(2)<<fixed<<showpoint;
- ///8. Utilize a WHILE loop to open the input file and extract the information sequentially until end of file.
- while(!inFile.eof())
- {
- ///a. Get (read) file input
- inFile>>Symbol>>currentP>>Invested_amount>>maxR>>maxL;
- ///b. Compute Max_return, Max_loss, Total_invested using a summation that continually compounds on the previous input
- totalInv+= Invested_amount;
- maxRusd = maxRusd + ((maxR/100)*Invested_amount);
- maxLusd = maxLusd + ((maxL/100)*Invested_amount);
- ///c. Output all information for each stock/asset currently in the loop (EX. QCV 30 300 100 100)
- ///cout<<Symbol<<currentP<<Invested_amount<<maxR<<maxL<<endl;
- cout<<"\n" << setfill(' ') << left << setw(7) << Symbol << setw(20) << currentP
- <<setw(22) << Invested_amount << setw(15) << maxR << setw(15) << maxL;
- outFile<<"\n" << setfill(' ') << left << setw(7) << Symbol << setw(20) << currentP
- <<setw(22) << Invested_amount << setw(15) << maxR << setw(15) << maxL;
- }///End of while loop
- ///9. Compute the aggregate values utilizing Max Return, Max Loss, and Total invested
- maxRov = maxRusd/totalInv;
- maxLov = maxLusd/totalInv;
- ///10. Check whether the investment portfolio is sound using the equation IsSound = 0.40 * Investment after Max Return percent - 0.60 * Investment after Max Loss percent
- soundness = (0.40*maxRov)-(0.60*maxLov);
- if (soundness > 1){
- is_sound = true;
- }
- else{
- is_sound = false;
- }
- ///Part C Output_____________________________________________________________________________________
- cout << endl << endl;
- outFile << endl << endl;
- ///11. Output results to the monitor, utilize the output manipulators
- cout << left << setw(10) << "Annual Analysis: " << endl;
- cout << left << setw(10) << "Total investment (USD): " <<totalInv << endl;
- cout << left << setw(10) << "Maximum return (USD): " <<maxRusd << endl;
- cout << left << setw(10) << "Maximum loss (USD): " <<maxLusd << endl;
- if(is_sound == true){
- cout << left << setw(10) << "Portfolio is a sound investment"<< endl;
- }
- else{
- cout << left << setw(10) << "Portfolio is NOT a sound investment"<< endl;
- }
- ///12. Output results to the file, utilize the output manipulators
- outFile << left << setw(10) << "Annual Analysis: " << endl;
- outFile << left << setw(10) << "Total investment (USD): " <<totalInv << endl;
- outFile << left << setw(10) << "Maximum return (USD): " <<maxRusd << endl;
- outFile << left << setw(10) << "Maximum loss (USD): " <<maxLusd << endl;
- if(is_sound == true){
- outFile << left << setw(10) << "Portfolio is a sound investment"<< endl;
- }
- else{
- outFile << left << setw(10) << "Portfolio is NOT a sound investment"<< endl;
- }
- ///14.Close the input and output files.
- outFile.close();
- inFile.close();
- ///15. Prompt user for continuation of program. 1 = Continue, Any other character = Exit.
- cout <<"\n\n\nEnter 1 to process another file. Enter any other key to exit: ";
- cin >> resp;
- if(resp == 1){
- choice == true;
- }
- else{
- break;
- return 0;
- }
- ///END OF OUTER WHILE LOOP
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment