Structure problem.. stachdump

hi everyone, please help: I doing a structure problem, it compiles fine, it looks fine to me as well, gives me stackdumpfile error. Any suggestions folks.. please help:
Below is my code:
/*
* structp.cpp
*
* Created on: Jan 31, 2014
* Author: BK
*/
#include<iostream>
#include<iomanip>
#include<string>
#include<cstdlib>

using namespace std;

struct DrinkData {
string nam;
float pr;
int nB;
};

void getData(DrinkData[]);

int showChoices(DrinkData[]);

void processSelection(int, DrinkData[]);

float calculateEarnings(DrinkData[], int);

int main() {
DrinkData dd[5];
//getData(dd);
showChoices(dd);
do{
processSelection(showChoices(dd), dd);
}while(showChoices(dd)!=6);
cout<<"Amount earned : "<<calculateEarnings(dd,showChoices(dd));
}

void getData(DrinkData drink[5]) {
drink[0].nam = "Coke(Can)";
drink[1].nam = "Coke(Bottle)";
drink[2].nam = "Sprite(Bottle)";
drink[3].nam = "Water";
drink[4].nam = "Full Throttle";

drink[0].pr = 0.75;
drink[1].pr = 1.25;
drink[2].pr = 1.25;
drink[3].pr = 1.25;
drink[4].pr = 2.00;

drink[0].nB = 4;
drink[1].nB = 4;
drink[2].nB = 4;
drink[3].nB = 4;
drink[4].nB = 4;
}

int showChoices(DrinkData drink[5]) {
int ch;
cout << "Vending Machine Options :\n";
for (int i = 0; i < 6; i++) {
cout << "[" << i + 1 << "]" << " " << drink[i].nam << " ... " << "$"
<< drink[i].pr << "\n";
if (i == 5)
cout << "[" << i + 1 << "]" << "Quit the Program\n";
}

cout << "Enter your choice : ";
cin >> ch;

return ch;

}

void processSelection(int ch, DrinkData drink[5]) {

if (drink[ch].nB < 1)
cout << "The item is sold out.\n";
else {
drink[ch].nB = drink[ch].nB - 1;
cout << "The price is : " << drink[ch].pr;
}
return;
}

float calculateEarnings(DrinkData drink[5], int ch)
{
float amount= 0;
amount= amount+drink[ch].pr;
return amount;
}
wrap your code in code tags and format it properly
http://www.cplusplus.com/articles/z13hAqkS/
Thank Yanson, sorry about that as well, wasn't aware.
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
/*
 * structp.cpp
 *
 *  Created on: Jan 31, 2014
 *      Author: BK
 */
#include<iostream>
#include<iomanip>
#include<string>
#include<cstdlib>

using namespace std;

struct DrinkData {
	string nam;
	float pr;
	int nB;
};

void getData(DrinkData[]);

int showChoices(DrinkData[]);

void processSelection(int, DrinkData[]);

float calculateEarnings(DrinkData[], int);

int main() {
	DrinkData dd[5];
	//getData(dd);
	showChoices(dd);
	do{
	processSelection(showChoices(dd), dd);
	}while(showChoices(dd)!=6);
	cout<<"Amount earned : "<<calculateEarnings(dd,showChoices(dd));

	return 0;
}

void getData(DrinkData drink[5]) {
	drink[0].nam = "Coke(Can)";
	drink[1].nam = "Coke(Bottle)";
	drink[2].nam = "Sprite(Bottle)";
	drink[3].nam = "Water";
	drink[4].nam = "Full Throttle";

	drink[0].pr = 0.75;
	drink[1].pr = 1.25;
	drink[2].pr = 1.25;
	drink[3].pr = 1.25;
	drink[4].pr = 2.00;

	drink[0].nB = 4;
	drink[1].nB = 4;
	drink[2].nB = 4;
	drink[3].nB = 4;
	drink[4].nB = 4;
}

int showChoices(DrinkData drink[5]) {
	int ch;
	cout << "Vending Machine Options :\n";
	for (int i = 0; i < 6; i++) {
		cout << "[" << i + 1 << "]" << " " << drink[i].nam << " ... " << "$"
				<< drink[i].pr << "\n";
		if (i == 5)
			cout << "[" << i + 1 << "]" << "Quit the Program\n";
	}

	cout << "Enter your choice : ";
	cin >> ch;

	return ch;

}

void processSelection(int ch, DrinkData drink[5]) {

	if (drink[ch].nB < 1)
		cout << "The item is sold out.\n";
	else {
		drink[ch].nB = drink[ch].nB - 1;
		cout << "The price is : " << drink[ch].pr;
	}
	return;
}

float calculateEarnings(DrinkData drink[5], int ch)
{
	float amount= 0;
	amount= amount+drink[ch].pr;
	return amount;
}
Line 63 for (int i = 0; i < 6; i++) { should be for (int i = 0; i < 5; i++) {

also you need to subtract one from ch in your processSelection function
the users options are 1 - 5 but the array is 0 - 4
Last edited on
Thanks Yanson got is sorted out:
Below is my working code:
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
/*
 * structp.cpp
 *
 *  Created on: Jan 31, 2014
 *      Author: BK
 */
#include<iostream>
#include<iomanip>
#include<string>
#include<cstdlib>

using namespace std;

struct DrinkData {
	string nam;
	float pr;
	int nB;
};

void getData(DrinkData[]);

int showChoices(DrinkData[]);

void processSelection(int, DrinkData[]);

float calculateEarnings(DrinkData[], int);

int main() {

	DrinkData dd[5];

	int ch, i;
	float amount;
	amount = 0;
	i = 0;

	getData(dd);
	ch = showChoices(dd);
	amount = amount + calculateEarnings(dd, ch);

	while (ch < 5) {
		processSelection(ch, dd);
		if (dd[ch].nB >= 1)
			amount = amount + calculateEarnings(dd, ch);
		ch = showChoices(dd);
	}

	cout << "Amount earned : " << amount;

	return 0;
}

void getData(DrinkData drink[5]) {
	drink[0].nam = "Coke (Can)";
	drink[1].nam = "Coke (Bottle)";
	drink[2].nam = "Sprite (Bottle)";
	drink[3].nam = "Water";
	drink[4].nam = "Full Throttle";

	drink[0].pr = 0.75;
	drink[1].pr = 1.25;
	drink[2].pr = 1.25;
	drink[3].pr = 1.25;
	drink[4].pr = 2;

	drink[0].nB = 4;
	drink[1].nB = 4;
	drink[2].nB = 4;
	drink[3].nB = 4;
	drink[4].nB = 4;
	return;
}

int showChoices(DrinkData drink[5]) {
	int ch, i;
	cout << "Vending Machine Options :\n";
	for (i = 0; i < 5; i++) {
		cout << "[" << i + 1 << "]" << " " << drink[i].nam << " ... " << "$"
				<< (drink[i].pr) << " ... Quantity available : " << drink[i].nB
				<< "\n";

	}
	cout << "[6] " << "Quit the Program\n\n";

	cout << "Enter your choice : ";

	cin >> ch;

	cout << "\n";

	return ch;

}

void processSelection(int ch, DrinkData drink[5]) {

	drink[ch].nB = drink[ch].nB - 1;
	cout << "\n";
	cout << "the quantity of the " << drink[ch].nam << " is : " << drink[ch].nB
			<< "\n\n";
	if (drink[ch].nB < 1)
		cout << "The item " << drink[ch].nam << " is sold out.\n\n";
	else
		cout << "the price of the " << drink[ch].nam << " is : $"
				<< drink[ch].pr << "\n\n";

	return;
}

float calculateEarnings(DrinkData drink[5], int ch) {
	return drink[ch].pr;
}
Topic archived. No new replies allowed.