Getting random value from function

I'm not sure what I'm doing wrong :(

My code is below, it keeps giving me a huge negative number for the total sales and avrg quarterly sales. Once I put the formulas in the DisplayData function it works fine! But I'm not supposed to do that so :S

I don't want exact answers or code! Just pointers if you could please. I have the solution manual for my book but I'd rather not look at it (this isn't homework btw!)

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
#include <iostream>
#include <windows.h> 
#include <string>

using namespace std;

struct CorpData
{
	string Division;
	int firstq;
	int secondq;
	int thirdq;
	int fourthq;
	int toal;
	int avrg;
};

void placeCursor(HANDLE, int, int);
void displayPrompts(HANDLE);
void getUserInput(HANDLE, CorpData&);
void displayData(HANDLE, CorpData, CorpData);
void Calculate(CorpData&, CorpData&);

int main()
{
	CorpData input, value; 
	

	
	HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);

	displayPrompts(screen);
	getUserInput(screen, input);
	displayData(screen, input, value);
	system("pause >nul");
	return 0;
}





void placeCursor(HANDLE screen, int row, int col)
{ 
	COORD position;
	position.Y = row;
	position.X = col;
	SetConsoleCursorPosition(screen, position);
}


void displayPrompts(HANDLE screen)
{

	placeCursor(screen, 3, 25);
	cout << "******* Corporation Form *******" << endl;
	placeCursor(screen, 5, 25);
	cout << "Division: " << endl;
	placeCursor(screen, 7, 25);
	cout << "First quarter sales: " << endl;
	placeCursor(screen, 9, 25);
	cout << "Second quarter sales: " << endl;
	placeCursor(screen, 11, 25);
	cout << "Third quarter sales: " << endl;
	placeCursor(screen, 13, 25);
	cout << "Fourth quarter sales: " << endl;

}

void getUserInput(HANDLE screen, CorpData &input)
{


	placeCursor(screen, 5, 34);
	getline(cin, input.Division);
	placeCursor(screen, 7, 45);
	cin >> input.firstq;
	placeCursor(screen, 9, 46);
	cin >> input.secondq;
	placeCursor(screen, 11, 45);
	cin >> input.thirdq;
	placeCursor(screen, 13, 46);
	cin >> input.fourthq;



}

void Calculate(CorpData &value, CorpData &input)
{
	
	value.toal = input.firstq + input.secondq + input.thirdq + input.fourthq;
	value.avrg = (input.firstq + input.secondq + input.thirdq + input.fourthq) / 4;

}



void displayData(HANDLE screen, CorpData input, CorpData value)
{

	placeCursor(screen, 15, 0);
	cout << "Division: " << input.Division << endl;
	cout << "First Quarter Sales: " << "$" << input.firstq << endl;
	cout << "Second Quarter Sales: " << "$" << input.secondq << endl;
	cout << "Third Quarter Sales: " << "$"<< input.thirdq << endl;
	cout << "Fourth Quarter Sales: " << "$" << input.fourthq << endl;
	cout << "Annual Sales: " << "$" << value.toal << endl;
	cout << "Average Quarterly Sales: " << "$" << value.avrg << endl;

}
What happens after you get the user input?
This is the output it gives:



******* Corporation Form *******

Division:west

First quarter sales:1

Second quarter sales:1

Third quarter sales:1

Fourth quarter sales:1

Division: west
First Quarter Sales: $1
Second Quarter Sales: $1
Third Quarter Sales: $1
Fourth Quarter Sales: $1
Annual Sales: $-858993460
Average Quarterly Sales: $-858993460

Yes, but in your code... You get the user input, and then what?
Oh...am I supposed to save it somewhere? Like..store the information? ( I thought & was enough?)
Last edited on
It's stored, in "input". But that doesn't automatically give the total and average ;)

You have a calculate function for that...
Last edited on
@__@ I think my brain is a little fried cause I still have no clue what to do haha, but I'm sure if I read more into what you have said so far i'll be able to figure it out.

Thank you for your guidance!
Yeah, it goes like that sometimes...

You forgot just one little thing...
You ask for user input, so the user inputs the 4 quarters. And then you print these 4 quarters and the total and average, which have been calculated... Where?
Haha wow I got to retrying this code and your pointer was VERY useful! I can't believe I forgot to add the void function line in my code in DisplayData. Thanks a lot again !!
Last edited on
Topic archived. No new replies allowed.