Thread: Odd ouput

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    38

    Odd ouput

    Why doesn't this work? If you run it you'll see the odd output. at first it asks me like normal then i put in a then it says enter letter 2 enter letter 3 on the same line and it doest it to the end. then it skips the last for and doesnt print them.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
            char alphabet[26],letters;
            int i;
    
            for(i=1; i<=26; i++)
            {
                    printf("Enter letter %d of the alphabet: ", i);
                    scanf("%c", &alphabet[letters]);
            }
            for(i=1; i<=26; i++)
                    printf("%c", alphabet[letters]);
    
            return 0;
    }

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    See the FAQ about scanf(), newlines, etc.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    letters - do you see anywhere in your code the variable to be initialized and modified?

    and do not forget that scanf &#37;c format leaves the \n character in the input stream
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    38
    Quote Originally Posted by vart View Post
    letters - do you see anywhere in your code the variable to be initialized and modified?

    and do not forget that scanf %c format leaves the \n character in the input stream
    Ok i'll check it out on the FAQ. but yes i initialize the variable at char letters and its modified every time scanf inserts a letter into it. no?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by me77 View Post
    Ok i'll check it out on the FAQ. but yes i initialize the variable at char letters and its modified every time scanf inserts a letter into it. no?
    char letters; does not initialize the variable.

    And you never read in anything into letters -- but rather alphabet[letters].

  6. #6
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Also, buffer overrun on your array alphabet. You have 26 elements, true, but you don't use element 0. Your for loop needs to go from 0 to 25, not 1 to 26. Array element 26 is out of bounds.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    38
    Quote Originally Posted by tabstop View Post
    char letters; does not initialize the variable.

    And you never read in anything into letters -- but rather alphabet[letters].
    so how would be the best way to put the letters into an array?

    also i cant seem to find the thread about the scanf in the faq thread would be great if someone could please link me.

  8. #8
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Remove the variable 'letters' entirely. Replace it with 'i' in your for loop.

    Code:
    scanf("&#37;c", &alphabet[i]);
    And also:
    Code:
    printf("%c", alphabet[i]);

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    38
    ok i did what you said but i still get this output

    Enter letter 1 of the alphabet: a
    Enter letter 2 of the alphabet: Enter letter 3 of the alphabet: b
    Enter letter 4 of the alphabet: Enter letter 5 of the alphabet:

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by me77 View Post
    ok i did what you said but i still get this output

    Enter letter 1 of the alphabet: a
    Enter letter 2 of the alphabet: Enter letter 3 of the alphabet: b
    Enter letter 4 of the alphabet: Enter letter 5 of the alphabet:
    The '%c' conversion operator to scanf() does not skip whitespace. Therefore the second call to scanf() is consuming the newline which is still sitting in the input buffer, instead of reading the character which follows it. This is the issue discussed in the FAQ.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    38
    oh i see now! thankyou.. i couldnt find the thread with that in it lol. anyways thanks for the simple explanation.
    so how would you it?

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    38
    let me rephrase that... is it possible to do this program without removing scanf. i know that i could just do "%c%c%c.. 26 times but thats not convenient.

  13. #13
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Possible, but not the best way.
    Code:
     
    for(i=1; i<=26; i++)
    {
         printf("Enter letter &#37;d of the alphabet: ", i);
         scanf("%c%c", &alphabet[letters], &enterkey);
    }
    Declare and use another char, "enterkey" (or whatever) that "eats up" the newlines.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    38
    lol i cant believe i didnt think of that lol.. its late here.. anyways thanks that should work for now just to get an understanding of how everything works.. since im just doing this as an exercise.

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    to eat up char just use &#37;*c format:
    Code:
    scanf("%c%*c", &alphabet[letters]);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding odd numbers only
    By CheyenneWay in forum C++ Programming
    Replies: 12
    Last Post: 05-06-2004, 12:22 AM
  2. new type: even or odd
    By nbo10 in forum C++ Programming
    Replies: 7
    Last Post: 09-05-2003, 11:17 AM
  3. Odd solution to an odd problem
    By Zeeshan in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 07-20-2002, 05:44 PM
  4. Replies: 4
    Last Post: 03-09-2002, 01:22 PM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM