-
strange error message
Hello everyone, I am new here and am having a problem with a homework assignment. I am supposed to write a program creating two empty arrays, and have the user add information to them. The part I am having a problem with is as follows:
Code:
void add(string names[], int numbers[], int& counter)
{
counter++;
cout << "Enter the name: ";
cin >> names[counter];
cout << "Enter the CWID: ";
cin >> numbers[counter];
}
It compiles fine, but when I run the program Visual Studio gives me this error: "Unhandled exception at 0x10489a8c (msvcp80d.dll) in 456.exe: 0xC0000005: Access violation reading location 0x99ac8c80."
Frankly I have no Idea what this means lol. Could someone please inform me as to what I am doing wrong? I am sure it is obvious but I am pretty new to this stuff so any help would be greatly appreciated, thanks.
-
Can you post the code that calls add(), please.
--
Mats
-
sure
Code:
int main()
{
char x='z';
string names[100]={};
int numbers[100]={};
int counter;
message();
while(x!='q')
{
x=getcommand();
switch(x)
{
case'a':
case'A':
add(names, numbers, counter);
break;
case'i':
case'I':
displayi(names, numbers, counter);
break;
case'n':
case'N':
displayn(names, numbers, counter);
break;
case'q':
case'Q':
x='q';
break;
default:
cout << "Invalid Input" << endl;
}
}
return 0;
-
Perhaps you should set counter to a value before you try adding names to you list?
--
Mats
-
Thanks, I did it and it works now.
-
Code:
int main()
{
char x='z';
string names[100]={};
int numbers[100]={};
int counter;
message();
while(x!='q')
{
x=getcommand();
switch(x)
{
case'a':
case'A':
add(names, numbers, counter);
break;
case'i':
case'I':
displayi(names, numbers, counter);
break;
case'n':
case'N':
displayn(names, numbers, counter);
break;
case'q':
case'Q':
x='q';
break;
default:
cout << "Invalid Input" << endl;
}
}
return 0;
It's very important that you indent properly.
Btw, Visual Studio usually complains if you use an unitialized variable, like in this case. You should always initialize variables.