Pausing Screen After Compiling...

Started by
16 comments, last by Soccerman4 20 years, 9 months ago
I''m guessing getch() wouldn''t work in an if statement...or something with user input. How would you pause the screen then?
I exist.
Advertisement
There is the getchar() function. I''m not sure what''s the difference with getch(), but here is a code snippet from the Visual C++ documentation


-----------------------------------------------------------/* This program uses getchar to read a single line * of input from stdin, places this input in buffer, then * terminates the string before printing it to the screen. */#include <stdio.h>void main( void ){   char buffer[81];   int i, ch;   printf( "Enter a line: " );   /* Read in single line from "stdin": */   for( i = 0; (i < 80) &&  ((ch = getchar()) != EOF)                         && (ch != ''\n''); i++ )      buffer[i] = (char)ch;   /* Terminate string with null character: */   buffer[i] = ''\0'';   printf( "%s\n", buffer );}------------------------------------------------------OutputEnter a line: This is a testThis is a test

Everything is better with Metal.

You mean to tell me that this question still isn''t in the FAQ?!




You fight like a dairy farmer.

--{You fight like a dairy farmer!}

oliii - getch() is not ANSI C standard and getchar() is, although they are both similar in the way they assume that stdin and stdout are the standard input devices but getch() is a non buffered function and does not echo the input characters on screen as it receives them.

You can use getchar() in the exact same way that i used getch() above (and probably should since getch() is non standard).


Soccerman4 - If im understanding your question ... getch() and getchar() will work in an if statement and will also work if there is any kind of user input.

for instance ...

#include <conio.h> #include <iostream> using namespace std;int main(){		char name =0;	cout<<"Please enter your name here\n" << endl; if (name > 0) {	 name = getchar(); }	return 0;}

or ...
       #include <conio.h> #include <iostream> using namespace std;int main(){		char name[30];	cout<<"Please enter your name here\n" << endl;	gets(name); // stores input from stdin into the string	getchar();	return 0;}


work just fine.

Edit: I probably shouldnt have used getch() because its non standard and i keep getting into trouble on these forums for posting non standard code, in this case getchar() is the better choice purley because its ANSI C standard.

[edited by - Tiffany Smith on July 5, 2003 3:12:32 PM]
An ASCII tetris clone... | AsciiRis
If you were to use that method wouldn''t it cause the program to end immedietaly after you entered your name? Thus causing the if statement intended to end before getting your response. Is there a way to keep the screen up without ending it with a keystroke?
I exist.
quote:Original post by Soccerman4
If you were to use that method wouldn''t it cause the program to end immedietaly after you entered your name?

In the first example yes thats why i posted the example using gets().
quote:Original post by Soccerman4
Thus causing the if statement intended to end before getting your response.

No that wouldnt happen.
quote:Original post by Soccerman4
Is there a way to keep the screen up without ending it with a keystroke?

yes but im unsure
An ASCII tetris clone... | AsciiRis
quote:Original post by Soccerman4
Is there a way to keep the screen up without ending it with a keystroke?
Use older IDEs that run in DOS such as Borland Turbo C++ 3.0.
Yeah, you could always just run it in the command prompt (comand line interface) ... goto ''Start'' > ''Run'' and type ''cmd'', then type the path to your program.

''cd'' to change directory.
''cd..'' to go back.
''dir'' to get a list of files and subdirectories.
''help'' to find out anything else you might wanna know.
An ASCII tetris clone... | AsciiRis

This topic is closed to new replies.

Advertisement