0% found this document useful (0 votes)
9 views4 pages

Numeric Data Extraction Program

The document describes two programming tasks: one for filtering numeric characters from a string and another for validating user registration inputs. The first task requires extracting digits from a mixed string and outputting them or -1 if none are found, while the second task checks username length and age constraints, printing appropriate error messages or a success message. Both tasks involve exception handling for invalid inputs.

Uploaded by

Kishore
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Numeric Data Extraction Program

The document describes two programming tasks: one for filtering numeric characters from a string and another for validating user registration inputs. The first task requires extracting digits from a mixed string and outputting them or -1 if none are found, while the second task checks username length and age constraints, printing appropriate error messages or a success message. Both tasks involve exception handling for invalid inputs.

Uploaded by

Kishore
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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;

You might also like