C equivalent of "cin.get()"

This is a discussion on C equivalent of "cin.get()" within the C Programming forums, part of the General Programming Boards category; Hello... This is my first post on this forums. I have been learning a bit of C++ in the past ...

  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
    21,589
    Read: Pause console.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 07:28 AM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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

Page 1 of 2 12 LastLast
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, 07: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, 10:31 PM
  5. Replies: 0
    Last Post: 11-01-2002, 10:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21