Thread: Error at if function?

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    6

    Error at if function?

    I'm new to C programming and as I was writing this program I ran into a couple of issues, the first of which was the program ignoring the second scanf function, that was fixed by adding a space before %c. The second problem that I ran into which I don't know how to fix is that whenever I enter Y into the second scanf function, the program exits and gives me an error.The other if commands work perfectly. I inserted a printf before the loop inside the if(a=='Y') just to check where the program is exiting and it didn't print it, seems like the issue is in the if command. Here's the code
    Code:
    #include<stdio.h>
    
    
    int main(){
        int i,b;
        char c[100],a;
        for(i=0;i<100;i++){
            printf("Please enter a character\n");
            scanf(" %c",&c[i]);
            if(c[i]=='0'){
                break;
            }
        }
        printf("If you want to print the entered characters type Y, otherwise type N\n");
        scanf(" %c",&a);
        if(a=='Y'){
            for(i=b;b>=0;b--){
                printf("%c and it's location in the array is %d\n",c[b],b);
            }
        }
        else if(a=='N'){
            printf("Goodbye!");
        }
        else{
            printf("Error");
        }
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2013
    Posts
    6
    An array of C[] is actually an adresse itself. Therefore you do not need the adresse operator "&"

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by AverageJoe55 View Post
    An array of C[] is actually an adresse itself. Therefore you do not need the adresse operator "&"
    incorrect. you need the address of the element. c[i] will give you the element itself, not its address. with scanf you still need the & with array elements.

    your problem is that b is used uninitialized. you need to turn up your warning level. with GCC, you should use command line options -Wall and -Wextra.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    6
    I just tried the code without the address operator "&". And it didn't work at all, anyhow that's not the issue here. Thank you anyways

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Using scanf with individual characters can be problematic for beginners. Input is line buffered and waits for a newline before processing. This newline character counts as a perfectly valid character that can be read/processed by a scanf call using a %c specifier. So, after the first call to scanf, the letter the user typed will be stored and there is a leftover newline sitting in the input buffer. The second call to scanf picks up this stray newline and then the program continues on. The end result to you is that is looks like every other scanf call is skipping for some reason. There are a few ways to deal with this issue, one of which you have discovered. Note that when using scanf with other format specifiers (%s, %d, etc...) any leftover newline characters in the input stream are ignored, this is a great source of confusion for beginners since this is a different observed behavior.

    The problem is not the "if" statement. What value does b have at the beginning of the for loop?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Nov 2013
    Posts
    6
    I'm sorry but I don't know how to turn up the warning level. And besides, in the for loop right after if(a=='Y') b is initialized as being equal to i. Second of all, it's not even reaching the loop, it's exiting as soon as I type Y, I've tried adding a printf function right before the loop and nothing got printed, which leads me to think that the error is in the if function

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    no. i is being initialized with the value of b, which is undefined at that point.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    Registered User
    Join Date
    Nov 2013
    Posts
    6
    Thank you! I just rewrote the code using a do while function, and I assigned b as being equal to i right before it breaks the for loop. I didn't know that i becomes undefined once the loop is broken. Thank you again

  9. #9
    Registered User
    Join Date
    Nov 2013
    Posts
    6
    Thank you That seemed to fix it. I didn't know that i becomes undefined once the loop is broken.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you don't need to change to a different type of loop. you simply need to initialize b to the value of i after your first loop, since that appears to be what you intended.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  11. #11
    Registered User
    Join Date
    Nov 2013
    Posts
    6
    Yeah I just rewrote it again to use the for loop. I also didn't know you could do the initialization outside of the for loop. My professor never mentioned that. Guess I should try and experiment more often.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Ibraheem Nofal View Post
    Guess I should try and experiment more often.
    experiment as much as possible. you'll find that your classroom instruction will be insufficient to learn everything you need to know about programming. independent study and self education are required in our field.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-23-2012, 02:37 PM
  2. Error "in function 'main' syntax error before 'int' Help Please
    By blackhat11907 in forum C Programming
    Replies: 5
    Last Post: 08-20-2011, 07:05 PM
  3. Replies: 8
    Last Post: 07-08-2011, 01:16 PM
  4. Error: _ defined as a function returning a function?
    By Jardon in forum C Programming
    Replies: 15
    Last Post: 07-29-2009, 11:53 AM
  5. function calling within another function error
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 01:40 AM

Tags for this Thread