Prev | Next |
Output using the iostream library (C++) | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
You can perform output to a monitor or a file using either the iostream or stdio libraries in C++. In C, you can only use the stdio library. #include <iostream> using namespace std; Variable cout refers to the standard output (usually the monitor). Binary operator << is used to write. You can write many different kinds of things, such as integers, characters and strings, and you can perform several writes in one statement. For example, int n = 2; char c = 'a'; cout << n << c;writes 2 and a, with no space between them. You can include null-terminated strings as well, as in cout << "Thank you for running me" << endl;where endl is just "\n". Some other operations using cout are as follows.
Variable cout is just one variable of type ofstream, used to print to a file.
|
Output using the stdio library (C or C++) | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Many experience C+++ programmers prefer the stdio library to the iostream library. To use this in a C++ program, include header file cstdio. #include <cstdio> using namespace std;To write in C style, you can use library functions printf and fprintf. The first parameter to printf is a format, a string that tells what the output should look like. Within the format are sequences that start with %, and that indicate that the value of the next parameter should be put here. For example int m = 3; int n = 5; printf("I have n = %i and m = %i\n", n, m);prints I have n = 5 and m = 3Here are some of the formats that are available.
|
Input using the iostream library (C++) | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
You can perform input from a keyboard or a file in either the C or C++ styles. (C style can be used in either C or C++ programs. C++ style can only be used in C++ programs.) Use the same header files as for output. Variable cin refers to the standard input (usually the keyboard). Binary operator >> is used to read. You can read many different kinds of things, such as integers, characters and strings, and you can perform several reads in one statement. For example, int n; char c; cin >> n >> c;reads an integer, and puts it into variable n, and then reads a character, and puts it into variable c. If you read a string (type char*) using >>, characters will be read up to a space or newline, and put into the string. Be sure that there is enough room in the arrray. If you read a character, you will get one character, except that white-space characters (blank, tab, newline) are skipped. Some other operations using cin are as follows.
|
Input using the stdio library (C or C++) | ||
---|---|---|
To read in C style, you can use library functions scanf and fscanf. Include header file cstdio to use these. The first parameter to scanf is a format, telling what kind of data to read. Usually, the format just has indications of types that are similar to the indications in output formats. For example, to read two integers m and n, you would write scanf("%i%i", &m, &n);Notice the need to use &. The parameters after the format are call-by-pointer. If you use format %s, then a string will be read up to a space or newline.
|
Prev | Next |