Code will compile but program won't work

It's not giving me any errors so I don't know why it's not working. It just crashes when I try to enter something.

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
snacksJustString getQuant (int I)
{
	snacksJustString fail;
	fail.name = "fail";
	int Z=I+1;
	int stuff=6;
	int six=6;
	int zero=0;
	int twentyThree = 0;
	int getname = 1;
	int getprice = 1;
	int getquant = 1;
	snacksDoubleName temp[3];
	string stringy[23];
	string stringy2[5];
	string junkString;
	snacksJustString thing;
	fstream doneDone;
	doneDone.open("DrinkMachine.txt");
	cout << "line94" << endl; //crashes here
	for(;twentyThree<=23;)
	{
		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)
		{
			doneDone >> mabob[getquant].quant;
			if (getquant==I)
			{
				if (mabob[I].quant == 0)
				{
					return fail;
				}
				else
				{
					mabob[I].quant--;
				}
			}
			getquant++;
		}
		twentyThree++;
	}
	int junkint = mabob[I].quant;
	stringstream convert;
	convert << junkint;
	string morejunk;
	morejunk = convert.str();
	cout << "line 134" << endl;
	for(;six!=0;)
	{
		getline (doneDone, junkString);
		if (zero==I)
		{
			junkString.replace(junkString.begin(), junkString.end()-2, morejunk);
			doneDone << junkString;
		}
		six--;
		zero++;
	}
	return mabob[I];
}

here's what the file looks like...

Drink Name Cost Number in Machine
Cola $0.75 20
Root Beer $0.75 20
Lemon-Lime $0.75 20
Grape Soda $0.80 20
Cream Soda $0.80 20
Hello Robsome97,

I am glad the program compiles for you, but it is missing some information to work for me. Like where are these defined:

1
2
3
snacksJustString
snacksDoubleName
mabob


and where do you get the header files that this function needs?

I see nothing wrong with cout << "line94" << endl; //crashes here other than not knowing where the "iostream" header file comes from. The only other place that could be a problem is the line above, but I can not test it.

I could try making something up, but it may not be correct.

I need more information and some time to work with it to figure it out.

Andy
print statements do not always crash where the output of the program says they crash.

use cout.flush() after each debugging statement of this nature and re-run it.
Even flush is not foolproof, but try it.

There is no reason to crash there. I suspect it failed to open your file and you tried to read from it anyway.... you might try testing that, or, right where that print statement is, before it, try reading something and printing from it as a debugging aid.

Hello Robsome97,

I finally managed to work something up and get the function to compile, but it did not work well. For this part of the code notice the comments that I made:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
snacksJustString fail;  //  <--- Could use a better name for the variabe.
fail.m_name = "fail";  //  <--- Useful only tosee that the rading of the file and if statements not working.
int Z = I + 1;  // <---  Never used.
int stuff = 6;  // <--- Never used.
int six = 6;
int zero = 0;
int twentyThree = 0;  //  <--- Used, but not a good name.
int getname = 1;  //  <--- Al 3 used, but not the best use for a subscript.
int getprice = 1;
int getquant = 1;
snacksDoubleName temp[3]; //  <--- defined as an array of 3, but not large enough. And never used.
snacksJustString mabob[5];
string stringy[23];  //  <--- Never used.
string stringy2[5];  //  <--- Never used.
string junkString;  //  <--- Not the best use of this variable.
snacksJustString thing;  //  <--- Never Used.
fstream doneDone;  //  <--- Not the best name. "file" or "ioFile"would be more descriptive. 


Next the for loop and if statements look like the most difficult way to read the file. The use of doneDone >> a variable does not always work the way you might be thinking doneDone >> mabob[getname].name; works fine when the name is "cola", but when the name should be "Root Beer" it will only read "Root". Leaving the next read which should be doneDone >> mabob[getprice].m_price; trying to put "Beer" into a numeric variable which caused the input stream to fail and any future reads to the file to fail because the stream was never reset. This means that price and quantity never receive good information from the file and the return at the end of the function has bad information.

I do not know what you have learned yet or what you are allowed to use with this program. a couple of thoughts is to use a std::map with a std::vector to work in parallel the map or just a std::vector to store a complete class or struct that can be passed around the program.

The first thing I would suggest is to adjust the data file and separate the fielsd or variables with a "," to make it easier to read the file. Like: Root Beer,0.75,20. This way "Root Beer;" can be read as one whole name and price and quantity can be read. The "$" is not needed in the data file just the number.

Hope that helps,

Andy
Last edited on
Topic archived. No new replies allowed.