Topic : Input / Output Tutorial
Author : brian
Page : 1

Input / Output Tutorial
Writen by fatal

This tuturial is writen for beginer to the c++ language, first off to the

beginer of c++. The most common error is forgeting the semi-colin at the end of each line. But theirs an exception to this, when using loops( ie: for loop, do/while loop, & while loop) you dont put a semi-colin at the end ex:
for( x=1; x<8; x++);
{
cout<< x << endl;
}



If you add the semi-colin it will ignore the cout command and continue with the program. That the only time where you dont put the semi-colin.

Input

When you input an integer, char, or arrays. You will be using the cin command.
Syntax:
cin>> varible_name;

this will take the inputed data and store it to the varible. Arrays are

different story tho, to make an input to a char array, you have to use cin.get

and cin.ignore.

Syntax:
char array[21]; // Declare a char array

cin.get(array, 21); // we do this instead of cin because it can store everything

ex: fatal is god

cin.ignore(80, '\n'); // This is to clear the stream

Its better to use cin.get than cin, because cin will store everything up to a space, but cin.get will store everything you input like Jane Doe. cin would just sote Jane if you inputed Jane Doe.

Other than storing to arrays, just using cin for numbers and charactors will be fine.

Output:

To output all it is, is using cout.
Syntax:
cout<<"Hello World";

Say you want to output an interger:
Syntax:
int x = 8;

cout<< x << endl;

Or

cout<<"My Integer: " << x << endl;

Since outputing is very simple, thats all i will explain.

Puting it all together...

Heres a simple program using everything that I explained in this tutorial:

#include // Needed for the cin, cout, cin.get, and cin.ignore

int main() // Start of main function
{
int x;
char i;
char array[21];
char array2[21];

cout<<"Enter in a Integer: "; // Using cout to give user instructions
cin>> x; // Storing the inputed number to x

cout<<"The Integer Inputed is: " << x << endl; // Outputing what the user

inputed

cout<<"Enter a single charactor: "; // Asking user to input a charactor
cin>> i; // Storeing the charactor to i

cout<<"The Charactor Inputed is: " << i << end;// Outputing what was inputed

cout<<"Enter in your full name: ": // Asking user for their full name
cin>> arrray;// Storing full name to array

cout<<"Your Full Name is: " << array << endl;
// As you will notice it will only outpu // t everything up to the space

cout<<"Enter in your full name again: ";
cin.get( array2, 21); // Gets everythin that was inputed
cin.ignore(80, '\n'); // Clears stream

cout<<"You full name is: " << array2 << endl;
// Notice how you full name is outputed // this time


return 0; // Tells the complier that you program is done

}



Now I hope that cleared up your confusion on input/output. In my opinion this is

a great tutorial on input/output, and i hope it was good enough to help you.


Page : 1