Need some help

This is a discussion on Need some help within the C Programming forums, part of the General Programming Boards category; Alright, I can figure out why but when I run my program, it will display the menu function and when ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    Need some help

    Alright, I can figure out why but when I run my program, it will display the menu function and when I input 1 as my choice it does not call my "hire" function. Instead the menu just reprints and waits for my input again.

    Code:
    int main(void)
    {
    	char firstName[5][20]={' '};
    	char lastName[5][20]={' '};
    	char class[5][20];
    	int id[5]={0};
    	double pay[5]={0};
    	int numEmploy=0;
    	int choice=1;
    	
    	while(choice>=1 && choice<=6)
    	{
    	
    		menu();
    		scanf("%d", &choice);
    	
    		switch(choice)
    		{
    		case 1:
    			hire(firstName, id, &numEmploy);
    			break;
    		case 2:
    			//pay function here
    			break;
    		case 3:
    			//promote function here
    			break;
    		case 4:
    			//transfer function here
    			break;
    		case 5:
    			//print single emploee
    			break;
    		case 6:
    			//print all employees
    			break;
    		case 7:
    			printf("Exiting program\n");
    			break;
    		default:
    			printf("Invalid input\n");
    		}
    	}
    	
    return(ZEROI);	
    }
    Code:
    void hire(char firstName[][20], int id[], int *numEmploy)
    {
    	int ident, i;
    	if(numEmploy <= 4)
    	{
    		printf("Enter a 5 digit id: ");
    		scanf("%d", &ident);
    		for(i=ZEROI; i<MAX_EMPLOYEE; i++)
    			{
    			if(ident==id[i])
    			{
    			printf("There is already an employee with this id number\n");
    			//else if(
    			}
    			}	
    	}
    No the hire function is not finished. I just want to test what is there currently.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because 1 and '1' are not the same thing. You need to use the characters '1', '2', '3', etc. in your switch cases, rather than the numbers 1, 2, 3, etc.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Ah oh thanks

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Well, actually in other programs I've written I have uses int and it has worked fine. This switch looks exactly like it but it does not work for some reason.

    Changing the values to char didn't fix it either.
    Last edited by Fox101; 02-04-2008 at 02:56 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's because I misread %c for %d in your original code (how, I don't know); sorry. So yes, you'll want 1, 2, 3, etc. as you had it originally. Let me look again, more carefully this time.

    Edit: You'll need to compare *numEmployee <= 4, rather than numEmployee <= 4 in your hire function. Otherwise, it compiled and ran correctly here (after fiddling with some missing #defines and missing functions. Are you sure the error isn't in your menu() function?
    Last edited by tabstop; 02-04-2008 at 03:08 PM.

Popular pages Recent additions subscribe to a feed

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