본문 바로가기
College Study/C++ (ENG)

[C++] Output (ENG)

by 2den 2021. 12. 5.
728x90

 

"Hello World"

std::cout << "Hello " << "World" << std::endl;
 

 

 

Namespace

namespace hello
{
    void SayHello();
}

namespace hi
{
    void SayHello();
}

// ...

hello::SayHello();
hi::SayHello();
 

 

More simple with using.

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello World" << endl;
    
    return 0;
}
 

 

 

Pragma

#pragma once
 

Instead of ifndef, define, and endif preprocessor. Include the header file only once.

 

 

 

<< Operator

With C++, the programmer can change the operation of the operator.

 

 

 

Output Formatting - Manipulator

 

(number == 123)

showpos / noshopos

cout << showpos << number;      // +123
cout << noshowpos << number;    // 123
 

dec / hex / oct

cout << dec << number;  // 123
cout << hex << number;  // 7b
cout << oct << numer;   // 173
 

uppercase / nouppercase

cout << uppercase << hex << number;     // 7B
cout << nouppercase << hex << number;   // 7b
 

showbase / noshowbase

cout << showbase << hex << number << endl;      // 0x7b
cout << noshowbase << hex << number << endl;    // 7b
 

left / internal / right

cout << setw(6) << left << number;      // -123
cout << setw(6) << internal << number;  // -   123
cout << setw(6) << right << number;     //    -123
 

(decimal1 == 100.0 , decimal2 == 100.12)

noshowpoint / showpoint

cout << noshowpoint << decimal1 << " " << decimal2; // 100 100.12
cout << showpoint << decimal1 << " " << decimal2;   // 100.000 100.120
 

(number == 123.456789)

fixed / scientific

cout << fixed << number;        // 123.456789
cout << scientific << number;   // 1.23456789E+02
 

(bReady == true)

boolalpha / noboolalpha

cout << boolalpha << bReady;    // true
cout << noboolalpha << bReady;  // 1
 

* Need #include <iomanip> when use the manipulators below.

(number == 123)

setw( )

cout << setw(5) << number;  //    123
 

setfill( )

cout << setfill('*') << setw(5) << number;  // **123
 

(number == 123.456789)

setprecision( )

cout << setprecision(7) << number;  // 123.4568
 

 

 

Output Formatting - cout Member Method

// manipulator
cout << showpos << number;

// cout member method
cout.setf(ios_base::showpos);
cout << number;
 
// manipulator
cout << setw(5) << number;

// cout member method
cout.width(5);
cout << number;
 

namespace (ios_base), setf( ), unsetf( ), width( ), fill( ), precision( )

 

728x90

'College Study > C++ (ENG)' 카테고리의 다른 글

[C++] File I/O (ENG)  (0) 2022.01.02
[C++] File I/O (ENG)  (0) 2022.01.02
[C++] String (ENG)  (0) 2021.12.05
[C++] New Concepts (ENG)  (0) 2021.12.05
[C++] Input (ENG)  (0) 2021.12.05

댓글