functions (numbers)

if you see right under the profit/loss in the output its showing crazy number. can someone check how it get rid of it
$8.96872e+10

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
 //header file declration

#include<iostream>

#include<stdlib.h>

#include<iomanip>
//Namespace declration

using namespace std;

//structure declration

struct StockInfo

{

//structure member variables

string coname;

int numShares;

float PurPrice;

float CurrPrice;

float ProfitLoss;

}portfolio[10];

//function declrations

void addRecord(struct StockInfo portfolio[], int count);

void displayRecord(struct StockInfo portfolio[], int count);

//start of main function

int main()

{

//crating structure object

struct StockInfo portfolio[10];

static int count = 0;

int option;

//start of do while loop
//this loop will display menu again and again until user select option 3

do

{

cout<<"Please select an option: "<<endl;

cout<<"1-Add Stock"<<endl;

cout<<"2.Display Profit/Loss"<<endl;

cout<<"3.Exit Program"<<endl;

cin>>option;

//if user select option 1


if(option==1)

{

count++;

//if array is full then print this message

if(count>=10)

{

cout<<"Sorry..!!!There is no space to add stock. Room is full."<<endl;

}

//call to function

addRecord(portfolio, count);

}

//if user select option 2

else if(option == 2)

{

//call to function

displayRecord(portfolio, count);

}

//if user select option 3 terminate the program

else if(option==3)
{

exit(0);

}

}while(option>0 && option<3);


return 0;

}

//defintion for addRecord function

void addRecord(struct StockInfo portfolio[], int count)

{

cout<<"Enter Company Name: ";

cin>>portfolio[count].coname;

cout<<"Number of Shares: ";

cin>>portfolio[count].numShares;

cout<<"Purchase Price: ";

cin>>portfolio[count].PurPrice;

cout<<"Current Price: ";

cin>>portfolio[count].CurrPrice;

//calculate current price

//portfolio[count].ProfitLoss=portfolio[count].numShares * (portfolio[count].CurrPrice - portfolio[count].PurPrice);

}

//function definition for displayRecord function

void displayRecord(struct StockInfo portfolio[], int count)
{
cout<<"Portfolio Report\n\n================"<<endl;

cout<<"Company\t\tProfit(Loss)"<<endl;
{
	for(int i=0; i<= count; i++)
{
	
	double ProfitLossoss= portfolio[i].numShares * (portfolio[i].CurrPrice-portfolio[i].PurPrice);

	cout<<portfolio[i].coname<<setw(15)<<"$"<<setw(5)<<ProfitLossoss<<endl;//cout<<"Your total Profitloss is "<<Profitloss<<endl;
}
}
}	


output
Please select an option:
1-Add Stock
2.Display Profit/Loss
3.Exit Program
1
Enter Company Name: wow
Number of Shares: 23
Purchase Price: 14
Current Price: 15
Please select an option:
1-Add Stock
2.Display Profit/Loss
3.Exit Program
1
Enter Company Name: lo
Number of Shares: 23
Purchase Price: 45
Current Price: 56
Please select an option:
1-Add Stock
2.Display Profit/Loss
3.Exit Program
2
Portfolio Report

================
Company Profit(Loss)
$8.96872e+10
wow $ 23
lo $ 253
Please select an option:
1-Add Stock
2.Display Profit/Loss
3.Exit Program


line 158. When i is equal to 0, portfolio[0].numShares has no value, etc.

Why?

line 76. The first use of count is to increase it to 1. portfolio[0] gets skipped.
Hello poonamp6792,

When I tested your program this is what I found:

You are missing the include file "<string>".

The line using namespace std; should be avoided.

The struct is better defined like this:
1
2
3
4
5
6
7
8
9
10
struct StockInfo
{
	//structure member variables

	string coname;
	int numShares{};
	double PurPrice{};
	double CurrPrice{};
	double ProfitLoss{};
};  // Leave off "portfolio[10]" as it is defined in main. 

The extra blank lines are not really needed, but OK. For floating point numbers it is better to use "double"s than "float"s. "double"s will store the number better and are less prone to the problems "float"s have. You also have more precision, i.e., more places to the right of the decimal pointnwith a double. Better for calculations.

In your proto types, that you call function declarations, the use of "struct" is not necessary. Just the type name, i.e., "StockInfo".

In main:

"struct" is not needed before "StockInfo" and "static" is not needed before "int count = 0; as main is the only place that it is changed.

Inside the do/while loop I started the first "cout" with a "\n" to break up the output for better readability.

Inside your if statement "count++;" should be the last line of the block so it will start with zero not one.

Hint: an if statement, for loop or while loop with only one line does not need the {} to form a block, but OK if you use them.

In option 3 using "exit(0);" works, but not the best way to end the program. What I like to do is define bool cont{ true }; then in the end of the do /while loop say while (cont);. And for option 3 say cont = false;. You may find this more useful in the future.

Lastly in the "displayRecord" function I added this line at the beginning:
std::cout << std::fixed << std::showpoint << std::setprecision(2);. The "fixed" says to use a regular decimal number not scientific notation. The " std::showpoint" will print ".0" in the output. And the "std::setprecision(2)" tells it how many numbers to print to the right of the decimal point. This only needs to be done once and will stay this way until changed or the program ends.

In the for loop you have i<= count this should be i < count. The "=" causes one to many elements of the array to be used and will cause the loop to go one past the end of the array.

In the "cout" statement the "setw(15),which I changed to 16, needs to go before what it is controling. Putting it after " portfolio[i].coname" has no effect. This is what I did for a quick fix to your "cout" statement:
cout << std::left << setw(16) << portfolio[i].coname << "$" << std::right << setw(5) << ProfitLossoss << endl;
I added the "std::left" and "std::right so the output would come out correctlu on my system. These may not be needed for your system.

In the end initializing the variables in the struct and any others may eliminate your problem because uninitialized you are using whatever is at the memory location when that variable is defined and whatever is there is turned into a large number that yo do not want. On my computer this us usually a vary large negative number.

Hope this helps,

Andy
Topic archived. No new replies allowed.