Why do I get strange values after I use functions to call on a data structure to perform various tasks?

Hello,

I have set a simple program to compute share variables. Unfortunately, I obtain very strange output values! I am not sure why?

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
  #include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

struct stock {
	string stock_name;
	double earnings_per_share;
	double est_price_to_earnings_ratio;
};

void share_price(stock);
void display_share_information(stock);
void enter_stock_info(stock);

void share_price(stock A)
{
	cout << "The share price is" << A.earnings_per_share << " x " << A.est_price_to_earnings_ratio <<
		" = " << A.earnings_per_share * A.est_price_to_earnings_ratio << endl;
}

void display_share_information(stock A)
{
	cout << "Your values or this stock are the following" << endl;
	cout << "Stock Name " << A.stock_name << endl;
	cout << "Earnings per share " << A.earnings_per_share << endl;
	cout << "Estimated price to earnings ratio" << A.est_price_to_earnings_ratio << endl;
}

void enter_stock_info(stock A)
{
	cout << "What is the name of the stock?" << endl;
	getline(cin, A.stock_name);

	cout << "What is the earnings per share?" << endl;
	cin >> A.earnings_per_share;

	cout << "What is the estimated price to earnings ratio?" << endl;
	cin >> A.est_price_to_earnings_ratio;
}

int main()
{
	stock stock_1;

	enter_stock_info(stock_1);
	share_price(stock_1);
	display_share_information(stock_1);
	
	system("pause");
	
	return 0;
}



[\output] What is the name of the stock?
10
What is the earnings per share?
10
What is the estimated price to earnings ratio?
10
The share price is-9.25596e+61 x -9.25596e+61 = 8.56729e+123
Your values or this stock are the following
Stock Name
Earnings per share -9.25596e+61
Estimated price to earnings ratio-9.25596e+61
[/output]
You are passing copies of stock_1 to each function. Each function works on that copy and then discards it, so the original stock_1 is unchanged.

This is called "pass by value".

Read up on "pass by reference" to learn how to pass to a function, not a copy of a variable, but the actual original variable.
By default, in C++, function parameters are passed by value. If you want that function to return something through one of its arguments then that needs to be a reference parameter. Line 32 should be (noting the &):
void enter_stock_info(stock &A)

You will also have to change the declaration on line 16 to reflect this.
Hello,

Thanks very much, I referred back to that and have amended my code below. This incorporates calls by references as supposed to by value.

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
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

struct stock {
	string stock_name;
	double earnings_per_share;
	double est_price_to_earnings_ratio;
};

void share_price(stock&);
void display_share_information(stock&);
void enter_stock_info(stock&);

void share_price(stock &A)
{
	cout	<< "The share price is " << A.earnings_per_share << " x " << A.est_price_to_earnings_ratio << " = " 
			<< (A.earnings_per_share * A.est_price_to_earnings_ratio) << endl;
}

void display_share_information(stock &A)
{
	cout << "Your values or this stock are the following" << endl;
	cout << "Stock Name " << A.stock_name << endl;
	cout << "Earnings per share " << A.earnings_per_share << endl;
	cout << "Estimated price to earnings ratio" << A.est_price_to_earnings_ratio << endl;
}

void enter_stock_info(stock &A)
{
	cout << "What is the name of the stock?" << endl;
	getline(cin, A.stock_name);

	cout << "What is the earnings per share?" << endl;
	cin >> A.earnings_per_share;

	cout << "What is the estimated price to earnings ratio?" << endl;
	cin >> A.est_price_to_earnings_ratio;
}

int main()
{
	stock stock_1;

	enter_stock_info(stock_1);
	share_price(stock_1);
	display_share_information(stock_1);
	
	system("pause");
	
	return 0;
}
Registered users can post here. Sign in or register to post.