Thread: Populating structure through a switch statement?

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

    Question Populating structure through a switch statement?

    Hi,

    Ok I've got two programs here. The first one is simply asking for an employee's name and age and storing them in an array of structures, then displaying them. Works fine, woop-de-do...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct EMPLOYEE
    {
    	char name[45];
    	int age;
    };
    
    void get_name (struct EMPLOYEE person []);
    void disp_name (struct EMPLOYEE person []);
    
    void main ()
    {
    	struct EMPLOYEE person [5];
    
    	get_name (person , 5);
    	disp_name (person , 5);
    	getch ();
    }
    void get_name (struct EMPLOYEE person [])
    {
    	int i = 0;
    
    	for (i = 0; i < 5; i ++)
    	{
    	printf ("Enter name: \n");
    	gets (person[i].name);
    	printf ("Enter age: \n");
    	scanf ("%d" , &person[i].age);
    	fflush (stdin);
    	}
    }
    void disp_name (struct EMPLOYEE person [])
    {
    	int i = 0;
    
    	for (i = 0; i < 5; i ++)
    	{
    	printf ("Name is: %s \n" ,person[i].name );
    	printf ("Age is: %d \n" , person[i].age);
    	}
    }
    Now the second program, which is just a more worked up version is giving me a headache. I've taken the above example and 'tried' to get some user selection into it. The problem is the loop in the get_name function just runs and runs. It's supposed to populate an array of 5 structures! I'm thinking it's something to do with the switch statement, not passing the structure correctly?. Any advice would be really appreciated at this point.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct EMPLOYEE
    {
    	char name[45];
    	int age;
    };
    
    int get_selection ();
    void use_selection (int selection , struct EMPLOYEE person []);
    void get_name (struct EMPLOYEE person []);
    void disp_name (struct EMPLOYEE person []);
    
    void main ()
    {
    	struct EMPLOYEE person [5];
    	int selection = 0;
    
    	selection = get_selection ();
    	use_selection (selection ,  person);
    
    	getch ();
    }
    int get_selection ()
    {
    	int selection = 0;
    
    	printf ("*** Main menu ***\n");
    	printf ("1.Add employee\n2.Display all employees\n3.Quit");
    	printf ("Enter choice: ");
    
    	do
    	{
    		scanf ("%d" , &selection);
    		fflush (stdin);
    
    		if ((selection < 1) || (selection > 5))
    		{
    			printf ("Invalid selection. Please re-enter: ");
    		}
    	}while((selection < 1) || (selection > 5));
    	return (selection);
    }
    void use_selection (int selection , struct EMPLOYEE person[])
    {
    	do
    	{
    		switch (selection)
    		{
    		case 1 :
    			get_name (person , 5);
    			break;
    		case 2 :
    			disp_name (person , 5);
    			break;
    		case 3 :
    			printf ("Exiting");
    			break;
    		}
    		}while(selection != 3);
    }
    void get_name (struct EMPLOYEE person [])
    {
    	int i = 0;
    
    	for (i = 0; i < 5; i ++)
    	{
    	printf ("Enter name: \n");
    	gets (person[i].name);
    	printf ("Enter age: \n");
    	scanf ("%d" , &person[i].age);
    	fflush (stdin);
    	}
    }
    void disp_name (struct EMPLOYEE person [])
    {
    	int i = 0;
    
    	for (i = 0; i < 5; i ++)
    	{
    	printf ("Name is: %s \n" ,person[i].name );
    	printf ("Age is: %d \n" , person[i].age);
    	}
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    It's because of do while loop in use_selection() function.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    8
    Indeed it was! Thanks for the help.

  4. #4
    Registered User jimtuv's Avatar
    Join Date
    May 2010
    Location
    Sylvania, Ohio
    Posts
    94
    You should get an error here

    Code:
    			get_name (person , 5);
    get_name doesn't have that many parameters.

    Code:
    void get_name (struct EMPLOYEE person []);
    Also you should read this

    Cprogramming.com FAQ > Why fflush(stdin) is wrong

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I don't know how you actually been able to compile that. You defined your get_name function like this:

    Code:
    void get_name (struct EMPLOYEE person []);
    but you call it like this:

    Code:
    get_name (person , 5);

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is not all that's wrong in the code!
    http://cpwiki.sf.net/void_main
    http://cpwiki.sf.net/gets
    So much wrong in one program. Where did you learn this stuff?
    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.

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

    Question

    Right, well following your suggestions I've tidied up the code a bit (yes gets will be tweaked too)

    As for 'where did you learn this stuff?' - I'm a beginner with maybe 3 spare hrs a week to study. Thought I was doing ok for 6 weeks practice? I'm sure you didn't wake up one day and all the knowledge was just sat there fizzing in your grey matter Elysia.

    And for my next annoying question... After populating the array of structures I return to the main menu (get_selection) function. My issue is the program terminates whatever the input. So say I enter 2 - display all employees, the program ends. Again, I cant pick out why this is happening. Any thoughts?



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct EMPLOYEE
    {
    	char name[20];
    	int age;
    };
    
    int get_selection ();
    void use_selection (int selection , struct EMPLOYEE person []);
    void get_name (struct EMPLOYEE person []);
    void disp_name (struct EMPLOYEE person []);
    
    int main (void)
    {
    	struct EMPLOYEE person [5] = {"Enter name",0};
    	int selection = 0;
    
    	selection = get_selection ();
    	use_selection (selection ,  person);
    
    	
    	return 0;
    }
    int get_selection ()
    {
    	int selection = 0;
    	system ("cls");
    	
    	printf ("*** Main menu ***\n");
    	printf ("1.Add employee\n2.Display all employees\n3.Quit\n");
    	printf ("Enter choice: ");
    	do
    	{
    		scanf_s ("%d" , &selection);
    		fflush (stdin);
    
    		if ((selection < 1) || (selection > 5))
    		{
    			printf ("Invalid selection. Please re-enter: ");
    		}
    	}while((selection < 1) || (selection > 5));
    	return (selection);
    }
    void use_selection (int selection , struct EMPLOYEE person[])
    {
    		switch (selection)
    		{
    		case 1 :
    			get_name (person);
    			break;
    		case 2 :
    			disp_name (person);
    			break;
    		case 3 :
    			printf ("Exiting");
    			break;
    		}
    }
    void get_name (struct EMPLOYEE person [])
    {	
    	int selection = 0;
    	int i = 0;
    	for (i = 0; i < 5; i++)
    	{
    		system("cls");
    		printf ("Enter employee %d's name: \n" , i+1);
    		do
    		{
    			gets (person[i].name);
    			if (strlen (person[i].name)> 20)
    			{
    				printf ("Name entered is too long. Please re-enter: ");
    			}
    		}while (strlen (person[i].name)> 20);
    			
    		printf ("Enter employee %d's age: \n" , i+1);
    		do
    		{
    			scanf_s ("%d" , &person[i].age);
    			fflush (stdin);
    			if ((person[i].age < 1) || (person[i].age > 70))
    			{
    				printf ("Invalid entry (Uk retiremnt age is 70) Please re-enter: ");
    			}
    		}while ((person[i].age < 1) || (person[i].age > 70));
    	}
    	 get_selection ();
    }
    
    void disp_name (struct EMPLOYEE person [])
    {
    	int i = 0;
    
    	for (i = 0; i < 5; i ++)
    	{
    	printf ("Name is: %s \n" ,person[i].name );
    	printf ("Age is: %d \n" , person[i].age);
    	}
    }

  8. #8
    Registered User jimtuv's Avatar
    Join Date
    May 2010
    Location
    Sylvania, Ohio
    Posts
    94
    beyond the very dangerous gets();
    or the use of fflush(stdin);
    both really bad ideas

    you should get errors on compiling this. scanf_s is an undefined reference. I think you ment to say scanf("%d",&selection); this is twice in the code once in get_selection the other in get_name


    Code:
    scanf_s ("%d" , &selection);
    		fflush (stdin);
    What compiler are you using???

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    8
    I'm using visual studio 2010 - perhaps it's just very forgiving. Interesting you say that though. scanf gives the warning - warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead.
    scanf_s doesn't hence it's use.

  10. #10
    Registered User jimtuv's Avatar
    Join Date
    May 2010
    Location
    Sylvania, Ohio
    Posts
    94
    That explains alot. I kinda figured it might be VS. Your right that scanf can be dangerous, its because the input can exceed the size of the array it is being put into. MS added scanf_s so not all compilers will handle that.

    That's why I started using fgets for most input.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Lee121 View Post
    As for 'where did you learn this stuff?' - I'm a beginner with maybe 3 spare hrs a week to study. Thought I was doing ok for 6 weeks practice? I'm sure you didn't wake up one day and all the knowledge was just sat there fizzing in your grey matter Elysia.
    I meant where you had been reading or hearing. Tutorials? Books? You've learned 3 bad things already in your short time, so it's a good guess that whatever you're reading contains a lot of bad things and we want to steer you away from that to write safe, standard compliant, portable code.
    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. Getting the switch statement to work.
    By mtymightymike in forum C Programming
    Replies: 7
    Last Post: 10-15-2008, 06:32 PM
  2. Stack operations from switch statement elements
    By mlsrar in forum C Programming
    Replies: 15
    Last Post: 10-02-2008, 01:12 PM
  3. switch statement
    By guillermoh in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 02:17 PM
  4. switch case statement
    By stanlvw in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 05:06 AM
  5. Replies: 17
    Last Post: 11-11-2007, 01:30 PM