CyberSlug Rules!

Beginner Programming CMPS060

Prof. Paulo Franca

Home

Syllabus

Login

Objects

Download

Staff only

Inputting data with cin

Input is considered an object which produces a stream of data. In order to read something you bring data from cin to your variables.

int x,float y;
cin>>x; // brings a value to x
cin>>x>>y; // brings first value to x, second to y

When dealing with arrays, you have to input each element, wich exception of character arrays which may be read with one shot:

int numbers[20];
char name[20];
for(int k=0;k<20;k++)
{
   cin>>numbers[k];
}
cin>>name;

Notes:

  • Input values are delimited by blanks. If you read a name containing blank spaces, each part will be considered a separate string (you can overcome this with manipulators).
  • Do not request a user to type several pieces of data at a time. It is a good idea to let the user know what data is expected before each typing.