The goal of my program is to take in 2 inputs - one for the base, and one for the number (which should be of that base type). It will then do a
calculation and print out the number in base 10 form. For instance,

Input Output
========== ======
10 1234 1234
8 77 63 (the value of 77 in base 8, octal)
2 1111 15 (the value of 1111 in base 2, binary)

So far, I've been able to do everything except successfully take a numerical input in - I can't think of a way to take individual numbers from a character array and store it into an integer array so that my equations will work properly =\

/*
1. Read in 2 inputs: base and number
2. Convert the number to the specified base
3. Print the results
*/

#include <stdio.h>
#include <stdlib.h>

#define SIZE 100

void to_bin(int *, int, int);
void to_oct(int *, int, int);
void to_dec(int *, int);
int pow(int, int);

int main(void)
{
	int base, i = 0;
	int num[SIZE] = {0};
	char input[SIZE], *ptr;

	puts("Enter a base number (2, 8, 10):");
	scanf("%d", &base);
	puts("Enter a number:");
	scanf("%s", &input);
	
	ptr = input;
	while (*ptr != '\0')
	{
		num[i] = *ptr;
		ptr++;
		i++;
	}

	if (base == 2)
		to_bin(num, i, base);
	if (base == 8)
		to_oct(num, i, base);
	if (base == 10)
		to_dec(num, i);

	return 0;
}

void to_bin(int *num, int i, int base)
{
	int sum = 0, n = 0;

	for (;i >= 0; i--)
	{
		if (num[i] != 0)
			sum += pow(n, base);
		n++;
	}
	printf("%d\n", sum);
}

void to_oct(int *num, int i, int base)
{
	int sum = 0, n = 0;

	for (;i >= 0; i--)
	{
		if (num[i] != 0)
			sum += (num[i] * pow(n, base));
		n++;
	}
	printf("%d\n", sum);
}

void to_dec(int *num, int i)
{
	int cnt;
	for (cnt = 0; cnt <= i; cnt++)
		printf("%d", num[cnt]);
	puts("");
}

int pow(int n, int base)
{
	int cnt, tot = 1;

	for (cnt = 0; cnt < n; cnt++)
		tot *= base;

	return tot;
}

>>I can't think of a way to take individual numbers from a character array and store it into an integer array

In general this often works for characters that are digits:
int integerValue = array - '0';

where '0' is the char representing zero and integerValue will be the integer value of array. This takes advantage of the integer values of characters as assigned by the character set being used and that digits should be sequential in character set with values ascending by one in the range of zero to nine.

Another useful way to do this is with a switch statement to convert symbols to integers. This can be a rather straightforward way to convert characters representing values greater than 9 in bases greater than 10.

int r;
switch(array[i])
{
   case 'A':
      r = 10;
      break;
   case 'B':
      r = 11;
      break;
   etc

I'm sure there are other ways as well.

For instance, if I take an input of numbers and store them into a character array, how would I take each individual number and store it into an integer array?

Like if I entered "1111" as my input it would go into the char array as {'1', '1', '1', '1', '\0'}. Now, I want to take each one of those numbers and put it into an integer array so i get {1, 1, 1, 1}. That way I can change a binary input into base 10 form (in this case 1111 -> 15).

I figured out a weird way of doing it =\

Storing each char into temp[0], placing a '\0' in temp[1] so it would be read as a string and then converting it with atoi().

int main(void)
{
	int base, i = 0, j = 0;
	int num[SIZE];
	char input[SIZE], temp[SIZE] = {""}, *ptr;


	puts("Enter a base number (2, 8, 10):");
	scanf("%d", &base);
	puts("Enter a number:");
	scanf("%s", &input);
   
	ptr = input;
	while (*ptr != '\0')
	{
		temp[j] = *ptr;
		temp[j + 1] = '\0';
		num[i] = atoi(temp);
		
		ptr++;
		i++;
		j = 0;
	}

	i -= 1;

	if (base == 2)
		to_bin(num, i, base);
	if (base == 8)
		to_oct(num, i, base);
	if (base == 10)
		to_dec(num, i);

	return 0;
}
int i = 0;
while (input[i] != '\0')
{
   num[i] = input[i] - '0';
   ++i;

or 

while(input[i] != '\0')
{
   switch(input[i])
   {
      case 'A':
         num[i] = 10;

Changing each char of the input into a string and using atoi() to convert it is another way, if the char you are trying to change is a digit, but it won't work if you are trying to change hexadecimal into decimal.

If I had to convert hexadecimal i would've had even more headaches :cry:

Thanks for the replies though.

This article has been dead for over six months. Start a new discussion instead.