Thread: if else loop

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

    if else loop

    Code:
    printf("Enter the employee's id number: ");
    	scanf("%d", &temp_id);
    	for(index = ZEROI; index < MAX_EMPLOYEE; index++)
    	{
    		if(temp_id == id[index])
    		{
    			for(loop = ZEROI; loop <= COLUMN; loop++)
    			{
    				class[index][loop] = NULL;
    				firstName[index][loop] = NULL;
    				lastName[index][loop] = NULL;
    				salary[index] = ZEROI;
    				id[index] = ZEROI;
    			}
    		}
    		else
    		{
    		printf("No employee with this id currently works here\n");
    		}
    	}
    This is part of a function that is supposed to check if the entered id exists, if it does then zero out all of values. If the id does not exist then print that no employees work there... Problem is, is that while it does zero out the values it is still printing out that no employees with that id works there. I cant figure out why it is still running the else statement if the if is executed.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Looks like you need a break statment

    Code:
    printf("Enter the employee's id number: ");
    	scanf("&#37;d", &temp_id);
    	for(index = ZEROI; index < MAX_EMPLOYEE; index++)
    	{
    		if(temp_id == id[index])
    		{
    			for(loop = ZEROI; loop <= COLUMN; loop++)
    			{
    				class[index][loop] = NULL;
    				firstName[index][loop] = NULL;
    				lastName[index][loop] = NULL;
    				salary[index] = ZEROI;
    				id[index] = ZEROI;
    			}
                        break;
    		}
    		else
    		{
    		printf("No employee with this id currently works here\n");
    		}
    	}
    SSHarish

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Try this and see what happens:
    Code:
    printf("No employee with id &#37;d currently works here, but %s with id %d does\n", 
                  temp_id, firstname[index], index );
    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    well the break; worked, but I don't see why its not just skipping of the else if the if() is executed.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think if you print the "index" as Todd suggested, it will be clear to you why it does that. And no, it's not quite as you think. It's a logical bug in your code, not in the compiler.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Can you show us how have u declared your varibled. You dont have to insert null on every element of your array. It not a 2D array. Or i could say that u dont want a 2D array to just store a one name. You might be looking for something like this

    Code:
    firstName[0] = '\0';
    And perhaps that applies for other variables as well.

    ssharish

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    so firstName[0] = '\0'; will null out all of the columns in the first row?

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by Fox101 View Post
    so firstName[0] = '\0'; will null out all of the columns in the first row?
    OK from what i understand is that u have a 2D array. Ok if that was the case yes it null out the first[index][loop].

    ssharish

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    My other problem was that the else was inside the for loop, so while it searched to see if the id was valid it printed the statement every time it was not. I fixed that issue.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Fox101 View Post
    My other problem was that the else was inside the for loop, so while it searched to see if the id was valid it printed the statement every time it was not. I fixed that issue.
    Yes, that's what I was hinting at - but me telling you that wouldn't teach you how to debug your code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Code:
    {
    	int index;
    	int loop;
    	int temp_id;
    	int valid_id = FALSE;
    	
    	printf("Enter the employee's id number: ");
    	scanf("%d", &temp_id);
    	for(index = ZEROI; index < MAX_EMPLOYEE; index++)
    	{
    		if(temp_id == id[index])
    		{
    			valid_id = TRUE;
    			break;
    		}
    	}
    	
    	if(valid_id == TRUE)
    	{
    		for(loop = ZEROI; loop <= COLUMN; loop++)
    		{
    		class[index][loop] = NULL;
    		firstName[index][loop] = NULL;
    		lastName[index][loop] = NULL;
    		}
    		salary[index] = ZEROI;
    		id[index] = ZEROI;
    		*numEmploy -= DECREMENT;
    		
    	}
    	else
    	{
    	printf("No employee with this id currently works here\n");
    	}
    	
    	
    }
    
    	
    }
    Well, i fixed some issues, but now it zeros out the id i input but it also NULLs the names and class of the array above the one that it is supposed to. Ive tried everything I can think of.

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I don't think you've posted enough code that we can figure it out. Without the rest of the code, I personally would have to start guessing, and I gave up on guessing solutions for software errors about 20 years ago.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    I get the warning that assignment makes integer from pointer without a cast. its on all 3 lines where it should be nulling out the strings. What does this warning mean?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It means that NULL is (still!) not the same as the character '\0'. Your class[][] is a char, so you need to assign it a char.

    Edit: Oh, and Todd, my hat says you're no fun.
    Last edited by tabstop; 02-06-2008 at 10:40 PM.

  15. #15
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by tabstop View Post
    Edit: Oh, and Todd, my hat says you're no fun.
    I can be a ton of fun! You should hear me try to sing. It's a riot.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM