Better Input/Output Tutorial

Submitted on: 1/2/2015 6:48:00 AM
By: tehfatal (from psc cd)  
Level: Beginner
User Rating: By 4 Users
Compatibility: C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 1165
     Just a quick Article on Input/Output, since the last artical on input/output was done by a beginner. And sucked, sorry dude. Please take a second and vote on this example, thanks.

 / 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

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.


When you input an integer, char, or arrays. You will be using the cin command.
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.

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.


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

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

cout<< x << endl;


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 output 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.

Feed Your Complier

Other 1 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.