Console Programming in C++

  • Thread starter Thread starter Robert Shull
  • Start date Start date
R

Robert Shull

I am taking a class in C++. We are creating console
applications using Visual C++.NET 2002. The assignment is
to create a "mailing label" using information coded into
the program. My code looks like this:

#include <iostream>

using std::cout;
using std::endl;

void main(void)
{
cout<<"Joe Schmoe"<<endl;
cout<<"5991 Crestview Ave"<<endl;
cout<<"Fairfield, OH 45014"
}//end of main

When I run this program the console appears and then
disappears before I can see the output. My instructor
suggested adding a getch() at the end of main, but the
compiler notes that there is an undeclared identifier.
How can I correct this problem with getch? Is there
another way to keep the console open until the user hits
a key?

Thank you,

Robert
 
Robert Shull said:
When I run this program the console appears and then
disappears before I can see the output. My instructor
suggested adding a getch() at the end of main, but the
compiler notes that there is an undeclared identifier.

If you would rather not have to add the extra lines to your program you can
run it at the command line. That is, first run CMD.EXE to get a process that
runs the command interpreter. Once there type the full path to your console
application. CMD.EXE will start your process pointing it to the existing
console window.

A tip from my friend Doug H. is to add the command prompt as an external
tool to the IDE from the menu: Tools->External Tools->Add. You can use a
title of 'Prompt', a command of 'c:\winnt\system32\cmd.exe' and an initial
directory of $(TargetDir). Then to run your program you can just type its
name with neither path nor extension.

Regards,
Will
 
Or just use Ctrl-F5 in which case VS.NET pauses the console and waits till
you press a key before closing it

--
Regards,
Nish [VC++ MVP]
 
Thanks for your help. This solved the problem.

The other solution I had encountered was to use:

system("pause");

Robert
 
Back
Top