Wednesday, January 6, 2010

Give input to a variable

We can change the value of a variable while the program is running without edit the syntax by make the program to ask the user to input any number or letter.
It is easy to do by using cin>>.

See the example below:

#include “iostream.h”
#include “conio.h”
main()
{
int x;
cout<< ”Insert a number: ”;
cin>> x;
cout<<” The number you inserted is: ” << x;
getch();
}

See the picture below as the result:


With cin>>; The program will ask the user to insert a number, and the program will fill the value inserted by user to the variable, then print it to screen because of cout<< x syntax. So the number shown on the screen is depend on the number inserted by user.
We can use the same way to a char variable. Just change int x; become char x;


0 comments:

Post a Comment