input output handling
input output handling
Explanation:
getch() is a function that reads a single character from the keyboard but does not display
it on the screen.
It is often used to wait for a user to press a key before proceeding.
Example:
cpp
Copy code
#include <conio.h> // Include the header file for getch()
int main() {
char ch;
return 0;
}
Explanation of Example:
The program waits for the user to press any key without displaying it on the screen.
Once a key is pressed, the character is stored in ch, and then it prints the character.
2) getche() Function
Explanation:
getche() is similar to getch(), but it displays the character on the screen as you press it.
Example:
cpp
Copy code
#include <conio.h> // Include the header file for getche()
int main() {
char ch;
return 0;
}
Explanation of Example:
3) gets() Function
Explanation:
Example:
cpp
Copy code
#include <stdio.h> // Include the header file for gets() and puts()
int main() {
char name[50];
printf("Hello, ");
puts(name); // Prints the string
return 0;
}
Explanation of Example:
4) puts() Function
Explanation:
puts() is used to print a string followed by a newline character.
It’s simpler than using printf() for just printing strings.
Example:
cpp
Copy code
#include <stdio.h>
int main() {
char greeting[] = "Welcome to C++ programming!";
return 0;
}
Explanation of Example:
The program prints the string greeting followed by a newline, making it easier to print
text on its own line.
Summary:
These functions are useful for handling basic input and output in C/C++ programs.
Definition:
Escape sequences are special characters used in strings and character literals to represent non-
printable characters, control characters, or characters that have special meanings.
1. Newline (\n)
o Description: Moves the cursor to the next line.
o Example:
cpp
#include <stdio.h>
int main() {
printf("Hello, World!\nWelcome to C++ programming.\n");
return 0;
}
Output:
Hello, World!
Welcome to C++ programming.
2. Tab (\t)
o Description: Inserts a horizontal tab space.
o Example:
#include <stdio.h>
int main() {
printf("Name\tAge\tCountry\nJohn\t20\tUSA\n");
return 0;
}
Output:
3. Backslash (\\)
o Description: Inserts a backslash (\) character.
o Example:
#include <stdio.h>
int main() {
printf("This is a backslash: \\\n");
return 0;
}
Output:
This is a backslash: \
#include <stdio.h>
int main() {
printf("It\'s a sunny day.\n");
return 0;
}
Output:
#include <stdio.h>
int main() {
printf("He said, \"Hello!\"\n");
return 0;
}
Output:
He said, "Hello!"
#include <stdio.h>
int main() {
printf("Hello, World!\rC++");
return 0;
}
Output:
C++o, World!
7. Backspace (\b)
o Description: Moves the cursor one position back, deleting the character at that
position.
o Example:
#include <stdio.h>
int main() {
printf("Hello\b World!\n");
return 0;
}
Output:
Hell World!
#include <stdio.h>
int main() {
printf("Page 1\fPage 2\n");
return 0;
}
Output:
markdown
Copy code
Page 1
Page 2
9. Alert/Bell (\a)
o Description: Produces a beep sound (if supported by the system).
o Example:
#include <stdio.h>
int main() {
printf("Beep sound\a\n");
return 0;
}
#include <stdio.h>
int main() {
char str[10] = "Hello\0World";
printf("%s\n", str);
return 0;
}
Output:
Hello
Summary
Escape sequences are used to represent special characters in strings.
They start with a backslash (\) followed by a character.
They are essential for formatting output, controlling cursor movement, and including
special characters in strings.
Understanding escape sequences is crucial for controlling text output and managing how strings
are displayed in C/C++ programs. C++ program that demonstrates basic I/O (Input/Output)
handling using standard functions:
cpp
Copy code
#include <iostream> // Include the necessary header for I/O operations
#include <string> // Include the string library for handling strings
int main() {
string name;
int age;
float grade;
return 0;
}
Explanation:
1. Headers Included:
o <iostream>: Provides functionalities for standard input and output streams.
o <string>: Allows the use of string objects for handling text.
2. Using Namespace std:
o This allows using standard functions (like cin, cout, etc.) without the std::
prefix.
3. Input Handling:
o getline(cin, name): Reads the full line of text for the name, including spaces.
o cin >> age;: Reads an integer value for age.
o cin >> grade;: Reads a floating-point number for the grade.
4. Output Handling:
o cout: Used to display output to the console.
o The program prints out the entered name, age, and grade, then evaluates the grade
and provides feedback.
This program is a basic example of how input and output handling works in C++, demonstrating
the use of cin, getline, and cout to interact with the user.