Hi Everyone,

I am BRAND NEW to C and I am trying to make a program that takes 3 inputs. The first is a number, the second represents what base the first argument is, and the third argument represents what base this number should be outputted as.

For example:
convert 43 8 10
35

I am supposed to use isdigit and atoi. I am quite confused and any tips would be greatly appreciated.

Thanks

If your using number/base then I would check out the function strtol()

If your using number/base then I would check out the function strtol()

thanks, but i have to use atoi ... and i have to define my own atoi which can take letters in the case of base 11 to 36

I might create a function that has this kind of a prototype:

int convert(const char *text, int inbase, int outbase);

Pass the function the text "43" as is. Convert the "8" and the "10" to ints however you choose -- maybe even your own atoi!

The convert function then does two things:

  1. It breaks down the incoming text into an integer value using the inbase.
  2. It creates an output string based on the outbase.

The first part involves a loop in which you multiply the current integer value by inbase and then add the "digit" value. Note that the text conversion (for the digit value) from '4' to 4 is easy, but for bases higher than 10 it is a little extra work.

The second part involves div and mod operations to create the digits in outbase. Depending on how you do this, it might be easier to create this string "backwards" and then reverse it.

I hope I have mentioned enough of the basic elements to give you some sort of starting point to begin writing your coding attempt.

I might create a function that has this kind of a prototype:

int convert(const char *text, int inbase, int outbase);

Pass the function the text "43" as is. Convert the "8" and the "10" to ints however you choose -- maybe even your own atoi!

The convert function then does two things:

  1. It breaks down the incoming text into an integer value using the inbase.
  2. It creates an output string based on the outbase.

The first part involves a loop in which you multiply the current integer value by inbase and then add the "digit" value. Note that the text conversion (for the digit value) from '4' to 4 is easy, but for bases higher than 10 it is a little extra work.

The second part involves div and mod operations to create the digits in outbase. Depending on how you do this, it might be easier to create this string "backwards" and then reverse it.

I hope I have mentioned enough of the basic elements to give you some sort of starting point to begin writing your coding attempt.

This is very helpful. Thanks a lot!

I am getting the following warning when trying to test my code: "passing arg 1 of 'jmsatoi' makes pointer from integer without cast"

I don't really understand why. Any help would be much appreciated.

#include <stdio.h>

int jmsisdigit(char c, int base){
	  if('0' <= c && c <= '9'){
           return 1;
    } else if ('a' <= c && c < ('a' + base - 10)){
           return 0;
    }
}

int jmsatoi(char s[], int base) {
	int i, n;
	n = 0;
	if (base <= 10){
	for(i = 0; jmsisdigit(s[i], base); i++) {
		n = base * n + (s[i]-'0');
		return n;
	}
	}else{
    for(i = 0; jmsisdigit(s[i], base); i++) {
		n = base * n + (s[i] - 'a' + 10);
		return n;
         }
  }
}

int main(char numeral, int inbase, int outbase){
    int test;
    test = jmsatoi(1234,10);
}
test = jmsatoi("1234",10);

You can't define your own form of main() like this:

int main(char numeral, int inbase, int outbase)

You'll have to use this (or equivalent):

int main(int argc, char *argc[])

Your for loops don't loop: they return after a single iteration.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.