Please help I'm confused on what to do

Write your question here.

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??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 #include <iostream>
#include <cstring>
#include <cmath>
#include "base.h"

using namespace 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 >>)


Also, there is no need to call cin.fail() you can simply do !cin and make use of operator !
http://www.cplusplus.com/reference/ios/ios/operator_not/


Edit:
By the way you never posted the base source so it's hard to tell if your conversion even works.
Last edited on
Could you help me check this? NOT SURE IF THIS IS CORRECT:(

#include <iostream>
#include <string>
#include <Windows.h>
#include <vector>
#include <algorithm>
using namespace std;

void main()
{
int inputBase;
int input1,input2;
int carry;

cout << "Input Base : ";
cin >> inputBase;

if (inputBase > 20 || inputBase < 2)
cout << "Base value out of bounds. ";
else
{
cout << "Input two numbers : "<< endl;
cin >> input1;
if (inputBase > 9)
input1 = input1 + '0';
cin >> input2;
vector<int> vector1;
vector<int> vector2;

cout << endl;
for (; input1; input1/=10)
vector1.push_back( input1%10 );
//reverse(vector1.begin(),vector1.end());


//for (int x=0;x<vector1.size();x++)
// cout << vector1.at(x);
cout << endl;
for (; input2; input2/=10)
vector2.push_back( input2%10 );
//reverse(vector2.begin(),vector2.end());
// for (int x=0;x<vector2.size();x++)
// cout << vector2.at(x);
cout << endl << "The sum is ";
for (int x=0;x<6;x++)
{
int y = vector1[x]+vector2[x];
if (y > inputBase)
{
y = (vector1[x]+vector2[x])-inputBase;
}
if (y == inputBase)
{
y = 0;
carry = 1;
}

cout << y;
}

cout << endl;
}
system("pause");
}

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.


could you help me write it?
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.
Topic archived. No new replies allowed.