Accessories for Data Input/Output


Exiting a Program

Many of the applications we have written so far stopped when the compiler found the return 0; line at the end of main(). In some circumstances it will be necessary to stop a program from a particular section if a bad or unexpected situation occurs. This is handled by using exit(). When called from anywhere, exit() closes everything from the program and stops it. The value you enter in the parentheses as an integer determines how successful the termination occurred. If you enter 0, the program terminated successfully. Any other value indicates that there was an error during the termination. Here is an example:

#include <iostream>
#include <iomanip>

using namespace std;
using namespace System;

int main()
    double Pop = 25.44059,
           Distance = 212.625,
           MaxExp = 709.78222656;

	Console::Write(L"Population: ");
	cout << setiosflags(ios::scientific) << setprecision(2);
 	cout << Pop << endl;


	Console::Write("Distance: ");
	cout << Distance << endl;
	Console::Write("Exponent: ");
	cout << MaxExp << endl;

	return 0;

This would produce:

Population: 2.54e+001
Press any key to continue . . .

Notice that the program doesn't get to the end; it stops where exit(0) is positioned. Besides exit(), you can use _exit() to stop the program. As opposed to exit() that closes everything from the program and exits, the _exit() function stops the program without closing the other parts. The value in the parentheses determines how the program terminates. If it is 0, the program would terminate successfully. Any other value indicates an error.

Aborting a Program

Sometimes a program terminates with a message box sent by the operating system. An example would be when a user is asked to provide a floating-point number but types a string and the program is supposed to multiply the provided value by another number. The program would stop or hang and display a message of abnormal termination. You can also create your own abnormal termination using abort(). When called, abort() terminates the program and sends an error message to the operating system. The OS uses different codes to identify these types of errors. Upon exiting, abort() terminates with an error code of 3. When the operating system receives it, it displays a message box.

You can click one of the buttons. If you click Ignore, the operating system can let you know that there was an abnormal termination.

Terminating a Program

If something bad happens in your program, the program is prepared to stop it. We will learn about some of these unusual but possible situations when studying exception handling. Nevertheless, if you suspect such a thing and would like to stop the program, you can use exit(), _exit(), or abort() we have just seen. Alternatively, you can stop a program using terminate(). You can call terminate() the same way we did with the exit() or abort().

Clearing the Screen

When an application is running (that is, a console application), for example while a user is performing data entry, the program processes one statement at a time in a top-down approach. When the user finishes, you will usually display the result on the screen. If the program is long, at one time it would fill out the screen and this could make the displayed result confusing with the data processing. One way you can approach this is to erase everything on the the screen. To do this, you can use system() and type "cls" in its parentheses.

system() is defined in the iostream.h header file of the std namespace. Here is an example:

#include <iostream>

using namespace std;
using namespace System;

public value class CHouse
	__wchar_t TypeOfHome;
	int NumberOfBedrooms;
	double NumberOfBathrooms;
	Byte Stories;
	int YearBuilt;
	double Value;

int main()
	CHouse ^ Townhouse = gcnew CHouse;

	Townhouse->YearBuilt = 1986;
	Townhouse->NumberOfBathrooms = 1.5;
	Townhouse->Stories = 3;
	Townhouse->Value = 348255;
	Townhouse->NumberOfBedrooms = 3;
	Townhouse->TypeOfHome = L'T';

	Console::WriteLine(L"Real Estate - Townhouse");
	Console::Write("Type of Home:        ");
	Console::Write("Number of Bedrooms:  ");
	Console::Write("Number of Bathrooms: ");
	Console::Write("Number of Stories:   ");
	Console::Write("Year Built:          ");
	Console::Write("Monetary Value:      ");

	Console::Write(L"Press Enter to view the next property . . .");


	CHouse ^ SingleFamily = gcnew CHouse;

	SingleFamily->YearBuilt = 2000;
	SingleFamily->NumberOfBathrooms = 3.5;
	SingleFamily->Stories = 3;
	SingleFamily->Value = 675855;
	SingleFamily->NumberOfBedrooms = 5;
	SingleFamily->TypeOfHome = L'S';
	Console::WriteLine(L"Real Estate - Single Family");
	Console::Write("Type of Home:        ");
	Console::Write("Number of Bedrooms:  ");
	Console::Write("Number of Bathrooms: ");
	Console::Write("Number of Stories:   ");
	Console::Write("Year Built:          ");
	Console::Write("Monetary Value:      ");

	return 0;

Here is an example of running the program:

Screen 1:

Real Estate - Townhouse
Type of Home:        T
Number of Bedrooms:  3
Number of Bathrooms: 1.5
Number of Stories:   3
Year Built:          1986
Monetary Value:      348255
Press Enter to view the next property . . .

Screen 2:

Real Estate - Single Family
Type of Home:        S
Number of Bedrooms:  5
Number of Bathrooms: 3.5
Number of Stories:   3
Year Built:          2000
Monetary Value:      675855

Press any key to continue . . .



Previous Copyright © 2006 FunctionX, Inc. Next