1. A system is required to process a sequence of characters and filter out only numeric data.
The task
involves reading a string that may contain a mix of alphabetic characters, numbers, and special symbols.
The program must extract and display only the numeric characters from the string. If no numeric
characters are present in the processed string, the program should output -1. Special characters should
be ignored without affecting the result.
Input Format
The input consists of a single string containing alphanumeric characters.
Output Format
Print the integer value after removing non-numeric characters.
If no numeric characters are found, output "-1".
Constraints
The input string must contain between 1 and 100 characters, inclusive.
The input string may include letters (both uppercase and lowercase), digits, and special
characters.
Program:
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
int main() {
try {
string input;
getline(cin, input);
if ([Link]() < 1 || [Link]() > 100) {
throw invalid_argument("Invalid input");
string numericString;
for (char ch : input) {
if (isdigit(ch)) {
numericString += ch;
if ([Link]()) {
throw -1;
} else {
cout << numericString << endl;
} catch (const invalid_argument& e) {
cout << [Link]() << endl;
} catch (int a) {
cout << a << endl;
}
return 0;
2. You are building a user registration system for a website. The system checks for valid inputs for
username and age. If the username is too short or the age is out of range, exceptions will be thrown.
Input Format
The first line contains the username (string).
The second line contains the user's age (integer).
Output Format
If the username is shorter than 3 characters, print an Invalid username.
If the age is below 18 or above 100, print Invalid age.
If both inputs are valid, print Registration is successful.
Constraints
-100 ≤ Username length ≤ 100
-20 ≤ Age ≤ 150
Program:
#include <iostream>
using namespace std;
int main() {
try {
string username;
int age;
cin >> username;
cin >> age;
if ([Link]() < 3) {
throw invalid_argument("Invalid username");
if (age < 18 || age > 100) {
throw invalid_argument("Invalid age");
cout << "Registration successful" << endl;
} catch (const invalid_argument& e) {
cout << [Link]() << endl;
} catch (const exception& e) {
cout << "An error occurred: " << [Link]() << endl;
return 0;