Code Blows up Computer...

Can anyone explain to me what in this code is causing it to blow up? I think it's on line 38 but i'm not sure.
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
#include <iostream>
#include <string>
#include <iomanip>
#include <windows.h>
using namespace std;

int main(int argc, char const *argv[])
{
	int index, temp=9999;
	string salsa[5] = {"mild", "medium", "sweet", "hot", "zesty"};
	int jars[5];

	for (index = 0; index < 5; index++)
	{
		cout << "Please enter the number of jars of " << salsa[index] << " salsa sold in the past month: ";
		cin >> jars[index];
	}
    cout << endl;
	cout << "================================================================================" << endl;
	cout << "=====================================REPORT=====================================" << endl;
	cout << "================================================================================" << endl;
	cout << endl;

	cout << left << setw(40) << "Salsa" << right << setw(40) << "# sold" << endl;
	for (index = 0; index!=5; index++)
	{
		cout << left << setw(40) << salsa[index] << right << setw(40) << jars[index] << endl;
	}

	cout << endl << left << setw(40) << "Total # sold:" << right << setw(40) << jars[0]+jars[1]+jars[2]+jars[3]+jars[4] << endl;

	for(index = 0; index < 5; index++)
    {
        if(jars[index]>temp)
        	temp=jars[index];
    }

    cout << endl << left << setw(40) << "Highest selling product:" << setw(40) << salsa[index] << endl;

    for(index = 0; index < 5; index++)
    {
        if(jars[index]<temp)
        	temp=jars[index];
    }

    ///cout << endl << left << setw(40) << "Lowest selling product:" << right << setw(40) << salsa[index] << endl;
    system("pause");
	return 0;
}
Last edited on
I think it's on line 38 but i'm not sure.

If you run the program with your debugger the debugger should be able to tell you exactly where it detected the problem and you should also be able to view the variables at the time of the crash.

The program has no errors at all but it explodes when I run it. Any Ideas? I am compiling with the GNU GCC Compiler on Code::Blocks with the GDB Debugger. I have never had any issues like this ever before.
Last edited on
Yes, run it with your debugger!
I didn't even have to debug it and Visual studio 2015 gave me whats wrong with it. I'll give you a tip.

1
2
if(jars[index]>temp)
        	temp=jars[index];


What are you comparing jars[index] with?
temp already has a starting value for comparison. It's just a sorting Algorithm.
When I compile it without using line 38 it works correctly. Can anyone tell me what is wrong with line 38?
It looks like you're accessing the array out of bounds.

Did you run the program with your debugger? The debugger is an essential tool that you need to learn to use if you really want to be a programmer. Set a break point before the line in question and then single step through the problem section watching your variables as you step.

When I run it I get the following crash message;

First-chance exception at 0x71fa1174 in Dummy.exe: 0xC0000005: Access violation reading location 0x012b8000.
Unhandled exception at 0x71fa1174 in Dummy.exe: 0xC0000005: Access violation reading location 0x012b8000.

Problem is in line
1
2
 cout << endl << left << setw(40) << "Highest selling product:" << setw(40) 
<< salsa[index] << endl;


You use the variable index outside the for loop, it has the value 5 and than of course salsa[index] accesses memory that you don't own.
> temp already has a starting value for comparison.
No, it doesn't.
`temp' is uninitialized.

> It's just a sorting Algorithm.
No, it isn't.
It is a maximum algorithm.

> You use the variable index outside the for loop,
Limit the scope of your variables.
`index' is only used to traverse the array, it should be limited to the loop
for(int index=0; index<5; ++index)
Topic archived. No new replies allowed.