Skip to content

[C++] Tutorial(3): Data Input and Output

Last Updated on 2021-05-06 by Clay

No matter which programming language, we all need to be familiar with Data Input and Output. In C++, we can use the <iostream> header file to master the basic I/O operation.

iostream, which is the abbreviation of Input/Output Stream.


Standard Output (cout)

cout is the most common output method, which can print out data of many type, and use it with the << symbol.

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


int main() {
    int a = 5;
    float b = 3.3;
    char c = 'a';
    string d = "test";

    cout << "int:    " << a << endl;
    cout << "float:  " << b << endl;
    cout << "char:   " << c << endl;
    cout << "string: " << d << endl;

    return 0;
}



Output:

int:    5
float:  3.3
char:   a
string: test

Standard Input (cin)

Relative to cout , cin is standard input, usually in the form of inputting a string from the keyboard, and it needs to be used with symbols such as >>.

#include <iostream>
using namespace std;


int main() {
    // Settings 
    char name[100];

    // Input
    cout << "What's your name?" << endl;
    cin >> name;

    // Output
    cout << "\nYour name is: " << name;

    return 0;
}



Output:

What's your name?
Clay

Your name is: Clay

The message can be printed! However, if you challenge a little, you will find that if the entered name is blank, for example, John Wick, then the name will only print John.

To solve this problem, a part of the concept of loop may be mentioned, maybe I will leave it to later.


printf()

printf() also outputs the message to the screen, and also returns the "number of characters" transmitted. If you want to specify variables as output in printf(), you need to match the so-called format specifier.

The specifier tables that can be referred to are listed below:

SpecifierOutput
%d, %iSigned decimal integer
%uUnsigned decimal integer
%oUnsigned octal integer
%xUnsigned hexadecimal integer
%XUnsigned hexadecimal integer (uppercase)
%fDecimal floating point number (lower case)
%FDecimal floating point number (uppercase)
%eScientific notation (lower case)
%EScientific notation (uppercase)
%gUse the shortest form (%e or %f)
%GUse the shortest form (%E or %F)
%aHexadecimal floating point number (lower case)
%AHexadecimal floating point number (uppercase)
%ccharacter
%sString
%pPointer address
%%Write a single% sign

The following is a simple demonstration:

#include <iostream>
using namespace std;


int main() {
    printf("%%c: %c\n", 'C');
    printf("%%s: %s\n", "Today is a nice day.");
    printf("%%d: %d\n", 10);
    printf("%%f: %f\n", 22.31);
       
    return 0;
}



Output:

%c: C
%s: Today is a nice day.
%d: 10
%f: 22.310000

You can try to print something different.


scanf()

In contrast to printf(), scanf() takes user input and uses the & symbol to address the variable input type.

#include <iostream>
using namespace std;


int main() {
    // Setting
    int num;

    // Input
    printf("Enter a number: ");
    scanf("%d", &num);

    // Output
    printf("Your input is: %d", num);

    return 0;
}



Output:

Enter a number: 100
Your input is: 100

References


Read More

Tags:

Leave a Reply