-3

In this program convert.c, I am trying to convert a given number of any base to base 10. The command is given as convert .todecimal is supposed to do that. The code below has errors of course but I am not sure how to make it work. For eg,the number in argv[3]is 123 in base 9. The equation is supposed to work like this :(1 x 9^2) + (2 x 9^1) + (3 x 9^0) = (102)10.where the variables are (n x argv[3]^i) + (n+1 * argv[3]^i-1)....How do i get a char of 123 when 123 itself a char? Any help is appreciated.

#include<stdio.h>
#include<math.h>
int todecimal();

main(int argc, char *argv[])
{
int s = 0;
int i = 0;
int n = 1;
if (argc < 4) {
   printf("Usage:  convert <basefrom> <baseto> <number>\n");
   }
printf("to decimal prints %d" todecimal());
}

int todecimal() {
    if (argv[3] > 0) {
       if ((getchar(argv[3]) != NULL)) {
          i = i + 1;
          }
          while (i >= 0) {
               s = s + (n * (pow(argv[3],i)));
               n = n + 1;
               i = i - 1;
                }
                return s;
        }
                }
  • 1
    `(argv[1])^i` isn't exponentiation, it's the bitwise `xor` operator. Use `pow` for exponentiation (add `#include ` to your program). – Daniel Kleinstein Feb 15 '15 at 20:29
  • 2
    First you need to learn some ***basic*** C, like what function arguments are, and function return values (and what happens when you don't return a value from a function declared to return something (hint: it's not good)). Then read about what [`printf`](http://en.cppreference.com/w/c/io/fprintf) expects. – Some programmer dude Feb 15 '15 at 20:29
  • Loose notes: `argv` cannot be reached in your function. It's not a number. `getchar` does not take an argument. `argv[3]` will always be greater than 0 -- it's a pointer. An so on. Voting to close as "too broken, can't be fixed". – Jongware Feb 15 '15 at 20:47
  • I'm voting to close this question as off-topic because too many things are wrong to reasonably clarify to the OP. – Jongware Feb 15 '15 at 20:48
  • I am quite new to C. What are the areas i need to work on other than the ones already mentioned? It says my variables are undeclared although I did in the main method. – Avinash Prabhakar Feb 15 '15 at 21:00
  • Also how do you get a character from what is already a character? Eg the number/char 125.Is it possible to get the 1? Not really sure how to use the getchar. – Avinash Prabhakar Feb 15 '15 at 21:04

1 Answers1

0

There is a difference between char and char*. The latter is a pointer to char, also used as a string (a sequence of chars). So, wherever you can have more than one char, you use a pointer (to the first one) instead - this is your argv[3].

So, to get one char from a sequence of chars, you apply square brackets:

argv[3] - is the whole string, let's pretend it's "192" to reduce confusion
argv[3][0] - is the first char, '1'
argv[3][1] - is the second char, '9'
argv[3][2] - is the third char, '2'

In your case, you need argv[3][i]. But you have to fix your other bugs too (there are many, as other people noted).

anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • Okay this is what I needed to know the most.Thanks. – Avinash Prabhakar Feb 15 '15 at 21:06
  • This is a bit confusing. When you store 5 and '5' in a char, is it not the same thing since the datatype is char? what does char c = '5' - '0'; actually do? Also can atoi be used on a single char since it requires char*(string) as argument? – Avinash Prabhakar Feb 15 '15 at 22:56