Write a program that will perform integer addition in any radix from 2 to 20. Use the
standard Hindu-Arabic numerals 0–9 and, if needed, the uppercase English letters A–J
Is this right??
#include <iostream>
#include <cstring>
#include <cmath>
#include "base.h"
usingnamespace std;
int main()
{
int base;
int num;
InVal num_c;
cout << "Please enter the number you wish to convert:\n";
cin >> num;
if (cin.fail())
{
cout << "Base value out of bounds.\n\n";
return 1;
}
cout << "Please enter the base you wish to convert to:\n";
cin >> base;
if (cin.fail())
{
cout << "This is not a valid input.\n\n";
return 1;
}
if (base < 2 || base > 20)
{
cout << "You have entered a base that is outside of\n"
<< "the valid range.\n\n";
return 1;
}
num_c.init_val(num,10);
cout << "Your number is: ";
num_c.PrintAsBase(base);
}
Adding two numbers of any given base is different than converting from one base to another. So either your description is wrong or the code is wrong.
If it is the former then you wouldn't be able to read in as base 10 by the way, you would have to read in as a string (or you could make a custom class and override the operator >>)
Are you trying to read in numbers in base 2 -> 20 and then add those 2 numbers? If so you are probably going to have to read into a custom class or a string before adding them and not read in as an integer; integers are base 10.
You have the read the numbers as strings.
Then convert the strings to numbers using strtol(): http://www.cplusplus.com/reference/cstdlib/strtol/?kw=strtol.
Add the two numbers.
Convert back to base-20 strings. You'll have to write this one yourself.
Sure. You have the code to input the base. Now write a function that asks the user for a number in that base. Read the number as a string. Then use strtol() to convert the string to a number. Print out the number.