Page 27 - Modular_V2.0_C++_Flikpbook
P. 27

By default, cout object prints output without line breaks. To insert a new line, a newline character
                 (\n) can be used. This is known as an escape sequence character. For example:

                  cout<<"I am Kittu.\n";
                  cout<<"I am 14 years old.\nI study in class 10.";
                 When you run the above code, it will show the output as shown below:

                  I am Kittu.
                  I am 14 years old.

                  I study in class 10.
                 Alternatively, you can use the endl manipulator to insert a line break.
                  cout<<"I am Kittu."<<endl;

                  cout<<"I am 14 years old."<<endl;

                    INPUT

                 The cin object is used to take input from the user through the keyboard. It is part of the iostream
                 library  and  works with the extraction  operator  (>>),  which is two  greater-than  signs. The
                 extraction operator is followed by the variable where the input value will be stored. For example:

                  int rollno;
                  cin>>rollno;
                 Here, the program waits for the user to enter a number. Once the user types a number and
                 presses Enter, the value is stored in the rollno variable. You can also take text input (characters
                 or words) from the user:

                  char s;
                  cin>>s;
                 The cin object can only take one word as input. If you enter multiple words, only the first word

                 will be stored, and the rest will be ignored after the first space. For example:
                  #include<iostream.h>

                  #include<conio.h>
                  void main()
                  {

                      clrscr();
                      char name[100];
                      cout<< "Enter your name: ";

                      cin>> name;
                      cout<< "Welcome " << name;                                    DOSBox 0.74, Cpu speed: max 100%
                      getch();                                                  Enter your name: Rohit Bali
                                                                                Welcome Rohit
                  }
                 Here, only "Rohit" is stored, while "Bali" is ignored because cin stops reading after the first space.


                                                                                                                  25
                                                                                          Input and Output in C++
   22   23   24   25   26   27   28   29   30   31   32