Input and output to the console window

Here, you will learn how to input and output text and numbers to the console window.

	
#include <iostream>

using namespace std;

int number;

int main(){
	cout<<"Hello world, and welcome to learn.adsgames.net!\n";
	cout<<"Enter a number:";

	cin>>number;
	cout<<"You entered "<< number <<".\n";
	system("pause");
}
	
It should end up like this

What cin>> and cout<< mean are console in and console out.
cin>> tells the console to wait for the user to enter some text and press enter, then puts the input into the variable that is put beside it. For example:

	
cin>> number;
	
will put the user text into the integer "number"(if the text is a number).


Let's put this code to good use with a calculator application:

	
#include <iostream>

using namespace std;

int number1;
int number2;
int finalAnswer;

int main(){
    
    cout<< "Welcome to your calculator application!\n";
    cout<< "Enter your first number:";
    cin>> number1;
    cout<<"Enter your second number:";
    cin>> number2;
    
    finalAnswer=number1+number2;
    
    cout<< "The answer is: "<< finalAnswer <<".\n";
    system("pause");
}
	

A simple calculator application

You can change the + to subtraction, or anything else, to change what the calculator can do.
*Note integers cannot store decimal points, so division will be rounded to the whole number.




Next lesson(Logic)
Back to index