fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <stdexcept>
  5. using namespace std;
  6.  
  7.  
  8. struct Asset {
  9. string name;
  10. string type;
  11. int quantity;
  12. float purchase_price;
  13. float current_price;
  14. float transaction_cost;
  15. };
  16. //ici on definit la fonction quinous permet de definir un autre actif
  17. void addAsset(vector<Asset>& portfolio) {
  18. Asset new_asset;
  19.  
  20. cout << " Add a New Asset: ";
  21.  
  22. cout << "Name: ";
  23. cin >> ws;
  24. getline(cin, new_asset.name); //ici on demande a l'utilisateur d'ajouter le nom du nouveau actif
  25.  
  26. cout << "Type: ";
  27. getline(cin, new_asset.type);
  28.  
  29. cout << "Quantity: ";
  30. cin >> new_asset.quantity;
  31. while (new_asset.quantity <= 0) {
  32. cout << "Invalid quantity . Enter again: ";
  33. cin >> new_asset.quantity;
  34. }
  35.  
  36. cout << "Purchase price: ";
  37. cin >> new_asset.purchase_price;
  38. while (new_asset.purchase_price <= 0) {
  39. cout << "Invalid purchase price. Enter again: ";
  40. cin >> new_asset.purchase_price;
  41. }
  42.  
  43. cout << "Current price: ";
  44. cin >> new_asset.current_price;
  45. while (new_asset.current_price <= 0) {
  46. cout << "Invalid current price (must be > 0). Enter again: ";
  47. cin >> new_asset.current_price;
  48. }
  49.  
  50. cout << "Transaction cost: ";
  51. cin >> new_asset.transaction_cost;
  52. while (new_asset.transaction_cost < 0) {
  53. cout << "Invalid transaction cost (must be >= 0). Enter again: ";
  54. cin >> new_asset.transaction_cost;
  55. }
  56.  
  57.  
  58. portfolio.push_back(new_asset);
  59. }
  60. //ici on definit la fonction qui nous permet d'afficher notre porte feuille.
  61. void displayPortfolio( vector<Asset> portfolio) {
  62. for (size_t i = 0; i < portfolio.size(); i++) {
  63. cout << portfolio[i].name << " "
  64. << portfolio[i].type << " "
  65. << portfolio[i].quantity << " "
  66. << portfolio[i].purchase_price << " "
  67. << portfolio[i].current_price << endl;
  68. }
  69. }
  70. //ici on definit une fonction qui permet de rechercher le nom d'un actif dans notre portefeuille
  71. int searchAssetByName(vector<Asset> portfolio, string search_name) {
  72. for (size_t i = 0; i < portfolio.size(); ++i) {
  73. if (portfolio[i].name.compare(search_name) == 0) {
  74. return i;//si on trouve un nom d'actif qui correspond a search_name
  75. //on recupere la position de cet actif
  76. }
  77. }
  78. //si aucun actif a le nom search_name, on affiche un message d'erreur.
  79. throw runtime_error("Error the requested asset does not exist in the portfolio!");
  80. }
  81. //ici on va creer une fonction pour mettre a jour lactif donc le nom est asset_name
  82. void updateAsset(vector<Asset> portfolio, string asset_name) {
  83. int index = searchAssetByName(portfolio, asset_name);
  84.  
  85. displayPortfolio(portfolio[index]);
  86. //nous voulons apresent modifier l'actif index denotre porte feuille
  87. cout << "enter the new name: ";
  88. cin >> ws;
  89. getline(cin, portfolio[index].name); //ici on demande a l'utilisateur d'ajouter le nom de modifier de l'actif
  90.  
  91. cout << "enter the new type: ";
  92. getline(cin, portfolio[index].type);
  93. cout << "enter the new quantity: ";
  94. cin >> portfolio[index].quantity;
  95. while (portfolio[index].quantity <= 0) {
  96. cout << "Error,the quantity must be positive. Try again: ";
  97. cin >> portfolio[index].quantity;
  98. }
  99.  
  100. cout << "enter the new purchase price: ";
  101. cin >> portfolio[index].purchase_price;
  102. while (portfolio[index].purchase_price <= 0) {
  103. cout << "Error the purchase price must be positive. Try again: ";
  104. cin >> portfolio[index].purchase_price;
  105. }
  106.  
  107. cout << "enter the new current price: ";
  108. cin >> portfolio[index].current_price;
  109. while (portfolio[index].current_price <= 0) {
  110. cout << "Error the Current price must be positive. Try again: ";
  111. cin >> portfolio[index].current_price;
  112. }
  113.  
  114. cout << "enter the new transaction cost: ";
  115. cin >> portfolio[index].transaction_cost;
  116. while (portfolio[index].transaction_cost < 0) {
  117. cout << "Error the transaction cost cannot be negative. Try again: ";
  118. cin >> portfolio[index].transaction_cost;
  119. }
  120.  
  121. cout << " the madified asset is given by:" << endl;
  122. displayPortfolio(portfolio[index])
  123. }
  124. int main() {
  125. // Initialisation du portefeuille avec les 5 positions
  126. vector<Asset> portfolio = {
  127. {"ABC Corp", "Stock", 100, 25.50, 28.20, 15.00},
  128. {"XYZ Bank", "Stock", 150, 18.75, 17.90, 25.00},
  129. {"CAM Bond", "Bond", 200, 98.50, 101.20, 28.00},
  130. {"QF ETF", "ETF", 80, 42.00, 46.50, 45.00},
  131. {"NXT Energy", "Stock", 60, 55.00, 44.25, 16.00}
  132. };
  133.  
  134.  
  135.  
  136.  
  137. return 0;
  138. }
Compilation error #stdin compilation error #stdout 0s 5324KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘void updateAsset(std::vector<Asset>, std::__cxx11::string)’:
prog.cpp:85:38: error: could not convert ‘portfolio.std::vector<Asset>::operator[](((std::vector<Asset>::size_type)index))’ from ‘__gnu_cxx::__alloc_traits<std::allocator<Asset>, Asset>::value_type’ {aka ‘Asset’} to ‘std::vector<Asset>’
     displayPortfolio(portfolio[index]);
                                      ^
prog.cpp:122:38: error: could not convert ‘portfolio.std::vector<Asset>::operator[](((std::vector<Asset>::size_type)index))’ from ‘__gnu_cxx::__alloc_traits<std::allocator<Asset>, Asset>::value_type’ {aka ‘Asset’} to ‘std::vector<Asset>’
     displayPortfolio(portfolio[index])
                                      ^
stdout
Standard output is empty