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)))?
Instead the output is 17.5143

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "stdafx.h"

#include <iostream>
#include <string>

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)))?
1
2
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.
Registered users can post here. Sign in or register to post.