Thread: Access Violation

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    1

    Access Violation

    <<< Split from Access Violation >>>
    Hey mike65535,

    I'm doing exactly the same exercise as Kiera89, I was getting the same sort of errors until I restarted the compiler. After that it worked fine, so then I tried hacking apart the code to make it do different things.

    I know little or nothing about C which is why I'm struggling even with the basics.

    I'm trying to create a while loop which allows the user to input a letter to choose the shape that they want.

    This code is a practise for another .exe I need to create, where a user can walk around a house choosing a direction if that direction is available, this is a tester to see if I can figure out how to do that.

    Any help would be greatly appreciated.

    Cheers,

    Josh

    Code:
    #include <string.h>
    
    typedef struct{	//Define structure
    unsigned char Type[20];	//Allow names of up to 20 characters
    unsigned int Next, Previous;	//Declare integers to hold the IDs of 
    }Shape;
    
    Shape MyShape[3];//Declare 10 Shape structures
    
    void main(){
    	strcpy(MyShape[0].Type ,"Square");//Define a Square Shape
    	MyShape[0].Next=1;
    	MyShape[0].Previous=2;
    
    	strcpy(MyShape[1].Type ,"Triangle");//Define a Triangle Shape
    	MyShape[1].Next=2;
    	MyShape[1].Previous=0;
    
    	strcpy(MyShape[2].Type ,"Circle");//Define a Circle Shape
    	MyShape[2].Next=0;
    	MyShape[2].Previous=1;
    
    int n,p,c;
    char Type;
    
    while(1){			//Loop forever
    		p=MyShape[c].Previous;//Read current Shape's previous neighbour
    		n=MyShape[c].Next;//Read current Shape's next neighbour
    				Type = getch(); 
    					if(Type == 's') printf("Square\n");
    					if(Type == 't') printf("Triangle\n");
    					if(Type == 'c') printf("Circle\n"); 
    					if(Type == 'q') {
    									printf("Closing down Sale..\n");
    									break;}
    		printf("%s is your chosen shape", MyShape[c].Type);
    				//Print chosen shape
    	}
    }

  2. #2
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    Just a few things to get this code to compile:
    1. The main function by definition must return an int. You have defined your function as void, and you fail to return anything at the end of the program.
    2. In your struct definition, you should change "unsigned char" to simply "char".
    3. In order to use I/O routines, you need to #include the appropriate library.

    Once you get the program to compile properly, you should be able to better test your algorithm of moving around.

    Kevin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access Violation
    By Graham Aker in forum C Programming
    Replies: 100
    Last Post: 01-26-2009, 08:31 PM
  2. Why am I having an access violation?
    By ldb88 in forum C++ Programming
    Replies: 12
    Last Post: 06-12-2007, 12:27 PM
  3. DLL Access Violation?
    By durban in forum C++ Programming
    Replies: 12
    Last Post: 10-04-2005, 05:55 PM
  4. Access Violation under VC++ 6.0
    By xephyr in forum C Programming
    Replies: 4
    Last Post: 07-30-2004, 12:06 AM
  5. access violation!!
    By accviol in forum C Programming
    Replies: 4
    Last Post: 06-20-2003, 09:13 PM