In the previous article, we learned about strings. Now let us learn about the methods of working with strings:
1. cin >> str
The 'cin >> str' statement is used to read input from the standard input stream (usually the keyboard) and store it in a string object or a C-style string. It reads characters until it encounters whitespace (space, tab, or newline) and discards the leading whitespace.
1.1 Using cin >> str with C++ strings
When working with C++ strings (std::string), the 'cin >> str' statement reads input and stores it in the string object. Here's an example:
#include <iostream>
#include <string>
int main() {
std::string input;
std::cout << "Enter a string: ";
std::cin >> input;
std::cout << "You entered: " << input << std::endl;
return 0;
}
1.2 Using cin >> str with C-style strings
When working with C-style strings, the 'cin >> str' statement reads input and stores it in the character array, ensuring that it is null-terminated. Here's an example:
#include <iostream>
int main() {
char input[256];
std::cout << "Enter a string: ";
std::cin >> input;
std::cout << "You entered: " << input << std::endl;
return 0;
}
2. str[80]
The 'str[80]' syntax is used to access the 81st element (zero-based index) of a string or a character array. If the index is within the bounds of the string, it returns the character at the specified position. If the index is out of bounds, the behavior is undefined.
2.1 Accessing characters in C++ strings
Here's an example of using the 'str[80]' syntax to access a character in a C++ string:
#include <iostream>
#include <string>
int main() {
std::string str = "This is an example string.";
char ch = str[2]; // Access the 3rd character (zero-based index)
std::cout << "The character at index 2 is: " << ch << std::endl;
return 0;
}
2.2 Accessing characters in C-style strings
Here's an example of using the 'str[80]' syntax to access a character in a C-style string:
#include <iostream>
int main() {
char str[] = "This is an example string.";
char ch = str[2]; // Access the 3rd character (zero-based index)
std::cout << "The character at index 2 is: " << ch << std::endl;
return 0;
}
3. cin.get
The 'cin.get' method is used to read a single character from the standard input stream (usually the keyboard). It can read any character, including whitespace (space, tab, or newline).
3.1 Using cin.get to read a single character
Here's an example of using 'cin.get' to read a single character:
#include <iostream>
int main() {
char ch;
std::cout << "Enter a character: ";
std::cin.get(ch);
std::cout << "You entered: " << ch << std::endl;
return 0;
}
3.2 Using cin.get to read a line of text
Using 'cin.get' with a character array and a maximum number of characters to read, you can read an entire line of text, including whitespace. Here's an example:
#include <iostream>
int main() {
char input[256];
std::cout << "Enter a line of text: ";
std::cin.get(input, 256);
std::cout << "You entered: " << input << std::endl;
return 0;
}
4. cin >> setw
The 'cin >> setw' statement is used in conjunction with the 'cin' object to limit the number of characters read from the standard input stream. The 'setw' function is part of the <iomanip> header and takes an integer parameter that specifies the maximum number of characters to read.
4.1 Using cin >> setw with C++ strings
When working with C++ strings, you need to use 'cin >> setw' with 'getline' to limit the number of characters read. Here's an example:
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
int main() {
std::string input;
std::cout << "Enter a string (maximum 10 characters): ";
std::getline(std::cin >> std::setw(11), input);
std::cout << "You entered: " << input << std::endl;
return 0;
}
4.2 Using cin >> setw with C-style strings
Here's an example of using 'cin >> setw' with a C-style string:
#include <iostream>
#include <iomanip>
int main() {
char input[11];
std::cout << "Enter a string (maximum 10 characters): ";
std::cin >> std::setw(11) >> input;
std::cout << "You entered: " << input << std::endl;
return 0;
}
5. cin.get with terminating sign
The 'cin.get' method can be used with a terminating sign to read characters from the standard input stream until a specified delimiter is encountered. This is useful when you want to read input until a specific character is found, rather than relying on whitespace or a fixed number of characters.
5.1 Using cin.get with a terminating sign for C++ strings
Here's an example of using 'cin.get' with a terminating sign to read input into a C++ string:
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::string input;
std::cout << "Enter a string, terminated by a period: ";
char ch;
std::stringstream ss;
while (std::cin.get(ch) && ch != '.') {
ss << ch;
}
input = ss.str();
std::cout << "You entered: " << input << std::endl;
return 0;
}
5.2 Using cin.get with a terminating sign for C-style strings
Here's an example of using 'cin.get' with a terminating sign to read input into a C-style string:
#include <iostream>
int main() {
char input[256];
std::cout << "Enter a string, terminated by a period: ";
char ch;
int index = 0;
while (std::cin.get(ch) && ch != '.' && index < 255) {
input[index++] = ch;
}
input[index] = '\0';
std::cout << "You entered: " << input << std::endl;
return 0;
}
Conclusion
In conclusion, this comprehensive guide has covered various methods of handling input and strings in C++, such as 'cin >> str', 'str[80]', 'cin.get', 'cin >> setw', and 'cin.get' with a terminating sign. By leveraging these methods and understanding their intricacies, you can efficiently read and process input in your C++ programs, even at the level of computer science students.