Memory could not be written, help!

This is a discussion on Memory could not be written, help! within the C Programming forums, part of the General Programming Boards category; Hi, I'm writing a HR style program. The user is presented with a list of options to add employees details ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    8

    Question Memory could not be written, help!

    Hi, I'm writing a HR style program. The user is presented with a list of options to add employees details and sort them in various ways, simple stuff.

    Now I'm trying to populate the 'PERSON' structure in the program below. However I keep getting the error message: 'The memory could not be written' - Being a raw beginner I'm finding it hard to get my head around the syntax for using a structure in this context. Any ideas on where I'm going wrong?

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int get_selection ();
    void main_menu (int selection);
    void add_emp (struct PERSON aa []);
    void disp_emp (struct PERSON aa []);
    
    struct PERSON
    {
    	char name [31];
    };
    
    void main ()
    {
    	int selection = 0;
    	struct PERSON aa [2];
    
    	selection = get_selection ();
    	main_menu (selection);
    
    	add_emp ( aa );
    	disp_emp ( aa );
    	getch();
    }
    int get_selection ()
    {
    	int selection = 0;
    
    	printf ("1.Display all employees\n2.Add an employee");
    	printf ("\nEnter choice: ");
    	do
    	{
    		scanf ("%d" , &selection);
    		if ((selection < 1) || (selection > 2))
    		{
    			printf ("Invalid");
    		}
    	}while((selection < 1) || (selection > 2));
    		return (selection);
    }
    void main_menu (int selection , struct PERSON aa [])
    {
    	do
    	{
    		switch (selection)
    		{
    		case 1 :
    			disp_emp ( aa );
    			break;
    		case 2 :
    			add_emp ( aa );
    			break;
    		case 3 :
    			printf ("Exiting. Goodbye");
    			break;
    		}
    	}while (selection != 3);
    }
    void add_emp (struct PERSON aa [])
    {
    	int i = 0;
    	printf ("Enter name: \n");
    	for (i = 0; i < 2; i++)
    	{
    	gets (aa[i].name);
    	}
    }
    void disp_emp (struct PERSON aa [])
    {
    	int i = 0;
    	for (i = 0; i < 2; i++)
    	{
    	printf ("%s\n" , aa[i].name);
    	}
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Are you sure that you can compile it without any error(or warning?)
    You should prefer to use fgets() rather than gets() which is dangerous.
    And it's int main(void).
    Code:
    	
    main_menu (selection);  
    void main_menu (int selection , struct PERSON aa [])

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    8
    Yeah, it compiles with no error or even warning.
    fgets gave me this though:

    error C2198: 'fgets' : too few arguments for call Line 66 Column 1

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    fgets(aa[i].name, sizeof(aa[i].name),stdin);
    You may want to strip newline from fgets().
    fgets

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    The logic of the program also doesn't seem right. When you are adding employees you are adding all of them at once instead of adding just one. Is this in accordance with your requirements?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    8
    Still no luck Bayint. Same result. As for the logic - true. At the moment I'm trying to get my head around the syntax in a stripped down version, which seems a good idea given the problems I'm encountering already.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    This shouldn't compile.
    You have prototype:
    Code:
    void main_menu (int selection);
    And definition:
    Code:
    void main_menu (int selection , struct PERSON aa [])
    Fix it. Remove SourceForge.net: Gets - cpwiki. And fix void main.
    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.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    What compiler are you using? Do you recompile after making changes?
    Last edited by Bayint Naung; 06-13-2010 at 09:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Fragmentation with Dynamic FIFO Queue
    By fguy817817 in forum Linux Programming
    Replies: 17
    Last Post: 10-31-2009, 04:17 AM
  2. Memory usage and memory leaks
    By vsanandan in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 05:45 AM
  3. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  4. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM
  5. Accessing Video Memory Information...need help
    By KneeLess in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2003, 03:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21