#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    While loop skipping


    Hi I have a very simple problem in the c code:

    The while loop is completely being skipped!

    Code:
    int main()
    
    
    
     
    
    
    
    {
    
    
    
     
    
    
    
    int dial=0;
    
    
    
     
    
    
    
    int temp=0;
    
    
    
     
    
    
    
    printf(“Enter the total no. of elements \n”);
    
    
    
     
    
    
    
    scanf(“%d”,&temp);
    
    
    
     
    
    
    
    printf(“%d \n”,temp); //to check
    
    
    
     
    
    
    
    char arr[temp];
    
    
    
     
    
    
    
    if((arr[dial])!=”NULL”)
    
    
    
     
    
    
    
    {
    
    
    
     
    
    
    
    while(arr[dial]!=”NULL”)
    
    
    
     
    
    
    
    {
    
    
    
     
    
    
    
    printf(“Enter the N elements one by one, with element# %d: \t”,dial+1);
    
    
    
     
    
    
    
    scanf(“%c”,&arr[dial]);
    
    
    
     
    
    
    
    dial++;
    
    
    
     
    
    
    
    printf(“\n”);
    
    
    
     
    
    
    
    }
    
    
    
     
    
    
    
    }
    
    
    
     
    
    
    
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,216
    Rep Power
    2222
    Crappiest ... code listing ... ever!

    Code:
    int main()
    {
        int dial=0;
        int temp=0;
    
        printf(“Enter the total no. of elements \n”);
        scanf(“%d”,&temp);
        printf(“%d \n”,temp); //to check
    
        char arr[temp];
    
        if((arr[dial])!=”NULL”)
        {
            while(arr[dial]!=”NULL”)
            {
                printf(“Enter the N elements one by one, with element# %d: \t”,dial+1);
                scanf(“%c”,&arr[dial]);
                dial++;
                printf(“\n”);
            }
        }
    }
    Now look at these lines:
    Code:
        if((arr[dial])!=”NULL”)
        {
            while(arr[dial]!=”NULL”)
    What are you trying to do? arr is a single C-style string. Since you have not initialized it, it contains garbage, which is to say whatever effectively random values just happened to have been left there. Never attempt to use uninitialized variables!

    Also arr[dial] would be a character, a single character. So then why are you trying to compare it with the address of a string literal? What do you think that you are trying to accompish? If you are going to compare a single character to anything, then it needs to a another single character!

    What did the compiler's warnings tell you? Don't tell me there weren't any warnings! The compiler had to squack and complain very loudly at the nonsense that you were feeding it! Don't tell me you were stupid enough to turn the warnings off. Or even more stupid enough to have ignored the warnings. Never ever ignore warnings! And never run a program that hasn't compiled cleanly -- meaning without any warnings. Because when a compiler issues warnings, it's because it's confused by the grave mistakes you made and, in its confusion, will guess at what you intended, which will almost never be what you did intend, and so generate code that will not do what you wanted it to do! Never ignore warnings! They are much more important than error messages!

    Even if you had attempted something like
    if (arr != "NULL")
    with the intention of testing whether arr contained a string that was different than "NULL", that would not do what you thought it did. All you would be doing would be to compare memory addresses, testing whether the literal "NULL" was located in the same place in memory as the arr array, which they are not. That means that you would never ever be able to leave that while loop, which is an infinite loop problem, not your stated "it ignored the while". Though since you are testing a character against the address of a string, who knows what the crazy code generated by your poor compiler could actually be doing?

    Your code is terminally messed up. Turn warnings on, read the warnings, and make the corrections needed.

    PS
    And the final statement in your main function needs to return an int. Try return 0;, which indicates that the program had run successfully and did not encounter any run-time errors.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Thumbs up


    What compiler are you using, may I ask? I would think that most compilers wouldn't even let you compile.

    One of your problems is that you used "NULL" (a string literal) instead of NULL (a constant). Some compilers support this by default while others may not.

IMN logo majestic logo threadwatch logo seochat tools logo