How to display a message on the screen. Output to standard error device. Other special characters

Team ECHO is probably one of the simplest and most used commands. Used to print text messages to standard output and to switch the display mode of commands on the screen.

Command line format:

ECHO- enable/disable the mode of displaying entered commands on the screen.

ECHO [message] display the message text on the screen.

Examples of using:

ECHO- when entering a command without parameters, the current command display mode is displayed:

ECHO mode is enabled.

To output an empty line, use the ECHO command with a dot:

echo Hello World!!!- message output Hello World!!! to the screen.

echo %USERNAME%- displaying the value of the USERNAME environment variable (current user name)

The most common use of the command is echo in command files. Almost any batch file begins with the command

@echo off- disable the mode of displaying commands on the screen. Symbol @ in front of the team echo is used to prevent the text of the command itself from being displayed on the screen.

Very often the team echo used to write messages to a text file using output redirection:

echo Getting started - %DATE% in %TIME% >logfile.txt

Echo ERORLEVEL=%ERRORLEVEL% >> logfile.txt

Echo End of work, %DATE% in %TIME% >>logfile.txt

To a text file logfile.txt The date and time the batch file started running, some of the results, and the end time are recorded.

Often, the team echo used to create a new file:

echo 1 >newfile.cmd- output the character "1" to a file newfile.cmd. If such a file does not exist, it will be created; if it exists, it will be overwritten.

The values ​​of command line parameters, variables and their substitution values ​​are also often displayed using the command echo. An example of a command file that displays information about itself:

@echo off
ECHO FILE PROCESSING - %0
ECHO Date/time the batch file was created/modified - %~t0
ECHO Batch file path - "%~f0"
ECHO Command file disk - %~d0
ECHO Batch file directory - "%~p0"
ECHO Batch file name - %~n0
ECHO Batch file extension - %~x0
ECHO Short name and extension - %~s0
ECHO Batch file attributes - %~a0
ECHO Batch file size - %~z0

When outputting service symbols interpreted by the command processor, you must use the symbol ^ . For example, instead of the value of the ERRORLEVEL variable, you need to display the text “%ERRORLEVEL%”

ECHO ^%ERRORLEVEL^% = %ERRORLEVEL%

A special feature of the ECHO command is the addition of carriage return and line feed characters. 0x0D And 0x0A(Carriage Return and Line Feed) to the end of the output text. The following batch file outputs 3 lines of text:

echo text1

echo text2

echo text3

If you want to display all the text on one line, you usually emulate the ECHO command with the SET command with the /P parameter, which is used to organize a dialogue with the user when a message is displayed that requires a response. The output message can be used in the same way as in the ECHO command, and input from a dummy device can be used instead of a response nul:

echo off

When running such a batch file, the message on the screen will be represented by one line:

text1 text2text3

To generate sound signals, you can use the output of the service symbol with code 07 (BELL). Simply including it in the output stream depends on the capabilities of the editor used to write the batch file. You can also use standard command line capabilities by adding the CTRL+G combination:

echo echo ^G > beepcmd.bat

After executing this command, a batch file will be created beepcmd.bat, which is output by the command ECHO service character 07, i.e. turns on the speaker beep. If necessary, it can be called in other batch files using the CALL command.

All the C++ programs you created in Lessons 1 and 2 used the output stream cout to display messages on the screen. In this tutorial you will use cout to output characters, integers such as 1001, and floating point numbers such as 0.12345. By the end of this lesson, you will have mastered the following core concepts:

  • To print characters and numbers to the screen you can use the output stream cout.
  • In C++ you can use it with cout special characters for tab or newline output and even for playing sound on your computer.
  • In C++, you can easily display numbers in decimal, octal (base 8), or hexadecimal (base 16) format.
  • By using redirection statements on the operating system command line, you can redirect your program's output messages to cout from screen to file or printer.
  • Using the output stream cerr, your programs can send messages to the standard error device, eliminating the need for users to redirect messages.
  • You can format your program's output using the modifier setw inside the output stream.

Almost every C++ program you create uses cout to display messages on the screen. In this tutorial you will learn how to better use som/.

USING cout TO OUTPUT NUMBERS

So far, the programs you have created have used som/ to output character strings(letters and numbers in quotation marks). Now you will learn that som/ can also be used to print numbers. The following program, 1001.CPP, displays the number 1001 on your screen:

#include

Void main(void)

{
cout<< 1001;
}

Compile and run this program. The number 1001 will be displayed on your screen as shown below:

C:\>1001

1001

Cout<< 2002;

In addition to display integers(numbers without a decimal point), som/also allows your programs to display floating point numbers, for example 1.2345. The following program FLOATING.CPP uses som/ to print the number 0.12345 to the screen:

#include

Void main(void)

{
cout<< 0.12345;
}

As before, compile and run this program. The following output will appear on your screen:

C:\>FLOATING

0.12345

OUTPUT SEVERAL VALUES AT THE SAME TIME

As you already know, the double less than sign is insert operation(this operation inserts characters into the output stream for display). By using cout you can use multiple insert operations within a single statement. For example, the following program, 1001TOO.CPP, uses this operation four times to display the number 1001 on your screen:

#include

Void main(void)

(
cout<< 1 << 0 << 0 << 1;
}

When you compile and run this program, the following will appear on your screen:

C:\>1001TOO

1001

Every time an insert operation is encountered in C++, the number or characters are simply added to those currently in the output stream. The following program SHOW1001.CPP using cout prints a character string and a number:

#include

Void main(void)

{
cout<< "Мое любимое число равно " << 1001;
}

Note that the space following the word equals(inside quotation marks), serves to separate the number 1001 from this word. Without a space, the number merges with the next word (equal to 1001). Similarly, the following program 1001MID.CPP displays the number 1001 in the middle of a character string:

#include

Void main(void)

{
cout<< "Number "<< 1001 << " мне очень нравится";
}

As before, pay attention to the placement of spaces before and after the number 1001.

Finally, the following MIXMATCH.CPP program combines strings, characters, integers, and floats within the same output stream:

#include

Void main(void)

{
cout<< "B " << 20 << " years my salary was "<< 493.34 << endl;
}

When you compile and run this program, the following output will appear on your screen:

C:\>MIXMATCH

At 20 years old my salary was 493.34

USING SPECIAL OUTPUT CHARACTERS

All the programs you have created so far have displayed their output as a single line. However, most programs you create later will display multiple lines of output. For example, suppose you are writing a program that will print addresses on the screen. You'll probably want the addresses to appear as multiple lines.

If you need to move the cursor to the beginning of the next line, you can place newline character(\n) to the output stream. In C++, you are given two different ways to generate a newline. First, you can put \n characters inside a character string. For example, the following program TWOLINES.CPP displays its output as two lines, using the newline character:

#include

Void main(void)
{
cout<< "Это строка один\nЭто строка два";
}

When you compile and run this program, the newline character will produce two lines of output, as shown below:

C:\>TWOLINES

This is line one

This is line two

If you are not outputting a character string, you can place a newline character inside single quotes. For example, the following program NEWLINES.CPP prints the numbers 1, 0, 0, and 1, each on its own line:

#include

Void main(void)
{
cout<< 1 << "\n"<< 0 << "\n"<< 0 << "\n" << 1;
}

In addition to using the newline character to advance the cursor to the beginning of the next line, your programs can use the character endl(end of line). The following program ENDL.CPP illustrates the use endl to move the cursor to the beginning of a new line:

#include

Void main(void)

{
cout<< "А теперь..." << endl
<< "Учимся программировать на языке C++";
}

As before, when you compile and run this program, the output of the program will be displayed on the screen in the form of two lines:

C:\>ENDL

And now

Learning to program in C++

Finally, the following program ADDRESS.CPP prints the address of the publisher "Jamsa Press" in a few lines:

#include

Void main(void)

{
cout<< "Jamsa Press" << endl;
cout<< "2975 South Rainbow, Suite I" << endl;
cout<< "Las Vegas, NV 89102" << endl;
}

Other special characters

In addition to the newline character, which allows your programs to advance the cursor to the beginning of a new line, you can use the special characters listed in Table. 3.1.

Table 3.1. Special characters for use with cout.

Symbol

Purpose

\A

Signal (or bell) symbol

\b

Return symbol

Page feed symbol

Newline character

\G

Carriage return (not line feed)

Horizontal tab character

Vertical tab character

Backslash character

Question mark

Single quotes

Double quotes

Null character

\000

Octal value, for example \007

\xhhhh

Hexadecimal value, such as \xFFFF

Comment: When using the special characters listed in table. 3.1, you should place them inside single quotes if you use these characters on their own, such as "\n", or inside double quotes if you use them inside a string, such as "Hi\nMup!".

The following program, SPECIAL.CPP, uses the special signal characters (\a) and tab (\t) to output audio to the computer's built-in speaker and then prints out the words Ring Ring Ring separated by tab:

#include

Void main(void)

{
cout<< "3вонок\a\tЗвонок\a\tЗвонок\a";
}

OUTPUT OCTAL AND HEXADECIMAL VALUES

The programs presented in this lesson so far have output numbers in decimal form. Depending on the purpose of your programs, you may need to output numbers in octal or hexadecimal. To do this, you can place modifiers dec, oct And hex inside the output stream. The following program OSTNEX.CPP uses these modifiers to output values ​​in decimal, octal, and hexadecimal:

#include

Void main(void)

{
cout<< "Восьмеричный: " << oct << 10 << " " << 20 << endl;
cout<< "Шестнадцатеричный: " << hex << 10 << " " << 20 << endl;
cout<< "Десятичный: " << dec << 10 << " " << 20 << endl;
}

When you compile and run this program, the following output will appear on your screen:

C:\>OCTEX

Octal: 12 24

Hexadecimal: a 14

Decimal: 10 20

Note: When you use one of the modifiers to select octal, hexadecimal, or decimal output, your selection will remain in effect until the program ends or you use another modifier.

OUTPUTING ERRORS TO A STANDARD DEVICE

As you already know, using cout You can redirect a program's output to a device or file using the operating system's output redirection operators. However, if your programs encounter error, you probably don't want the error message to be redirected from the screen. Redirecting error messages to a file can hide the fact that an error occurred from the user.

If your program needs to output an error message, you should use the output stream cerr. C++ links cerr with the standard operating system error device. The following program CERR.CPP uses the output stream cerr to display a message on the screen "This message always appears":

#include

Void main(void)

{
cerr<< "Это сообщение появляется всегда";
}

Compile and run this program. Next, try to redirect the program's output to a file using the output redirection operator:

C:\>CERR>FILENAME.EXT

Because the operating system will not allow your programs to redirect output written to the standard error device, a message will appear on your screen.

OUTPUT WIDTH CONTROL

Several previous programs displayed numbers on the screen. To ensure that these numbers were displayed correctly (with proper spacing), the programs included spaces before and after the numbers. When withdrawing to cout or cerr your programs can specify the output width of each number using a modifier setw(setting the width). By using setw programs specify the minimum number of characters a number can take up. For example, the following program SETW.CPP uses the modifier setw to select widths 3, 4, 5 and 6 for the number 1001. To use the modifier setw, your program must include a header file iomanip.h:

#include

#include

Void main (void)

{
cout<< "Мое любимое число равно" << setw(3) << 1001 << endl;
cout<< "Мое любимое число равно" << setw(4) << 1001 << endl;
cout<< "Мое любимое число равно" << setw(5) << 1001 << endl;
cout<< "Мое любимое число равно" << setw(6) << 1001 << endl;
}

When you compile and run this program, the following output will appear on your screen:

C:\>SETW

My favorite number is 1001

My favorite number is 1001

If you specify the width using setw, you indicate minimum the number of character positions occupied by a number. In the previous program the modifier setw(3) specified a minimum of three characters. However, since the number 1001 required more than three characters, cout used the actually required quantity, which in this case was four. It should be noted that when using setw to select the width, the specified width is valid for outputting only one number. If you need to specify the width for multiple numbers, you should use setw repeatedly.

Comment: The previous program uses the header file IOMANIP.H. You may need to print and examine the contents of this file now. As with the IOSTREAM.H header file, you will find this file inside the INCLUDE subdirectory of your compiler files directory.

WHAT YOU NEED TO KNOW

In this tutorial you learned several ways to use cout to display the output on the screen. All programs you create throughout the rest of this book will use cout to display the output. In Lesson 4, you'll learn how to use variables within your programs to store values ​​that can change as the program runs. However, before studying Lesson 4, make sure you have mastered the following basic concepts:

    1. Output stream cout allows you to output characters and numbers.
    2. By using special characters within the output stream, your program can indicate newline, tab, and other special features.
    3. To advance the cursor to the beginning of the next line, programs can create a new line using the \n character or the modifier endl.
    4. Modifiers Dec, Oct And hex Allow programs to output values ​​in decimal, octal, and hexadecimal.
    5. Using the output stream cerr, programs can write messages to the operating system's standard error device.
    6. Using a modifier setw your programs can control the width of the number output.

Instructions

To withdraw to screen message, in the Windows operating system it is enough to write a small script in Visual Basic. Create a text file script.txt. The name and location of the file do not matter. Open the file in any text editor. Enter the line: “MsgBox “message text”” (without the “herringbones”, but with quotes “dashes”). Save the file and close the text editor. Change the file extension to *.vbs. The icon should change. Now the operating system perceives this file not as a text file, but as a set of commands that is executed by the Visual Basic interpreter built into Windows. This component is present in all operating systems of this family, starting with XP, which allows you to run the script on any computer. When run, this script will output message in the window. The text in quotes can be anything.

An alternative to VBS script is to write Java Script. The Java language is more complex, but a script written in it can be run not only on Windows operating systems, but also on many others. Create a text file, open it in an editor and enter the line "WScript.Echo("message text");". There is no need to enter external quotes, but if you do not enter internal quotes (those that enclose the text), the script will not work. Change the file extension to *.js. Run the file for execution. As a result, a window will be displayed, exactly the same as when writing a program in Visual Basic.

There is another way to output messages - using the net send command. The advantage and distinctive feature of this method is that message can be sent to any computer on the local network. On your keyboard, press the win+r keys simultaneously, in the window that opens, enter the line cmd and press Enter. A command entry window will open. In it write "net send computer name message" (without quotes) and press Enter. The specified computer will display message almost the same as if it were done using a script on the local machine. Please note that it will include a return address. Instead of the name of the message recipient computer, you can specify an IP address.

The simplest program is a program for displaying text on the screen. Basically, most students get acquainted with programming languages ​​from it. It’s quite easy to write it yourself; the main thing is to understand the compiler interface and the basics of the programming language.

You will need

  • - Qt-SDK.

Instructions

Download any compiler program to your computer. It is best to use well-known programs, such as, for example, Nokia Qt SDK, since it has the most intuitive interface and will be more convenient for use by novice users. Download it from the official website and under no circumstances confuse it with Qt-Creator. Install the compiler on your computer and run it.

Choose to create a project called Hello World. To do this, click Create Project in the lower right corner and select the application type in the new window that appears. In Programming Languages, select C++. In the window that appears next, enter the name helloworld. Next, select the device if the program is not being developed for use on a personal computer.

Click Next twice, select the desired configuration in the last window and click OK. You will see a work field, open the editor, select main on the left side of the screen, enter the words QLabel label (Hello Everyboby) under the #endif line. You can use any phrase you like. Place a / at the end of the line.