Program abort due to memory problem
I have no errors or warnings but my program keep cancelling anyway. The error message is this:
The instruction at "0x004053f0" referenced memory at "0x00000000". The memory could not be "read".
click ok to terminate the program or cancel to debug.
// this program has a problem with memory
// Purpose of this program:
//This program uses the selection sort algorithm to sort
// an array of names in alphabetical order.
void showArray(char *[][2], int);
void selectionSort(char *[][2]);
#include <iostream.h>
#include <string.h>
// Function prototypes
// this code not used void selectionSort(int [], int);
// this code not used void showArray(int [], int);
void main(void)
{
char *names[20][2] = {{"Collins, Bill"},
{"Smith, Bart"},
{"Allen, Jim"},
{"Griffin, Jim"},
{"Stamey, Marty"},
{"Rose, Geri"},
{"Taylor Terri"},
{"Johnson, Jill"},
{"Allison, Jeff"},
{"Looney, Joey"},
{"Wolfe, Bill"},
{"James, Jean"},
{"Weaver, Jim"},
{"Pore, Bob"},
{"Rutherford, Greg"},
{"Javens, Renee"},
{"Harrison, Rose"},
{"Setzer, Gathy"},
{"Pike, Gordon"},
{"Holland, Beth"} };
cout << "The unsorted names are: \n\n";
showArray(names, 20);
selectionSort(names);
cout << "The sorted names are\n";
showArray(names, 20);
}
//************************************************** ************
// Definition of function selectionSort. *
// This function performs an ascending order selection sort on *
// array. elems is the number of elements in the array. *
//************************************************** ************
void selectionSort(char *array[][2])
{
char sEarch[15];
int found = 0;
cout<<"\nEnter a String to sEarch : ";
cin >> sEarch;
cout << sEarch << endl;
for ( int i = 0 ; (i < 20 && !found); i++)
{
for ( int j = 0; (j < 2 && !found); j++)
{
cout<<endl << "Comparing " << array[i][j];
if (strcmp(sEarch,array[i][j])== 0)
{
found = 1;
break;
}
}
}
if (!found)
cout <<"\nString is NOT in the array!!!";
else
cout <<"\nString is in the array";
}
//************************************************** ************
// Definition of function showArray. *
// This function displays the contents of array. elems is the *
// number of elements. *
//************************************************** ************
void showArray(char *array[][2], int elems)
{
for (int i = 0; i < elems; i++)
{
for ( int j = 0 ; j < 2; j++)
cout << array[i][j]<<" ";
cout << endl;
}
cout<<endl<<endl;
}
Program abort due to memory problem
Yes, kooma. I removed all the 2's from every areas but the program aborted anyway with this error:
C:\Documents\Desktop\_#4.cpp(66) : error C2664: 'strcmp' : cannot
convert parameter 2 from 'char' to 'const char *'
error is pointing tpo this code:
if (strcmp(sEarch,array[i][j])== 0)
Looking for some more help.
More problems with 2 dim array
Your suggestion doesn't work.
I tried some different approach. The latest run cancel for a different reason. See the errors below:
/*
--------------------Configuration: page500_#4 - Win32 Debug--------------------
Compiling...
page500_#4.cpp
C:\Documents\STUDENT\Desktop\C++_tests\pg_#4.cpp(1 27) : error C2440: '=' : cannot convert from 'char *[17]' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Documents\STUDENT\Desktop\C++_tests\pg_#4.cpp(1 28) : error C2106: '=' : left operand must be l-value
C:\Documents\STUDENT\Desktop\C++_tests\pg_#4.cpp(1 29) : error C2440: '=' : cannot convert from 'char' to 'char *[17]'
There are no conversions to array types, although there are conversions to references or pointers to arrays
C:\Documents\STUDENT\Desktop\C++_tests\pg_#4.cpp(1 39) : error C2143: syntax error : missing ';' before '}'
C:\Documents\STUDENT\Desktop\C++_tests\pg_#4.cpp(1 39) : error C2143: syntax error : missing ';' before '}'
C:\Documents\STUDENT\Desktop\C++_tests\pg_#4.cpp(1 39) : error C2143: syntax error : missing ';' before '}'
Error executing cl.exe.
pg_#4.obj - 6 error(s), 0 warning(s)
*/