Re: A base conversion~ help me to correct it since it can't run



jyck91@xxxxxxxxx wrote:
// Base Conversion
// Aim: This program is to convert an inputted number
// from base M into base N. Display the converted
// number in base N.

Atleast for posting to Usenet, the C style, (i.e. /* ... */) comments
are better.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 20

int temp, m, n, i, r, base10, true;
char num[LENGTH], result[LENGTH];

// This function is to convert a number in base M
// into a number in base 10.
void baseM_to_base10(void)
{
base10 = 0;
for( i=0; i<20 && true = 1; i++) // get the number from base N

ITYM true == 1;. Also use LENGTH instead of an hardcoded value.

{ num[i] = getchar(); // sub. the number into string

getchar returns an int value to signal EOF, in case of failure. Only
after having checked it against EOF should you assign it to a char
object. Aside from that I think your whole statement is wrong. Why not
use fgets to input a line, check whether it's a valid number with for
example strtol or something else and then assign it to num?

if (num == '\n')

An array name is a pointer to the first element of the array. Here
you're comparing a pointer constant to a char. You should compare a
particular element of num against '\n', like num[i] == '\n'.

{num[i] = '\0'; // if the above statement is true , num[i] will
equal end of string

This is the reason why C++ comments are discouraged in postings to
newsgroups.

true = 0 ; }} // end
fflush(stdin); // wash away the excess char

fflush is only defined for output streams. fflush with stdin leads to
undefined behaviour. Use a simple while loop with getchar or getc and
encapsulate it into a convenient function.

for(i = strlen(num) - 1; i >=0; i--)
{if(num[i] == '1')
base10 = base10 + 1 * pow(2 , strlen(num)- 1 - i);

What exactly does this do?
Also where have you included math.h for pow?

}
}

// This function is to convert a number in base 10
// into a number in base N.
void base10_to_baseN(unsigned long long number,unsigned short base)
{
unsigned short temp[30],i;
for (i=0;i<30 && number<base;i++)
{
temp[i] = number % base;
number = number / base;
}
temp[i] = number;

Assigning a long long type to a short.

printf("The number in %u base is : ",base);

Use %hu for unsigned short. Also terminate printf strings with a
newline or call fflush(stdout) immediately afterwards if you want to
ensure output is to appear synchronously.

for (i=29;i<30;i--)
{
if (temp[i]<10)
printf("%u",temp);

ITYM an element of temp.

if (temp[i]>10 || temp[i]<36)
printf("%uc",temp[i]+51);

What's this supposed to do?

}
}

main()
{
// m - base M (input base)
// n - base N (output base)
// num - inputted number in base M
// result - converted number in base N

// Variable Declaration
// Prompt the user to enter data required.
printf("Base Conversion\n");
printf("---------------\n");
printf("Please enter an inputted number: ");
scanf("%s",num);

Specify a length argument along with scanf for string input. Otherwise
it's as dangerous as gets.

printf("Base M (2 to 36): ");
scanf("%d",&m);
printf("Base N (2 to 36): ");
scanf("%d",&n);
printf("Result is %d \n", result);

You're attempting to print a pointer value as an integer. That's
undefined behaviour. To print a pointer value use the p conversion
specifier or print a particular element of result, (since you've
declared it as an array), or print out the whole array through a for
loop, or print a string in the array, if one is present, with a %s
format specifer and result as the corresponding argument.

// Perform Base Conversion
// - Call function baseM_to_base10 to convert the number
accordingly
// - Call function base10_to_baseN to convert the number
accordingly

// Display the converted number
system("PAUSE");

Non-portable. Use getchar for the same effect.

}

// The End Of Main Program

You're taking on a too ambitious project at too early a time in your
study of C. You've several fundamental misunderstandings and your
program; it is very, very fragile, and broken. At a minimum fix the
mistakes I've pointed out and try again.

.



Relevant Pages

  • Re: "Mastering C Pointers"....
    ... A pointer is a kind of variable that can "point to" some object. ... has a type (pointer to int), and a value of some kind. ... You may know that you can access these integers by using array notation ... The function will take one argument, a string, and will return the length ...
    (comp.lang.c)
  • Re: copy a string into a 2d array of chars
    ... This split function should allocate a 2D array of chars ... >focus the program the string is not actually split. ... later) is an array of char containing the original contents of the ... The i-th pointer will contain the starting address of the ...
    (comp.lang.c)
  • Re: new IL: C (sort of...).
    ... C doesn't need a string type... ... variant of PL/1 which was very Pascal-ish. ... - C does implement an array declaration. ... effectively converted into a pointer that can be used with the offset ...
    (comp.lang.misc)
  • Re: multi dimensional arrays as one dimension array
    ... please - where does the standard say that such a conversion ... Pointer conversion yields a pointer to the same object as ... exist only where there are array declarations. ...
    (comp.lang.c)
  • Re: Arrays of Strings, malloc
    ... Each string is at most 50 characters long, ... How do I actually start the array? ... an "array of pointers" and a "pointer ... Allocate space for some number of pointers to char ...
    (comp.lang.c)