Thread: strange error message

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    Question 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.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you post the code that calls add(), please.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    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;

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you should set counter to a value before you try adding names to you list?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    Thanks, I did it and it works now.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange number problem
    By kkjj in forum C Programming
    Replies: 9
    Last Post: 08-09-2007, 07:30 AM
  2. strange error -- COM DLL is not installed correctly?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 07-16-2007, 08:32 AM
  3. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  4. Very strange bug...
    By JaWiB in forum Tech Board
    Replies: 6
    Last Post: 04-27-2003, 01:56 PM
  5. bcc32 compiling error (really strange!!)
    By jester in forum C++ Programming
    Replies: 14
    Last Post: 01-26-2002, 04:00 PM