Input Function/Statements in C++

  • Home
  • Input Function/Statements in C++
Shape Image One

INPUT FUNCTION/STATEMENTS IN C++

advanced divider

Following area the input functions/statement in C++.

  • cin STATEMENT
  • getchar() STATEMENT
  • getch() STATEMENT
  • getche() STATEMENT

gets() STATEMENT

cin STATEMENT

cin stands for “Character Input”. In C++, cin reads formatted data as input from keyboard.cin

object is used along with the extraction operator (>>) to accept data from standard input device.

Syntax

cin >> variable;

Example:

#include<iostream>

using namespace std;

int main()

{

int b;

cin >>b; // cin takes input in “b” variable

return 0;

}

getchar() STATEMENT:

The getchar() function reads the available character from the keyboard. This function reads

only single character at a time. When the user presses the key, getchar() requires Enter key

to be pressed. This function is defined in <stdio.h> header file.

example

#include<iostream>

#include<stdio.h>

using namespace std;

int main()

char b;

cout<<“\n Enter a character:”;ot.com

b = getchar();

cout<<“\n Input character is ” << b;

return 0;

getch() STATEMENT

The getch() function reads the character from the keyboard. This function reads only

single character and not printed on the screen. This function takes input and does not

requires Enter key to be pressed. This function is defined in <conio.h> header file.

example

char ch;

cout<<“\n Enter a character:”;

ch = getch();

cout<<“\n Input character is ” << ch;

return 0;

char name [25];

cout<<“\n Enter your name:”;

gets(name);

cout<<“\n Your Name is ” << name;

return 0;

using namespace std;

getche() STATEMENT:

The getche() function reads the character from the keyboard and echoes on the screen. It

reads only single character and displays on the screen. This function takes input and does

not require Enter key to be pressed. This function is defined in <conio.h> header file.

example

#include<iostream>

#include<conio.h>

using namespace std;

int main()

char ch;

cout<<“\n Enter a character:”;

ch = getche();

Fot.com

cout<<“\n Input character is ” << ch;

return 0;

}

gets() STATEMENT:

example in C++.

This function is used to reads characters or the string input and stores them until a newline

character found. This function is defined in <cstdio.h> header file.

syntax

example

STATEMENT TERMINATOR (:):

In C++, statement terminator is used to end the statement. Statements are terminated with

semicolon ( 😉 symbol. Every statement in C++ must be terminated otherwise and error

message will occur.

ESCAPE SEQUENCES:

Escape sequences are used to control the cursor moves on screen by using special codes.

An escape sequence is a special non-printing characters consists of the escape character

(the backslash “\”) and a second (code) character. The list of the escape sequences is given

below:

Escapedamjeecoaching.blogspot.com

Sequence

\n

\t\

Explanation with Example

Newline. Position the cursor at the beginning of the next line.

Example: cout << “\n”;

= 10

\a

Horizontal tab. It move the cursor the next tab stop.

Example: cout << “\t”; “

Backslash. Insert a backslash character in a string.

Example: cout << “\\”;

Alert. Produces a beep sound or visible alert.

Example: cout << “\a”;

Backspace. It moves the cursor backspace.

Example: cout << “\b”;

Carriage Return. Moves the cursor to the beginning of the current line.

Example: cout < “\r”;

Single Quotation. It is used to print apostrophe sign (‘).

Example: cout << “\”;

Double Quotation.” It is used to print quotation mark (‘).

Example: cout << “\””;

Remainder Or Modulas: Find remainder after integer

division.

Division; It is used to perform division.

Multiplication: It is used to perform multiplication.

to perform addition.

Subtraction: It is used to perform subtraction.

  • Increment Operators
  • Decrement Operators
  • Relational Operators
  • Logical/Boolean Operators
  • Assignment Operators
  • Arithmetic Assignment Operators.com

Quiz

advanced divider