954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Pausing program with getchar();

My compiler won't pause on getchar();. I have Dev C++ 4.9.9.2 and if I put getchar(); at the end of the program, it doesn't stop, and the console window still exits. It still exits when I assign a variable to it
c = getchar();
Is there another way to pause the program with C, without using system("PAUSE");

Firestone
Light Poster
37 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

The program will not stop if there are still keystroks in the keyboard buffer, such as if you previously called scanf() to get an integer the '\n' will still be in the keyboard buffer. But you will have to post your program for anyone to give you more help with this.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

>Is there another way to pause the program with C, without using system("PAUSE");
Sure. Don't run your program in an IDE that isn't smart enough to keep the window open. Run it from the command prompt.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

The problem arises when you are using different variables to read stuff, like ints and strings.

My advice would be to read everything in as a string and just convert to ints or floats where necessary.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

The problem arises when you don't know how C uses stream based input. You can mix input methods without any trouble if you know what happens, but most people just make function calls without knowing what's going on. The ideal fix is to learn how stream I/O works and learn what each input function does.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

That too but my way is best, safer (for newbies ) and works :)

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Its ok, I switched to a new IDE and it solved the problem. Thanks for the help guys.

Firestone
Light Poster
37 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

>but my way is best
No, your way promotes ignorance. Mine promotes learning.

>safer (for newbies )
Trading one mystery for another isn't safer.

>and works
Which is why I didn't immediately correct you.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

LOL

Which IDE did you change too. BTW I think you should try my idea cos I bet it works.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Pelles C. Its a good IDE, similar to Dev C++. I don't know how to convert from type to another anyways...

Firestone
Light Poster
37 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

You also know if you run your program from the command line you can see the output.

i.e start > run > cmd cd "path to exe" blah.exe ? If I recall.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Try to use _getch();

ayarosh
Newbie Poster
1 post since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

>Try to use _getch();
Bumping a three year old thread with bad advice. Awesome first post! :icon_rolleyes:

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You