การอ่านข้อมูลและแสดงผล

ตัวแปรที่ใช้

Streams

Remark

cin

input stream

cout

output stream

cerr

error stream

clog

logging stream

wcin

wide input stream

wcout

wide output stream

wcerr

wide error stream

wclog

wide logging stream

การแสดงผลผ่าน cout

 1#include <bits/stdc++.h>
 2
 3using namespace std;
 4
 5int main() {
 6
 7  auto x = 1234;
 8
 9  cout << "Hello world." << endl;
10  cout << u8"สวัสดีชาวโลก\n";
11  cout << "x = " << x << '\n';
12
13  auto t = true, f = false;
14  cout << "t = " << t << "  f = " << f << endl;
15  cout << "t = " << t << "  f = " << boolalpha << f << endl;
16  cout << "t = " << boolalpha << t << "  f = " << f << endl;
17
18  auto y = 0.1234567890;
19  auto name = "Paul";
20  cout << "name = [" << name << "]  y = [" << y << "]\n";
21  cout << "name = [" << setw(15) << name << "]  y = [" << setw(15) << y << "]\n";
22  cout << "name = [" << setw(15) << right << name << "]  y = [" << setw(15) << left << y << "]\n";
23  cout << "name = [" << setw(15) << right << name << "]  y = [" << setw(15) << left << setprecision(4) << y << "]\n";
24  cout << "name = [" << setw(15) << right << setfill('*') << name << "]  y = [" << setw(15) << left << setprecision(4) << showpos << y << "]\n";
25  cout << "name = [" << setw(15) << right << setfill('*') << name << "]  y = [" << scientific << y << "]\n";
26  cout << "name = [" << setw(15) << right << setfill('*') << name << "]  y = [" << fixed << y << "]\n";
27  cout << "name = [" << setw(15) << right << setfill('*') << name << "]  12 = [" << noshowpoint << 12.0 << "]\n";
28
29  x = 1234;
30  cout << "x = " << x << endl;
31  cout << "x = " << noshowpos << x << endl;
32  cout << "dec(x) = " << dec << x << endl;
33  cout << "oct(x) = " << oct << x << endl;
34  cout << "hex(x) = " << hex << x << endl;
35  cout << "showbase oct(x) = " << showbase << oct << x << endl;
36  cout << "showbase hex(x) = " << showbase << hex << x << endl;
37  cout << "showbase uppercase oct(x) = " << showbase << uppercase << oct << x << endl;
38  cout << "showbase hex(x) = " << showbase << hex << x << endl;
39
40  return 0;
41}

การรับข้อมูลผ่าน cin

 1#include <bits/stdc++.h>
 2
 3using namespace std;
 4
 5int main() {
 6
 7  int x;
 8  cout << "Enter x ";
 9  cin >> x;
10  cout << "x ยกกำลังสามคือ " << x*x*x << endl;
11
12  long a, b, c;
13  cout << "Enter a b c : ";
14  cin >> a >> b >> c;
15  cout << "a = " << a << "  b = " << b << "  c = " << c << endl;
16
17  float f, g;
18  cin >> f >> g;
19  cout << "f = " << f << "  g = " << g << endl;
20
21  
22  return 0;
23}

การใช้ cin กับ string

 1#include <bits/stdc++.h>
 2
 3using namespace std;
 4
 5int main() {
 6
 7  string line;
 8
 9  cout << "Enter a line: ";
10  getline(cin, line);
11  cout << line << endl;
12
13  cout << "Enter a line: ";
14  getline(cin, line);
15  cout << line << endl;
16  
17  return 0;
18}

การใช้ stringstream

 1#include <bits/stdc++.h>
 2
 3using namespace std;
 4
 5int main() {
 6
 7  string line;
 8
 9  string name;
10  int id;
11  float gpa;
12
13  cout << u8"กรอกชื่อ: ";
14  getline(cin, name);
15
16  cout << u8"กรอกรหัส: ";
17  getline(cin, line);
18  stringstream(line) >> id;
19  cout.flush();
20
21  cout << u8"กรอกเกรดเฉลี่ย: ";
22  getline(cin, line);
23  stringstream(line) >> gpa;
24  cout.flush();
25  
26  cout << id << " : " << name << " เกรดเฉลี่ย " << gpa << endl;
27
28  return 0;
29}

การแสดงผลด้วย printf

คำสั่งประกาศของ printf

int printf(const char *format, ... )

โดยที่ format เป็นข้อความที่สามารถนำค่าของตัวแปรมาแสดงผลได้ในต่ำแหน่งของ tag พิเศษ

รูปแบบของ tag พิเศษเป็นดังนี้

%[flags][width][.precision][length]specifier

โดยค่าที่ระบุไว้ใน [ ] ไม่จำเป็นต้องมีก็ได้ และแต่ละตำแหน่งมีความหมายและค่าที่เป็นไปได้ดังนี้

Possible Values

Meaning

flags

-

left justify

+

show + or - sign

(space)

blank space before the value.

#

o, x, X shows 0, 0x, 0X

e, E, f shows decimal points.

g, G shows decimal with trailing zeros.

width

(number)

Number of characters for display area.

*

Use preceding variable's value as width.

precision

(number)

d, i, o, u, x, X - minimum number of digits with padded zeros

. or .0 means no character printed if the value is zero.

e, E and f - number of digits after dot.

g, G - number of significant digits.

*

Use preceding variable's value as width.

length

h

i, d, o, u, x, X - print as short int for unsigned short int

l

i, d, o, u, x, X - print as long int for unsigned long int.

L

e, E, f, g, G - print as long double.

specifier

c

on character

d or i

signed decimal integer

e

scientific notation using e character

E

scientific notation using E character

f

floating point

g

uses the shorterof %e or %f

G

uses the shorterof %E or %f

o

signed octal

s

string

u

unsigned decimal integer

x

unsigned hexadecimal integer

X

unsigned hexadecimal integer capital

p

pointer address

n

nothing

%

print % character

 1#include <bits/stdc++.h>
 2
 3int main() {
 4
 5  char c = 'a';
 6  printf(u8"x มีค่า %c\n", c);
 7
 8  int d = 20;
 9  printf(u8"d มีค่า|%d|%o|%#x|%X|%+d|%+10.5d|\n", d, d, d, d, d, d);
10
11  double x = 1234567890;
12  printf("%15.5e %15.5f %15.5g %15.5G\n", x, x, x, x);
13
14  float f = 3.99;
15  float *ptr = &f;
16  printf("f=%f  ptr=%p  *ptr=%f \n", f, ptr, *ptr);
17
18  printf("%n-%%\n", &d);
19  return 0;
20}

การรับข้อมูลด้วย scanf

คำสั่งประกาศของ scanf

int printf(const char *format, ... )

โดยที่ format เป็นข้อความที่สามารถนำค่าของตัวแปรมาแสดงผลได้ในต่ำแหน่งของ tag พิเศษ

รูปแบบของ tag พิเศษเป็นดังนี้

%[*][width][length]type

โดยค่าที่ระบุไว้ใน [ ] ไม่จำเป็นต้องมีก็ได้ และแต่ละตำแหน่งมีความหมายและค่าที่เป็นไปได้ดังนี้

Possible Values

Variable Type

Meaning

*

*

read data but ignored

width

(number)

maximum number of characters to read

length

h

i, d, o, u, x, X - print as short int for unsigned short int

l

i, d, o, u, x, X - print as long int for unsigned long int.

L

e, E, f, g, G - print as long double.

type

c

char *

a single character

d

int *

integer

e, E, f, g, G

float *

floating point

o

int *

integer

s

char *

unsigned integer

u

unsigned int *

string of characters

x, X

int *

hexadecimal integer

 1#include <bits/stdc++.h>
 2
 3int main() {
 4
 5  char c = 'a';
 6  scanf("%c%*c", &c);
 7  printf(">> %c\n", c);
 8
 9  int d, o, x;
10  scanf("%d%o%x", &d, &o, &x);
11  printf(">> %15.5d|%15.5o|%15.5x\n", d, o, x);
12
13  unsigned int u;
14  scanf("%u", &u);
15  printf("%u\n", u);
16
17  return 0;
18}