Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help editing a code for c++ [I need to get the following message When (ABC) is lower than base 13] plus i want

I need help editing a code for c++

[I need to get the following message When (ABC) is lower than base 13] plus i want to see if it can work with decimals as well.

here is the code

#include #include #include #include  using namespace std; /* This Class represents the Number in a Number System and also provides the facility to convert the Number system associated with that Number i.e. conversion from any Base to any other base like, octal to hexa or hexa to decimal etc. */ struct Number { // Contains the number as string std::string m_data; // Contains the internal base int m_base; // Calculates the power of given number int powerOf(int val, int power) { if (power == 0) return 1; int result = val; for (int i = 1; i < power; i++) { result = result*val; } return result; } public: Number(std::string num, int base = 10) : m_base(base), m_data(num) {} int getBase() const { return m_base; } const std::string& getData() const { return m_data; } // Convert the internal number to decimal base and returns the number // according to decimal number system. int getDecimalValue() { int decValue = 0; for (int i = m_data.size() - 1; i >= 0; i--) { if (m_data[i] >= 'A' && m_data[i] < 'Z') decValue = decValue + (m_data[i] - 'A' + 10)*powerOf(m_base, (m_data.size() - 1 - i)); else decValue = decValue + (m_data[i] - '0')*powerOf(m_base, (m_data.size() - 1 - i)); } return decValue; } // This API will convert the existing base in Number system to any other base bool convertBase(int newBase) { if (newBase > (10 + 'Z' - 'A')) { std::cout << "Conversion Not Possible for Base Greater Than " << (10 + 'Z' - 'A') << std::endl; return false; } int decValue = getDecimalValue(); std::string newValue = ""; while (decValue > 0) { int rem = decValue % newBase; decValue = decValue / newBase; char newChar = '0' + rem; if (newChar > '9') { newChar = 'A' + (newChar - '9') - 1; } newValue += newChar; } std::reverse(newValue.begin(), newValue.end()); m_data = newValue; m_base = newBase; return true; } }; int main() { bool flag = true; string input; cout << "This is a program to read and write integer numbers in different bases." << endl << endl; while (flag) { string original = ""; char yn; int target_base = 0; string x = ""; string arr[2]; int base = 0; int i = 0; cout << "Type in an integer number in the format(n)b "; cin >> input; original = input; cout << "What is the target base? "; cin >> target_base; input = input.substr(1); std::istringstream ins(input); std::string out; i = out.length(); i = 2; while (ins.good() && i!=0) { getline(ins, out, ')'); if (!out.empty()) { i = i - 1; arr[i] = out; } } stringstream geek(arr[0]); geek >> base; Number numObj(arr[1], base); numObj.convertBase(target_base); cout << original << " = (" << numObj.getData() << ")" << numObj.getBase()<> yn; if (yn == 'n' || yn == 'N') { flag = false; } cout << endl< 

Example output:

Type in an integer number in the format (n)b:

(ABC)5

!!! Invalid number.... the digits are out of range. !

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Accounting For Value

Authors: Stephen Penman, S Penman

1st Edition

0231151187, 9780231151184

More Books

Students also viewed these Accounting questions

Question

3. Where are the employees looking for clarity?

Answered: 1 week ago