I am getting some memory errors and don't understand.

I am taking C++ for the first time and using Visual Studio to compile the programs. Thus far I have been able to get a grasp of the errors that are occuring in my previous programs. With this one I am at a loss can someone please help me with the following errors in the below program:

First-chance exception at 0x753ad36f in Week 7 Assignment.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0012f8c0..
Unhandled exception at 0x753ad36f in Week 7 Assignment.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0012f8c0..
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.


Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.


First-chance exception at 0x753ad36f in Week 7 Assignment.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0012f8c0..
The thread 'Win32 Thread' (0x1448) has exited with code -1073741749 (0xc000004b).
The program '[5840] Week 7 Assignment.exe: Native' has exited with code -1073741510 (0xc000013a).


Here is the code that I have been completed thus far:

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
const int NUM_CITIES=5, NUM_DESCRIPTORS=16;
string city[NUM_CITIES]={"San Fransisco", "Berkeley", "Palo Alto",
"Santa Cruz", "San Jose"};
string descriptor[NUM_DESCRIPTORS]={"mild", "weak", "slow", "moderate",
"medium", "tempered", "strong",
"powerful", "sharp", "violent",
"destructive", "extreme", "feeble",
"firm", "jolting", "devastating"};
int main ()
{
int city_index=0, descriptor_index=0, index_at_pound=0,
current_index=0, i, j, k;
int tally [4][NUM_CITIES];
string entire_file, anecdote;
ifstream infile ("D:\\EQREPT.TXT");

getline (infile, entire_file, '&');

index_at_pound = entire_file.find ('#', current_index);
anecdote = entire_file.substr(current_index,
index_at_pound-current_index);

while (entire_file.at (index_at_pound + 1) != '&')
{
while (anecdote.find (city[city_index]) == -1)
city_index++;
while (anecdote.find (descriptor[descriptor_index]) == -1)
descriptor_index++;

if (0 <= descriptor_index&&descriptor_index <= 2)
{
tally[0][city_index]++;
}
else if (3 <= descriptor_index&&descriptor_index
<= 5)
{
tally[1][city_index]++;
}
else if (6 <= descriptor_index&&descriptor_index
<= 8)
{
tally[2][city_index]++;
}
else if (9 <= descriptor_index&&descriptor_index
<= 11)
{
tally[3][city_index]++;
}

current_index = index_at_pound + 1;
index_at_pound = entire_file.find ('#', current_index);
anecdote = entire_file.substr (current_index,
index_at_pound - current_index);
}
cout<<"Final tally of Modified Mercalli intensities:";
cout<<"Modified";
cout<<"Mercalli";
cout<<"Intensity San Fransisco Berkeley Palo Alto Santa Cruz San Jose"<<endl;

for (i=4, j=0, i<=10; i+2, j++;)
{
cout<<setw(8)<<i;
for (k=0; k<5; k++)
{
cout<<setw(15)<<tally[j][k];
}
cout<<endl;
}
system("pause");
}

The input file EQREPT.TXT
San Fransisco.
I felt strong shock followed by rolling waves.
It lasted a long time.#
Berkely.
It was mild shaking.
Gave a jolting rattling windows.
It frightened my dog.#
Palo Alto.
The shaking was very violent and devastating.#
Santa Cruz.
The earthquake was very destructive.
It knocked down the supposed firm
chimney on my house.#
Palo Alto.
The extreme feeble shaking made me feel like I was
on a boat in rough sea.#
San Fransisco.
I slept right through it. It was much weaker than
our last earthquake.#




Any assistance would be greatly appreciated, I think I am very close and just need some assistance getting over the hump.
Last edited on
You should reset city_index and descriptor_index back to zero at the beginning of the loop.
I am struggling, this is the first time I have had to reset a variables and was wondering if you wouldn't mind giving me an example so that I have a reference to work with. I would greatly appreciate it.
city_index = 0; //reset to 0
When I mean "reset", I mean simply assign zero to it again.

e.g.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
while (entire_file.at (index_at_pound + 1) != '&')
{
     //resetting city_index and descriptor_index, so as to
     //start searching at the beginning of the arrays

     city_index = 0;
     descriptor_index = 0;

     //find city and descriptor (added brackets to loops for clarity)

     while (anecdote.find (city[city_index]) == -1)
     {
          city_index++;
     }

     while (anecdote.find (descriptor[descriptor_index]) == -1)
     {
          descriptor_index++;
     }

     //the rest of the loop
     ...
}

Thank you very much, now with this code:

while (entire_file.at (index_at_pound + 1) != '&')
{
city_index = 0;
descriptor_index = 0;
while (anecdote.find (city[city_index]) == -1)
city_index++;
while (anecdote.find (descriptor[descriptor_index]) == -1)
descriptor_index++;

if (0 <= descriptor_index&&descriptor_index <= 2)
{
tally[0][city_index]++;
}
else if (3 <= descriptor_index&&descriptor_index
<= 5)
{
tally[1][city_index]++;
}
else if (6 <= descriptor_index&&descriptor_index
<= 8)
{
tally[2][city_index]++;
}
else if (9 <= descriptor_index&&descriptor_index
<= 11)
{
tally[3][city_index]++;
}

current_index = index_at_pound + 1;
index_at_pound = entire_file.find ('#', current_index);
anecdote = entire_file.substr (current_index,
index_at_pound - current_index);

I am getting "Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention." As well as "First-chance exception at 0x753ad36f in Week 7.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0012f6e0..
Unhandled exception at 0x753ad36f in Week 7.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0012f6e0.."

I am sorry if it is something small that I am missing and I appreciate all the assistance.
Last edited on
That means you are exiting normally. As in, no problems.
But it isn't displaying what I am asking it to, I am new to this and it is becoming very frustrating. I updated with the new errors. I just realized that I hit to not continue debugging and that is why I recieved the normal exit.
Last edited on
Check out the first loop here:

1
2
3
4
5
6
7
8
9
for (i=4, j=0, i<=10; i+2, j++;) // <- this one does not do what you want it to
{
     cout<<setw(8)<<i;
     for (k=0; k<5; k++)
     {
          cout<<setw(15)<<tally[j][k];
     }
     cout<<endl;
}


The loop compiles but it does not do what you expect. (i is never incremented so there is an infinite loop. It stops because j goes out-of-bounds of the tally array).
Last edited on
Ok I thought I saw the error in my ways, but am still getting the same error. Here is the updated code.

for (i = 4, j = 0; i <= 10; i += 2, j ++)
{
cout<<setw(8)<<i;
for (k=0; k<5; k++)
{
cout<<setw(15)<<tally[j][k];
}
cout<<endl;
<10, not <=10.

I'd suggest using exclusively the canonical for loop, instead of coming up with creative uses for it, at least until you've gotten the hang of it:
for (T i=0;i<n;i++)
I know that I am stuck on the loop and I am going with this since it was referenced in the book. I am having difficulty finding any other options and I know that the memory errors are coming from the i never getting incremented. I think it has something to do with the input file that I referenced above. I am trying to get a grasp on the loops and am stuck. I am not a great programmer I try to modify something that was already out there and the code that I am modifying from was broke to begin with. Instead of starting over I am hoping that this forum will be able to assist me in getting over the hump. I appreciate the assistance.
I am having difficulty finding any other options and I know that the memory errors are coming from the i never getting incremented

Actually, looking at your current implementation of the loop, there shouldn't be any memory errors coming from it.

1
2
3
4
5
6
7
8
9
10
11
for (i = 4, j = 0; i <= 10; i += 2, j ++)
{
     cout<<setw(8)<<i;

     for (k=0; k<5; k++)
     {
          cout<<setw(15)<<tally[j][k];
     }

     cout<<endl;
}


In the loop, i goes from 4 to 10 inclusively in increments of 2, therefore the loop goes 4 times. The 4 times is important because the first dimension of tally has a length of 4. The second loop goes 5 times which is also good because the second dimension has a length of 5 (though you may consider changing 5 to NUM_CITIES in this case).

Another thing I see is that int tally [4][NUM_CITIES]; is never initialized. Maybe this is the source of the error? You should set each element in tally to zero in a loop after declaring it.
Topic archived. No new replies allowed.