### Odd Output

First I add a stock(selection 2) with the input: "add", "18", "300". Second I add another stock(selection 2) with input: "add", "17", "300".

Shouldn't the output be 17.5 from (18*(300/(300+300))) + (17*(300/(300+300)))?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119`` ``````#include "stdafx.h" #include #include using namespace std; struct stock { string name; double buyPrice; int shares; }; void Display(stock tempStocks[]); void Initialize(stock tempStocks[]); int main() { const int MAX_INVENTORY = 10; int inventory = 0; stock stocks[MAX_INVENTORY]; int choice = 0; string tempName = ""; int temp; do { cout << endl << "Inventory" << endl; cout << "---------" << endl << endl; cout << "0 - Quit" << endl; cout << "1 - Display stock portfolio"; cout << endl; cout << "2 - Add a stock to portfolio"; cout << endl; cout << "3 - Remove a stock from portfolio"; cout << endl; cout << endl << "Choice: "; cin >> choice; cout << endl; switch (choice) { case 0: cout << "Good-bye." << endl; break; case 1: cout << "Portfolio:" << endl; Display(stocks); break; case 2: cout << "Enter the stock name: "; cin >> stocks[inventory].name; cout << "Enter the stock buy price: "; cin >> stocks[inventory].buyPrice; cout << "Enter how many shares: "; cin >> stocks[inventory].shares; temp = inventory-1; while ( temp >= 0)//1 0 { if (stocks[inventory].name == stocks[temp].name) { stocks[temp].buyPrice = (stocks[inventory].buyPrice * (stocks[inventory].buyPrice / (stocks[inventory].buyPrice + stocks[temp].buyPrice))) + (stocks[temp].buyPrice *(stocks[temp].buyPrice / (stocks[temp].buyPrice + stocks[inventory].buyPrice))); stocks[temp].shares += stocks[inventory].shares; //erase data stocks[inventory].name = ""; stocks[inventory].buyPrice = 0.0; stocks[inventory].shares = 0; inventory--; } temp--; } cout << inventory; inventory++; break; case 3: cout << "Enter the name of the stock you want to remove: "; cin >> tempName; cout << inventory; if (stocks[inventory-1].name == tempName) { cout << inventory; stocks[inventory-1].name = ""; stocks[inventory-1].buyPrice = 0.0; stocks[inventory-1].shares = 0; } inventory--; break; default: cout << "Sorry, " << choice; cout << " isn't a valid choice." << endl; } } while (choice != 0); return 0; } void Display(stock tempStocks[]) { int count = 0; while (tempStocks[count].name != "") cout << ++count; for (int i = 0; i < count; i++) { cout << "Stock Name: " << tempStocks[i].name << endl; cout << "Stock Buy Price: " << tempStocks[i].buyPrice << endl; cout << "Stock Shares: " << tempStocks[i].shares << endl; } } void Initialize(stock tempStocks[]) { tempStocks[0].name = ""; tempStocks[0].buyPrice = 0.0; tempStocks[0].shares = 0; } ``````

Last edited on
> Shouldn't the output be 17.5 from (18*(300/(300+300))) + (17*(300/(300+300)))?
 ``12`` ``````stocks[temp].buyPrice = (stocks[inventory].buyPrice * (stocks[inventory].buyPrice / (stocks[inventory].buyPrice + stocks[temp].buyPrice))) + (stocks[temp].buyPrice *(stocks[temp].buyPrice / (stocks[temp].buyPrice + stocks[inventory].buyPrice)));``````
you never use `.shares' in your formula
you are doing `18 * 18 / (18+17) + 17 * 17 / (18+17)`

as a suggestion, create an average function to tidy your code.
Last edited on
Whoops. Thank you.
Topic archived. No new replies allowed.