Aller au contenu

Input and Output

Standard Output and Input

In C++, the standard output, standard input, and standard error streams are represented by the std::cout, std::cin, and std::cerr objects, respectively. Also, newline characters can be outputted using the std::endl keyword. You use the << operator to write to standard output (or to any ostream, for that matter).

Standard Output/Input in Embedded Systems

Embedded systems often offer different ways of logging information on the console. For this reason, embedded programs do not use mechanisms such as std::cout or std::cin for standard output or input, but they rather use dedicated libraries.

example

#include <iostream>

int main() {
    std::cout << "Hello world!" << std::endl << "My name is Toto." << std::endl;
    return 0;
}
#include <iostream>
#include <string>

int main() {
    std::string userInput = "";
    std::cout << "Say something : ";
    std::getline(std::cin, userInput);
    std::cout << "Echo : " << userInput << std::endl;

    int n = 0;                          // Declare the variable to hold input
    std::cout << "Please enter n: ";     // Prompt user
    std::cin >> n;                      // Read user input and store in n
    std::cout << "N value is : " << n << std::endl;

    if (std::cin.fail()) {
        std::cerr << "Input error!" << std::endl;
    }

    return 0;
}