Archived

This topic is now archived and is closed to further replies.

Soccerman4

Pausing Screen After Compiling...

Recommended Posts

After creating a program and running it the screen will flash up with the program really fast and then stop. I''ve heard about this problem before but I forgot how to deal with it. Oh by the way..I''m using the newest version of Dev-C++ as my compiler.

Share this post


Link to post
Share on other sites
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 );
}


------------------------------------------------------
Output

Enter a line: This is a test
This is a test

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites