Program compiles but crashes

I'm trying to write a program that, you tell it what you want, and it removes one from the quantity. It's compiling just fine and it even sends back the fail message I set up but it won't replace the number I want it to. I'm not allowed to modify the text file in any way.

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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

	struct snacksJustString
	{
		string name;
		string price;
		int quant;
	};
	
	void display();
	snacksJustString getQuant(int);
snacksJustString mabob[5];
int main()
{
	for (;;)
	{
		int I;
		display();
		cout << "Please enter the number you want. Enter 0 to quit." << endl;
		cin >> I;
		if (I==0)
		{
			return 0;
		}
		else if (I>5)
		{
			cout << "not a valid input" << endl;
		}
		else
		{
			cout << getQuant(I).name << endl;
		}
	}
}

void display()
{
	fstream doneDone("DrinkMachine.txt");
	string thing;
	int stuff=6;
	int burb = 0;
	while (stuff!=0)
	{
		getline (doneDone, thing);
		cout << burb << " " << thing << endl;
		burb++;
		stuff = stuff-1;
	}
   doneDone.close();
}

snacksJustString getQuant (int I)
{
	bool yaDoneDidIt = false;
	for (;;)
	{
	snacksJustString fail;
	fail.name = "fail";
	snacksJustString win;
	win.name = "win";
	int stuff=6;
	int twentyThree = 0;
	int getname = 1;
	int getprice = 1;
	int getquant = 1;
	string junkString;
	fstream doneDone;
	doneDone.open("DrinkMachine.txt");
	cout << "line94" << endl; //crashes here
	for(;twentyThree<=100;)
	{
		if (twentyThree<=5 || twentyThree==10 || twentyThree==17 || twentyThree==21)
		{
			doneDone >> junkString;
		}
		if (twentyThree==6 || twentyThree==9 || twentyThree==13 || twentyThree==16 ||twentyThree == 20)
		{
			doneDone >> mabob[getname].name;
			getname++;
		}
		if (twentyThree==7 || twentyThree==11 || twentyThree==14 || twentyThree==18 || twentyThree==22)
		{
			doneDone >> mabob[getprice].price;
			getprice++;
		}
		if (twentyThree==8 || twentyThree==12 || twentyThree==15 || twentyThree==19 || twentyThree==23)
		{
			if (yaDoneDidIt == true && getquant==I)
			{
				mabob[getquant].quant--;
				doneDone << mabob[getquant].quant;
				doneDone.close();
				return win;
			}
			doneDone >> mabob[getquant].quant;
			if (getquant==I)
			{
				if (mabob[I].quant == 0)
				{
					doneDone.close();
					return fail;
				}
				else
				{
					yaDoneDidIt = true;
				}
			}
			getquant++;
		}
		twentyThree++;
	}
}
}

Here's the contents of the text file:
Drink Name Cost Number in Machine
Cola $0.75 20
Root Beer $0.75 0
Lemon-Lime $0.75 20
Grape Soda $0.80 20
Cream Soda $0.80 20
this is somewhat confusing to read but it sure looks suspect to see:

for(;twentyThree<=100;)
{
...
getprice++;

mabob[getprice].price;

and similar logic.

I would almost bet money that you increment past 5 in there somewhere. If it is crashing, I would check that.

That aside, what exactly did not update as expected? What did you input? How far into the program did it get "correctly"? Youll want to put some debugging print statements in there, trace the logic, and see where it went wrong.

Last edited on
Topic archived. No new replies allowed.