Re: A base conversion~ help me to correct it since it can't run
- From: "santosh" <santosh.k83@xxxxxxxxx>
- Date: 4 Mar 2007 08:07:46 -0800
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.
.
- References:
- Prev by Date: Re: pointer to itself
- Next by Date: Re: A base conversion~ help me to correct it since it can't run
- Previous by thread: Re: A base conversion~ help me to correct it since it can't run
- Next by thread: Re: A base conversion~ help me to correct it since it can't run
- Index(es):
Relevant Pages
|