Basic Cpp: Input/Output

Input/Output di C++

didalam C++, input dan output operation lebih dikenal dengan stream

Input Stream, jika arahnya adalah dari device(keyboard) ke main memory

Output Stream, jika arahnya dari main memory ke device (monitor)

Header for Input/Output

Iostream, singkatan dari standard input output stream, contoh: cin, cout, cerr.

Iomanip, singkatan dari input output manipulators, contoh: setw,

fstream, singkatan dari file stream, baca tulis dengan file.

Cin dan Cout

Standard output stream (cout):

#include <iostream> 

using namespace std; 

int main( ) { 
char sample[] = "GeeksforGeeks"; 

cout << sample << " - A computer science portal for geeks"; 
  
return 0; 
} 

Output:

GeeksforGeeks - A computer science portal for geeks

Standard input stream(cin)

#include<iostream> 
using namespace std; 

int main() 
{ 
  int age; 
  
  cout << "Enter your age:"; 
  cin >> age; 
  cout << "\nYour age is: "<<age; 
  
  return 0; 
} 

Input : 18
Output:

Enter your age:
Your age is: 18

Scanf dan Printf

Format Specifiers:

Int (“%d”): 32 Bit integer

Long (“%ld”): 64 bit integer

Char (“%c”): Character type

Float (“%f”): 32 bit real value

Double (“%lf”): 64 bit real value

Contoh:

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    // Complete the code.
    int i;
    long l;
    char c;
    float fl;
    double d;
    scanf("%d %ld %c %f %lf", &i, &l, &c, &fl, &d);
    printf("%d \n", i);
    printf("%ld \n", l);
    printf("%c \n", c );
    printf("%f \n", fl);
    printf("%lf\n", d);
    return 0;
}