1) getch() Function
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;
printf("Press any key to continue...");
ch = getch(); // Waits for the user to press a key
printf("\nYou pressed: %c", 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;
printf("Press any key to continue...");
ch = getche(); // Waits for the user to press a key and displays it
printf("\nYou pressed: %c", ch);
return 0;
}
Explanation of Example:
The program waits for the user to press any key.
The pressed key is immediately displayed on the screen, and then the character is stored
in ch and printed.
3) gets() Function
Explanation:
gets() is used to read a string (a line of text) from the user.
It reads the entire line of input until the user presses Enter.
Example:
cpp
Copy code
#include <stdio.h> // Include the header file for gets() and puts()
int main() {
char name[50];
printf("Enter your name: ");
gets(name); // Reads a string of characters from the user
printf("Hello, ");
puts(name); // Prints the string
return 0;
}
Explanation of Example:
The program prompts the user to enter their name.
The gets() function reads the name and stores it in the name array.
The puts() function then prints the name on the screen.
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!";
puts(greeting); // Prints the string with a newline
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:
getch(): Reads a character without displaying it.
getche(): Reads a character and displays it.
gets(): Reads an entire line of text.
puts(): Prints a string followed by a newline.
These functions are useful for handling basic input and output in C/C++ programs.
Escape Sequences in C/C++
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.
Types of Escape Sequences
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:
Name Age Country
John 20 USA
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: \
4. Single Quote (\')
o Description: Inserts a single quote (') character.
o Example:
#include <stdio.h>
int main() {
printf("It\'s a sunny day.\n");
return 0;
}
Output:
It's a sunny day.
5. Double Quote (\")
o Description: Inserts a double quote (") character.
o Example:
#include <stdio.h>
int main() {
printf("He said, \"Hello!\"\n");
return 0;
}
Output:
He said, "Hello!"
6. Carriage Return (\r)
o Description: Moves the cursor to the beginning of the current line.
o Example:
#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!
8. Form Feed (\f)
o Description: Moves the cursor to the next page (mainly used in printing).
o Example:
#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;
}
10. Null Character (\0)
o Description: Represents the end of a string. It is automatically added at the end of
strings in C/C++.
o Example:
#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
using namespace std;
int main() {
string name;
int age;
float grade;
// Input: Asking user for their name
cout << "Enter your name: ";
getline(cin, name); // Using getline to read a full line of text
// Input: Asking user for their age
cout << "Enter your age: ";
cin >> age;
// Input: Asking user for their grade
cout << "Enter your grade (out of 100): ";
cin >> grade;
// Output: Displaying the collected information
cout << "\n--- Student Information ---" << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Grade: " << grade << endl;
// Output: Evaluating grade
if (grade >= 90) {
cout << "You got an A!" << endl;
} else if (grade >= 80) {
cout << "You got a B!" << endl;
} else if (grade >= 70) {
cout << "You got a C!" << endl;
} else if (grade >= 60) {
cout << "You got a D!" << endl;
} else {
cout << "You got an F. Better luck next time!" << endl;
}
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.