Guest User

Untitled

a guest
Mar 22nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. //Programmer: Daniel B. Papp
  2. //CS150 SP18
  3. //TA:
  4. //DATE: 03/18/2018
  5. ///The following program takes input from an investment portfolio and displays
  6. ///the content to the output device (monitor/outfile). It also calculates the
  7. ///total invested, Possible annual holdings for maximum loss and gain and whether
  8. ///the portfolio is a sound investment or not. Outputting the information to the output
  9. ///device as well.
  10. #include <iostream>
  11. #include <iomanip>
  12. #include <fstream>
  13. #include <math.h>
  14. using namespace std;
  15. int main()
  16. {
  17.  
  18. ///PART A Declare and Initialize____________________________________________________________________________
  19.  
  20. ///1. Declare and INITAILIZE the variables
  21. string Symbol;
  22. double Invested_amount = 0;
  23. float currentP, maxR = 0, maxL = 0, totalInv = 0, maxRov = 0, maxLov = 0, soundness, maxRusd = 0, maxLusd = 0;
  24. int resp;
  25. bool is_sound;
  26. bool choice = true;
  27. string inFile_name;
  28. string outFile_name;
  29.  
  30. ///2.Declare the input file
  31. ifstream inFile;
  32.  
  33. ///3.Declare the output file
  34. ofstream outFile;
  35.  
  36. ///4.OUTER WHILE LOOP GOES HERE
  37. while(choice == true){
  38.  
  39. ///5. Set output fixed, showpoint, setprecision of two for all decimals
  40. outFile<<setprecision(2)<<showpoint<<fixed;
  41.  
  42.  
  43. ///6. Prompt user for the input file(Ex. folio1.txt):
  44. cout<< "Enter input file name: ";
  45. cin>>inFile_name;
  46. ///a. Open the input file using the users input, remember to convert string to a c string (ex. filename.c_str())
  47. inFile.open(inFile_name.c_str());
  48. if(!inFile){
  49. ///b. Include error handling: if file cannot be found -> exit program
  50. cout<<"File could not be found. Exiting program."<<endl;
  51. return 0;
  52. }
  53. ///6. Prompt user for the output file
  54. ///a. Use a DO-WHILE to avoid overwriting the input file
  55. do{
  56. cout<<"Under what file name would you like to save the output data (ex: folio1out.txt): ";
  57. cout<<"\nWARNING: name cannot be the same as the input file."<<endl;
  58. cin>>outFile_name;
  59. }
  60. while(outFile_name == inFile_name);
  61. ///b. Open the output file using the users input, remember to convert to a c string (ex. filename.c-str())
  62. outFile.open(outFile_name.c_str());
  63.  
  64.  
  65.  
  66.  
  67. ///PART B Input and computations____________________________________________________________________________
  68.  
  69.  
  70. ///7. Output the table heading to the monitor and output file here (EX. SYMBOL CURRENT PRICE(USD) INVESTED AMOUNT(USD) MAX Return(%) Max Loss(%))
  71. ///Remember to use the iomanipulator class to format the output
  72. cout << setfill(' ') << left << setw(7) << "Symbol" << setw(20) << "Current Price (USD)"
  73. <<setw(22) << "Invested Amount (USD)" << setw(15) << "Max Return (%)" << setw(15) << "Max Loss (%)";
  74.  
  75. outFile << setfill(' ') << left << setw(7) << "Symbol" << setw(20) << "Current Price (USD)"
  76. <<setw(22) << "Invested Amount (USD)" << setw(15) << "Max Return (%)" << setw(15) << "Max Loss (%)";
  77.  
  78. cout<<setprecision(2)<<fixed<<showpoint;
  79. ///8. Utilize a WHILE loop to open the input file and extract the information sequentially until end of file.
  80. while(!inFile.eof())
  81. {
  82. ///a. Get (read) file input
  83. inFile>>Symbol>>currentP>>Invested_amount>>maxR>>maxL;
  84. ///b. Compute Max_return, Max_loss, Total_invested using a summation that continually compounds on the previous input
  85. totalInv+= Invested_amount;
  86. maxRusd = maxRusd + ((maxR/100)*Invested_amount);
  87. maxLusd = maxLusd + ((maxL/100)*Invested_amount);
  88.  
  89. ///c. Output all information for each stock/asset currently in the loop (EX. QCV 30 300 100 100)
  90.  
  91. ///cout<<Symbol<<currentP<<Invested_amount<<maxR<<maxL<<endl;
  92. cout<<"\n" << setfill(' ') << left << setw(7) << Symbol << setw(20) << currentP
  93. <<setw(22) << Invested_amount << setw(15) << maxR << setw(15) << maxL;
  94.  
  95. outFile<<"\n" << setfill(' ') << left << setw(7) << Symbol << setw(20) << currentP
  96. <<setw(22) << Invested_amount << setw(15) << maxR << setw(15) << maxL;
  97.  
  98.  
  99. }///End of while loop
  100.  
  101.  
  102. ///9. Compute the aggregate values utilizing Max Return, Max Loss, and Total invested
  103. maxRov = maxRusd/totalInv;
  104. maxLov = maxLusd/totalInv;
  105.  
  106. ///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
  107. soundness = (0.40*maxRov)-(0.60*maxLov);
  108. if (soundness > 1){
  109. is_sound = true;
  110. }
  111. else{
  112. is_sound = false;
  113. }
  114. ///Part C Output_____________________________________________________________________________________
  115.  
  116. cout << endl << endl;
  117. outFile << endl << endl;
  118.  
  119. ///11. Output results to the monitor, utilize the output manipulators
  120. cout << left << setw(10) << "Annual Analysis: " << endl;
  121. cout << left << setw(10) << "Total investment (USD): " <<totalInv << endl;
  122. cout << left << setw(10) << "Maximum return (USD): " <<maxRusd << endl;
  123. cout << left << setw(10) << "Maximum loss (USD): " <<maxLusd << endl;
  124. if(is_sound == true){
  125. cout << left << setw(10) << "Portfolio is a sound investment"<< endl;
  126. }
  127. else{
  128. cout << left << setw(10) << "Portfolio is NOT a sound investment"<< endl;
  129. }
  130.  
  131. ///12. Output results to the file, utilize the output manipulators
  132. outFile << left << setw(10) << "Annual Analysis: " << endl;
  133. outFile << left << setw(10) << "Total investment (USD): " <<totalInv << endl;
  134. outFile << left << setw(10) << "Maximum return (USD): " <<maxRusd << endl;
  135. outFile << left << setw(10) << "Maximum loss (USD): " <<maxLusd << endl;
  136. if(is_sound == true){
  137. outFile << left << setw(10) << "Portfolio is a sound investment"<< endl;
  138. }
  139. else{
  140. outFile << left << setw(10) << "Portfolio is NOT a sound investment"<< endl;
  141. }
  142.  
  143. ///14.Close the input and output files.
  144. outFile.close();
  145. inFile.close();
  146.  
  147.  
  148.  
  149. ///15. Prompt user for continuation of program. 1 = Continue, Any other character = Exit.
  150. cout <<"\n\n\nEnter 1 to process another file. Enter any other key to exit: ";
  151. cin >> resp;
  152.  
  153. if(resp == 1){
  154. choice == true;
  155. }
  156. else{
  157. break;
  158. return 0;
  159. }
  160.  
  161. ///END OF OUTER WHILE LOOP
  162. }
  163.  
  164.  
  165. return 0;
  166. }
Add Comment
Please, Sign In to add comment