C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-10-2009, 06:12 AM   #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?
ikislav is offline   Reply With Quote
Old 11-10-2009, 06:15 AM   #2
Registered User
 
Join Date: Oct 2009
Location: While(1)
Posts: 316
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
RockyMarrone is offline   Reply With Quote
Old 11-10-2009, 06:15 AM   #3
Robot
 
Join Date: Mar 2009
Posts: 100
getchar or getc(stdin)
Memloop is offline   Reply With Quote
Old 11-10-2009, 06:45 AM   #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)?
ikislav is offline   Reply With Quote
Old 11-10-2009, 06:48 AM   #5
Registered User
 
Join Date: Oct 2009
Location: While(1)
Posts: 316
yup getchar() is equivalent to getc(stdin) and it will return the int
RockyMarrone is offline   Reply With Quote
Old 11-10-2009, 07:19 AM   #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
ikislav is offline   Reply With Quote
Old 11-10-2009, 07:33 AM   #7
Registered User
 
kryptkat's Avatar
 
Join Date: Dec 2002
Posts: 409
try %d
meow.
kryptkat is offline   Reply With Quote
Old 11-10-2009, 07:51 AM   #8
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
Read: Pause console.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 11-10-2009, 08:11 AM   #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
ikislav is offline   Reply With Quote
Old 11-10-2009, 08:22 AM   #10
Registered User
 
Join Date: Sep 2006
Posts: 2,517
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.
Adak is offline   Reply With Quote
Old 11-10-2009, 08:29 AM   #11
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 11-10-2009, 08:33 AM   #12
Robot
 
Join Date: Mar 2009
Posts: 100
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.
Memloop is offline   Reply With Quote
Old 11-11-2009, 06:25 AM   #13
Registered User
 
Join Date: Nov 2009
Posts: 6
Is there any function I can use to empty the input buffer?
ikislav is offline   Reply With Quote
Old 11-11-2009, 06:33 AM   #14
Registered User
 
Join Date: Oct 2009
Location: While(1)
Posts: 316
flush
RockyMarrone is offline   Reply With Quote
Old 11-11-2009, 06:52 AM   #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

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

Cprogramming.com FAQ > Flush the input buffer
Brain_Child is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer equivalent to array notation bekkilyn C Programming 4 12-06-2006 08:22 PM
C++ Equivalent JPEG Functions? stickman C++ Programming 9 05-06-2006 10:50 AM
Convert char string to decimal equivalent (eg: a = chr(97))... Blueprint C Programming 10 08-17-2005 11:17 PM
Header File Question(s) AQWst C++ Programming 10 12-23-2004 11:31 PM
FAQ: Is there a getch() (from conio) equivalent on Linux/UNIX? kermi3 FAQ Board 0 11-01-2002 11:56 AM


All times are GMT -6. The time now is 03:45 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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