Thread: A Few Short Questions....

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    2

    A Few Short Questions....

    Code:
    #include <stdio.h>
    int main(void)
    {
        int in;
        
        printf("Please enter an ascii number to convert: ");
        scanf("%d\n", &in);
        printf("You entered %d, the regular value is %c\n", in, in);
        getchar();
        getchar();
        getchar();
        getchar();
        return 0;
    }
    The program is supposed to take the ascii number input and convert it to its regular character value. Example: inputting 90 would give you Z. Why do I have to enter 90 twice?, and why do I have to have so many "getchar()"'s to keep the window from closing before I can view the results? Thanks!

    -=Allen=-

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Removing the '\n' from your formatted input string should do the trick for your first question. As for the second one, I'm not sure, but compiling under Dev-C++ only required two.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    2
    wow. I all the sudden feel very small, thank you very much for your help and yeh it only requires that I use 2 "getchar()"'s after I did that. Why would having the "\n" cause the program to act like that in the first place though? (for general knowledge, and curiosity)

    Does having the "\n" require the program to actually start a newline before accepting the input?

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Using your old code, you would not actually have to enter the number twice. You could enter it once, and then press Return twice. What happens, is that you tell you program to input that variable, and then a new-line character. When it inputs the variable, it makes the user press enter before moving on. So it's almost as if it had to wait for 2 new line characters.

    As for your getchar() problem, you got me stumped, thought it may just be something about how that function operates (possibly a similar problem as the last one). I can't think of a solution off-hand though.

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Using your old code, you would not actually have to enter the number twice. You could enter it once, and then press Return twice. What happens, is that you tell you program to input that variable, and then a new-line character. When it inputs the variable, it makes the user press enter before moving on. So it's almost as if it had to wait for 2 new line characters.
    On my system you Actually still have to enter the number twice.

    as to the OP. you need to put extra getchar because of the way scanf works. This function takes a string - that is a sequence of chars - and formats it according to your conversion specifier. It stops on the first whitespace, and assigns the sequence to the address of the argument in this case - in.
    In the case of the second code
    Code:
    #include <stdio.h>
    int main(void)
    {
        int in;
        
        printf("Please enter an ascii number to convert: ");
        scanf("%d", &in);
        printf("You entered %d, the regular value is %c\n", in, in);
        getchar();
        getchar();
        return 0;
    }
    Once you've entered your input, your press enter to flush your input to stdout - your monitor - the '\n' is stored in stdin as part of your input and scanf reads that as part of your string and acts on it. subsequently you get a '\n' in your code, which satifies the getchar() function, which reads the next chr from stdin. which leads to program termination.
    So if you code getchar() twice, the first accepts the '\n' in the printf statement, the second waits for your input and then terminates.
    the general rule of thumb is not to use scanf, but instead fgets. Which i'm sure you'll come to soon.
    Last edited by caroundw5h; 11-14-2004 at 11:40 PM.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    132
    actually a quicker method is to include conio.h into your program and then just use the _getch() which only requires input of one character

    Regards,
    Tyouk

  7. #7
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by tyouk
    actually a quicker method is to include conio.h into your program and then just use the _getch() which only requires input of one character

    Regards,
    Tyouk
    the conio.h is not portable. If your learning ANSI C learn ANSI C. why complicate it.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing Structures Inside Structures
    By Mellowz in forum C Programming
    Replies: 1
    Last Post: 01-13-2008, 03:55 AM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  3. Say what? - Weird error.
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 08-15-2006, 11:54 PM
  4. Help with mult-dim arrays and pointers to them
    By skybolt_1 in forum C Programming
    Replies: 11
    Last Post: 05-02-2003, 11:47 AM
  5. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 09:17 AM