![]() |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 6
| 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 | |
| | #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 | |
| | #3 |
| Robot Join Date: Mar 2009
Posts: 100
| getchar or getc(stdin) |
| Memloop is offline | |
| | #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 | |
| | #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 | |
| | #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;
} Program quits as soon as I input an integer |
| ikislav is offline | |
| | #7 |
| Registered User Join Date: Dec 2002
Posts: 409
| try %d meow. |
| kryptkat is offline | |
| | #8 |
| C++ Witch 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 | |
| | #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 | |
| | #10 | |
| Registered User Join Date: Sep 2006
Posts: 2,517
| Quote:
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(); Last edited by Adak; 11-10-2009 at 08:28 AM. | |
| Adak is offline | |
| | #11 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,368
| Quote:
__________________ 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 | |
| | #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 | |
| | #13 |
| Registered User Join Date: Nov 2009
Posts: 6
| Is there any function I can use to empty the input buffer? |
| ikislav is offline | |
| | #14 |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 316
| flush |
| RockyMarrone is offline | |
| | #15 | |
| Registered User Join Date: Feb 2009
Posts: 35
| cant use fflush to clear the input buffer Cprogramming.com FAQ > Why fflush(stdin) is wrong Quote:
Cprogramming.com FAQ > Flush the input buffer | |
| Brain_Child is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |