decimals

can some tell me how to process decimals in code. my code doesnt do decimals for current and purchase price, as soon as i try to run it , it crashes

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
#include <iostream> 
#include <string>
#include <iomanip>
using namespace std;
	
	struct StockInfo
	
	{
		string coname;
		int numShares;
		int PurPrice;
		int CurrPrice;
	};
	
	int main()
	{
		const int  size =2;
		 StockInfo portfolio[size];
		
	int choice;

do
{


cout << endl
<< " 1 - Add a stock.\n"
<< " 2 - Display Profit/loss.\n"
<< " 3 - Exit Program.\n"
<< " Enter your choice and press return: ";
cin >> choice;

switch (choice)
{
case 1:

	for(int i =0;i< size ;i++) 
			{
				
		cout << "enter company's  name please:"<<endl;
		cin>>portfolio[i].coname;
		cout << "enter the number of shares bought:"<<endl;
		cin >> portfolio[i].numShares;
		cout <<"What was the purchase price?"<<endl;
		cin >> portfolio[i].PurPrice;
		cout << "what is the current price of the share?"<<endl;
		cin >> portfolio[i].CurrPrice;
		cin.ignore();
	      }
break;
case 2:
cout<<"Portfolio Report\n\n================"<<endl;

cout<<"Company\t\tProfit(Loss)"<<endl;


	for(int i=0; i< size; i++)
{
	
	double Profitloss= portfolio[i].numShares * (portfolio[i].CurrPrice-portfolio[i].PurPrice);

	cout<<portfolio[i].coname<<setw(15)<<"$"<<setw(5)<<Profitloss<<endl;//cout<<"Your total Profitloss is "<<Profitloss<<endl;
}
//Number of shares * (current price – purchase price)
break;
case 3:
//Exit the program
break;
default:
cout << "Not a Valid Choice. \n"<< "Choose again.\n";
break;
}
}
while(choice <3);


return 0;
}





I think those two functions are the ones you're looking for:

std::setprecision
http://www.cplusplus.com/reference/iomanip/setprecision/

std::showpoint
http://www.cplusplus.com/reference/ios/showpoint/


Also don't double post, instead you can ask two or many different questions in one post in relation to the same code.

What about switches and your functions are you having trouble here?


Last edited on
change your prices from int to double

this line does int * int and saves the result as double, so the decimals were never generated.
double Profitloss= portfolio[i].numShares * (portfolio[i].CurrPrice-portfolio[i].PurPrice);

you need at least one double/float in the expression to force the compiler to promote all other variables to double/float

double = int * double
double = double * int
double = double * double
int = int * int

that should also fix your input problems, when i enter 1.50 as the purchase price, cin only takes "1" because its looking for an int and stops at the decimal. from that point on all your input is screwed to put it nicely :)

oh, and remove that cin.ignore(), i presume you were trying to fix the messed up inputs.
Last edited on
grrr, now i see the double post :)
change your prices from int to double

One should avoid that with money. Rather operate on cents (or even smaller units). Integer values, nevertheless.

Output can format to dollars.
I agree with Jaybob.

When I changed int PurPrice & int CurrPrice to doubles, the program worked for me fine.
Topic archived. No new replies allowed.