Thread: Question

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

    Question

    Code:
    void hire(char firstName[][20], char lastName[][20],char class[][20], int id[], int *numEmploy)
    {
    	int ident=ZEROI;
    	int index=ZEROI;
    	int valid_id;
    	if(*numEmploy >= MAX_EMPLOYEE)
    		printf("You can not hire anymore employees");
    	else
    	{
    		do
    		{
    			valid_id = TRUE;
    			printf("Enter ID: ");
    			scanf("%d", &ident);
    			for(index = ZEROI; index < MAX_EMPLOYEE; index++)
    			{
    				if(id[index] == ident)
    				{
    					printf("Invalid ID\n");
    					valid_id = FALSE;
    					break;
    				}
    			}
    			
    		} while(valid_id = FALSE);
    			
    	id[index]=ident;
    	printf("Enter first and last name: ");
    	scanf("%s%s", firstName[*numEmploy], lastName[*numEmploy]);
    	*numEmploy += 1;
    	}
    	
    }
    ZEROI=0 MAX_EMPLOYEE=5 *numEmploy=0, I am having issues storing the user input in id[]. I want it to store the value only if it is a unique id, and I want it to store in the next available space in the array. So the first time I run this function it should store it in id[0]. The next time in id[1], if it not the same as id[0]. My problem is I check to see if there is a copy but after the check, I cant get it to store the right value in the right space. I dunno if that makes sense...

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Fox101 View Post
    Code:
    void hire(char firstName[][20], char lastName[][20],char class[][20], int id[], int *numEmploy)
    {
    	int ident=ZEROI;
    	int index=ZEROI;
    	int valid_id;
    	if(*numEmploy >= MAX_EMPLOYEE)
    		printf("You can not hire anymore employees");
    	else
    	{
    		do
    		{
    			/*valid_id = TRUE;*/
    			printf("Enter ID: ");
    			scanf("&#37;d", &ident);
    			for(index = ZEROI; index < MAX_EMPLOYEE; index++)
    			{
    				if(id[index] == ident)
    				{
    					printf("Invalid ID\n");
    					valid_id = FALSE;
    					break;
    				}
    			}
    			
    		} while(valid_id == FALSE);
    			
    	id[index]=ident;
    	printf("Enter first and last name: ");
    	scanf("%s%s", firstName[*numEmploy], lastName[*numEmploy]);
    	*numEmploy += 1;
    	}
    	
    }
    ZEROI=0 MAX_EMPLOYEE=5 *numEmploy=0, I am having issues storing the user input in id[]. I want it to store the value only if it is a unique id, and I want it to store in the next available space in the array. So the first time I run this function it should store it in id[0]. The next time in id[1], if it not the same as id[0]. My problem is I check to see if there is a copy but after the check, I cant get it to store the right value in the right space. I dunno if that makes sense...
    Note the change in red, above.
    Last edited by Adak; 02-05-2008 at 04:40 PM.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Well, that was an error I did not see but my problem still remains. The problem is that index is incremented while checking to see if the ID is already in use. After the loop I want to set the value of id[lowest unused number here] to the value he user entered.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The variable "index" is why that's happening. You need to use a different variable for the array index name, or you need to reset the index variable.

    I'd just a different index variable name for id[index] = ...
    Code:
    for(i = 0; i < MAX_EMPLOYEE; index++)
    {
        if(id[i] == ident)
        {
           printf("Invalid ID\n");
           valid_id = FALSE;
           break;
        }
    }
    Last edited by Adak; 02-05-2008 at 04:47 PM.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    id[*numEmploy] is what I am gonna try. That way it will put the id number in the slot of the next employee.

    Edit* And it worked, cool beans.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM