Thread: C equivalent of "cin.get()"

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

    Question C equivalent of "cin.get()"

    Hello...

    This is my first post on this forums. I have been learning a bit of C++ in the past and got to the point where I was able to write a drawing program (Console drawing program, where you "draw" with characters. Program had a few different brushes, two layers, save and load capabilities). Now I want to suspend my learning of C++ and switch over to C and learn it well before I switch back to C++ and throw myself at OOP, classes and stuff. Reasons for this, no matter how illogical they seam, are my own

    My question is this:
    Since I am using <stdio.h> instead of <iostream>, I cant find an equivalent of cin.get() in stdio. I need it to stop the program from finishing... i.e. when I wrote Hello world program in C using stdio, upon its execution I saw a console window flashing for a millisecond without seeing the results of a program. Can anyone help?

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Ok First of all welcome to the forum

    Have nice learning exp of C/C++ here

    You can have getch on windows and you can just run your program in release mode

    not it debug it will show you without getch

    for this just press CTRL+F5 for debug building and running you do F5 just do CTRL+F5

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    getchar or getc(stdin)

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    Thank you, this really helps Few more questions: Is getchar() a part of standard library? Is getchar() going to interfere with my input if I use it in the middle of a program (i.e. like putting read character into some kind of buffer and then adding it to whatever scanf() reads)?

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    yup getchar() is equivalent to getc(stdin) and it will return the int

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    Ouch... my fears have come to pass... this is what I mean:

    Code:
    #include <stdio.h>
    
    int main() {
        int something;
        
        printf("blahblahblah!\n");
        getchar();
        scanf("%i", &something);
        printf("%i", something);
        getchar();
         
        return 0;
    }
    It doesn't work Program quits as soon as I input an integer

  7. #7
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    try %d
    meow.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Read: Pause console.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    @kryptkat -- I didn't quite understand what you meant by "try %d".
    @laserlight -- Nice article, thank you, helped me a lot

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by ikislav View Post
    @kryptkat -- I didn't quite understand what you meant by "try %d".
    @laserlight -- Nice article, thank you, helped me a lot
    %d is a common data type format for scanf, e.g. scanf("%d", &number)

    Check with your compiler and see if yours has any differences between %d and %i, for scanning in integers.

    I like the int or char variable = getchar() to hold the console window open. If it still closes, then I go medieval on it:

    Code:
    while((somevar = getchar()) != '\n');
    somevar = getchar();
    It isn't always needed, but it will stop a console window from closing.
    Last edited by Adak; 11-10-2009 at 08:28 AM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adak
    it is my understanding that %i can also be used for scanf(), to get an integer.
    The difference is that %d is used to read in the integer in base 10, but %i is used to read in the integer using the same rules as integer constants in C, e.g., a 0 prefix for octal and a 0x or 0X prefix for hexadecimal.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    scanf will also leave the newline in the input buffer so when you use getchar() directly after scanf it will just read the newline and quit instead of pausing like you want it to.

  13. #13
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    Is there any function I can use to empty the input buffer?

  14. #14
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    flush

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    Quote Originally Posted by RockyMarrone View Post
    flush
    cant use fflush to clear the input buffer

    Cprogramming.com FAQ > Why fflush(stdin) is wrong

    Is there any function I can use to empty the input buffer?
    see here

    Cprogramming.com FAQ > Flush the input buffer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer equivalent to array notation
    By bekkilyn in forum C Programming
    Replies: 4
    Last Post: 12-06-2006, 08:22 PM
  2. C++ Equivalent JPEG Functions?
    By stickman in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2006, 10:50 AM
  3. Replies: 10
    Last Post: 08-17-2005, 11:17 PM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. Replies: 0
    Last Post: 11-01-2002, 11:56 AM