The included attachment gives the hex2Dec(const string& hexString) function that returns a decimal number from a hex string. Implement the hex2Dec function to throw an invalid_argument exception if the string is not a hex string. Write a test program in C++ that prompts the user to enter a hex number as a string and display the number in decimal.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

The included attachment gives the hex2Dec(const string& hexString) function that returns a decimal number from a hex string. Implement the hex2Dec function to throw an invalid_argument exception if the string is not a hex string. Write a test program in C++ that prompts the user to enter a hex number as a string and display the
number in decimal.

```cpp
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

// Converts a hex number as a string to decimal
int hex2Dec(const string& hex);
// Converts a hex character to a decimal value
int hexCharToDecimal(char ch);

int main()
{
    // Prompt the user to enter a hex number as a string
    cout << "Enter a hex number: ";
    string hex;
    cin >> hex;

    cout << "The decimal value for hex number " << hex 
         << " is " << hex2Dec(hex) << endl;

    return 0;
}

int hex2Dec(const string& hex)
{
    int decimalValue = 0;
    for (unsigned i = 0; i < hex.size(); i++)
        decimalValue = decimalValue * 16 + hexCharToDecimal(hex[i]);

    return decimalValue;
}

int hexCharToDecimal(char ch)
{
    ch = toupper(ch); // Change it to uppercase
    if (ch >= 'A' && ch <= 'F')
        return 10 + ch - 'A';
    else // ch is '0', '1', ..., or '9'
        return ch - '0';
}
```

### Explanation

This C++ program converts a hexadecimal number, given as a string, into its decimal equivalent. It involves two major functions:

1. **hex2Dec**: This function takes a hexadecimal string and converts it into a decimal number by iterating through each character in the string, leveraging the `hexCharToDecimal` function for individual character conversion.

2. **hexCharToDecimal**: This converts a single hexadecimal character into its decimal equivalent. It differentiates between alphabetic ('A' to 'F') and numeric ('0' to '9') characters, using `toupper` to handle the case insensitivity.

In the `main` function, the user is prompted to enter a hexadecimal number. The program then outputs its decimal equivalent.
Transcribed Image Text:```cpp #include <iostream> #include <string> #include <cctype> using namespace std; // Converts a hex number as a string to decimal int hex2Dec(const string& hex); // Converts a hex character to a decimal value int hexCharToDecimal(char ch); int main() { // Prompt the user to enter a hex number as a string cout << "Enter a hex number: "; string hex; cin >> hex; cout << "The decimal value for hex number " << hex << " is " << hex2Dec(hex) << endl; return 0; } int hex2Dec(const string& hex) { int decimalValue = 0; for (unsigned i = 0; i < hex.size(); i++) decimalValue = decimalValue * 16 + hexCharToDecimal(hex[i]); return decimalValue; } int hexCharToDecimal(char ch) { ch = toupper(ch); // Change it to uppercase if (ch >= 'A' && ch <= 'F') return 10 + ch - 'A'; else // ch is '0', '1', ..., or '9' return ch - '0'; } ``` ### Explanation This C++ program converts a hexadecimal number, given as a string, into its decimal equivalent. It involves two major functions: 1. **hex2Dec**: This function takes a hexadecimal string and converts it into a decimal number by iterating through each character in the string, leveraging the `hexCharToDecimal` function for individual character conversion. 2. **hexCharToDecimal**: This converts a single hexadecimal character into its decimal equivalent. It differentiates between alphabetic ('A' to 'F') and numeric ('0' to '9') characters, using `toupper` to handle the case insensitivity. In the `main` function, the user is prompted to enter a hexadecimal number. The program then outputs its decimal equivalent.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Header Files
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education