ConsoleIO

=Console IO=

Every programming language has some kind of output and input to the console. Most programming languages have a format in common for input and output that has variable formatted into the line e.g. printf("%s is %d years old", name, age); The %s is replaced with name and the %d is replaced with age. 's' referring to a string and 'd' to an integer. These single character abbreviations is the important part in the function.


 * Character || data type || example output ||
 * %d || int, long, short, byte, char || 55 ||
 * %c || char || a ||
 * %s || *char, string || sample ||
 * %f || float, double || 5.55 ||
 * %e || float, double || 4.55e+5 ||
 * %p || *ptr || B800:0000 ||
 * %% || prints a percent || % ||

You can also set precision by adding a the max digits on the left side of the decimal, then a decimal followed by the max digits on the right. e.g: %4.2 for 3.141548 will print 3.14

Here are a few console IO methods I know off the top of my head.

Output
The main method of output in Java is through System.out. It allows a basic string output to the console, but one thing some people might not realize is that Java also has an error stream called System.err that can come in handy if you want your debug information to only output at certain times. The standard output method is println, but there is also printf for those who like formatted text.

code format="java" public class Main{ public static void main(String[] args){ System.out.println("Standard output"); System.err.println("Error output"); } } code

Input
This is where Java has options. They all read out of System.in but there are a few ways of interpreting the stream. You can go with streams or buffers, or maybe something I haven't heard of, either way you get the same result. I like to use the Scanner class it gets the job done, but some say its too slow, if this is you then go with a buffer.

code format="java" import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Scanner;

public class Main{ public static void main(String[] args) throws Exception{ //don't forget these classes throw exceptions //scanner input Scanner in = new Scanner(System.in); int x = in.nextInt;

//buffered input BufferedReader bfIn = new BufferedReader(new InputStreamReader(System.in)); String line = bfIn.readLine; } } code

In C++
The primary method of IO in C++ is through libraries. I only know of two, stdio.h and iostream.

Output
code format="cpp" using namespace std; //specify namespace because of iostream
 * 1) include
 * 2) include

int main{ char[] key = "world!"; //iostream method cout<<"Hello, "<
 * 2) include

int main{ int x;   //iostream method cout<<"Enter a whole number: "; cin>>x; //read

//stdio.h method printf("Enter another whole number: "); scanf("%d", &x); //read

return 0; } code