IC011 – C++ Output Statements

Feiyu BLOG 2023.3.31

Information Technology Education

Python Curriculum Teaching

Research Topics

Academic Level Examination

Python Program Design

C++ Informatics

……

IC011 - C++ Output StatementsIC011 - C++ Output Statements

1. cout Output Statement

The cout output statement is a common output statement in C++ language, consisting of two parts: cout and the output operator “<<

Format of cout statement: cout<<expression<<……

Function: Outputs the value of the expression

Example: After the midterm exam, Liangliang is preparing to report his grades to his mother.

Program to output his grades in various subjects.

Requirements: The first line should include the name and subject names, and the second line should include the name and subject scores. Please help Liangliang program to realize his idea.

Reference Program:

#include <iostream>
using namespace std;
int main(){
    string name="亮亮";
    float c=98,m=100,n=99;
    cout<<"姓名";
    cout<<" "<<"语文";
    cout<<" "<<"数学";
    cout<<" "<<"英语"<<endl;
    cout<<name;
    cout<<" "<<c;
    cout<<" "<<m;
    cout<<" "<<n;
    return 0;
} 

Running Result:

IC011 - C++ Output Statements

Characteristics of cout statement: When using cout to output data, the user does not need to set how the computer outputs the data type; the system automatically determines the type of the output data and outputs it accordingly.

Difference between outputting strings and variables: When outputting string constants, they must be enclosed in double quotes to distinguish them from variable names.

cout<<“data”; function outputs the string data.

cout<<name; outputs the content stored in the variable name to the screen.

Extension Improvement

1. Read the following program segment, think about the program running process, and fill in the final running result on the line below.

#include <iostream>
using namespace std;
int main(){
    cout<<"10+20=";
    cout<<30;
}

Running Result: _________________

2. Aunt Li plans to plant trees around a square field, requiring 10 trees on each side, with 1 tree planted at each corner. Write a program to calculate the total number of trees needed and output it.

2. printf Formatted Output

In C++ programs, if the output result is relatively complex and has formatting requirements, the printf() function is used to output the result.

Format of printf output function: printf(“format control”, output list);

Function: Outputs data according to the specified format.

For example: printf(“%d”,s); outputs the integer value of s

printf(“%.2f”,a); outputs the real number value of a, keeping two decimal places.

The output list is a group of data to be output, with parameters separated by “,”.

Practice Program:

#include <iostream>
using namespace std;
int main(){
    int b=126;
    printf("%7s %7s %7s \n","Decimal","Octal","Hexadecimal");
    printf("%7d %7o %7x \n",b,b,b);
    return 0;
}

Running Result:

IC011 - C++ Output Statements

printf Output Control Characters

In printf applications, “format control characters” consist of format control and ordinary characters. Format control consists of % and format characters, while ordinary characters are those that need to be output as they are. For example: printf(“a=%d”,a); the running result is: a=12

Common format characters are as follows:

%d Outputs the actual length of decimal integer data

%md m is the specified width of the output field

%c Used to output a character

%f Used to output real numbers, with the integer part fully output and the decimal part outputting 6 digits

%.mf When outputting real numbers, keeps m digits after the decimal point, note that there is a dot before m

%o Outputs in octal integer form

%x Outputs in hexadecimal integer form

Outputting Data on New Lines

When using cout<< statements to output multiple lines of data, you can add the statement <<endl to complete it. However, in printf output statements, the newline character \n is often used.

Extension Improvement

1. Read the following program segment, think about the program running process, and fill in the final running result on the line below.

#include <iostream>
#include"cstdio"
using namespace std;
int main(){
    int num=66;
    char ch='E';
    printf("num=%d\n",num);
    printf("ch=%c\n",ch);
    return 0;
}

Running Result: ____________

2. Programming Problem

Doctor Liu goes up the mountain to collect herbs. He walks 48 meters per minute and reaches the top in 18 minutes; on the way down, he returns along the same path, walking 74 meters per minute. Write a program to output Doctor Liu’s average speed going up and down the mountain (result kept to two decimal places).

IC011 - C++ Output Statements1.IC006 – C++ Expressions2.IC007 – C++ Data Types (Integer)3.IC008 – C++ Data Types (Real)4.IC009 – C++ Data Types (Character) / Type Casting5.IC010 – C++ Assignment OperationsIC011 - C++ Output Statements

Improve 1% Every Day, and after a year you can achieve37times growth; if you regress by 1% every day, after a year you will weaken, even regress to zero; a small change or a good habit can produce an amazing compounding effect!

Leave a Comment