By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,678 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,678 IT Pros & Developers. It's quick & easy.

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

P: n/a
// Base Conversion
// Aim: This program is to convert an inputted number
// from base M into base N. Display the converted
// number in base N.
#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
{ num[i] = getchar(); // sub. the number into string
if (num == '\n')
{num[i] = '\0'; // if the above statement is true , num[i] will
equal end of string
true = 0 ; }} // end
fflush(stdin); // wash away the excess char
for(i = strlen(num) - 1; i >=0; i--)
{if(num[i] == '1')
base10 = base10 + 1 * pow(2 , strlen(num)- 1 - i);
}
}

// 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;
printf("The number in %u base is : ",base);
for (i=29;i<30;i--)
{
if (temp[i]<10)
printf("%u",temp);
if (temp[i]>10 || temp[i]<36)
printf("%uc",temp[i]+51);
}
}

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);
printf("Base M (2 to 36): ");
scanf("%d",&m);
printf("Base N (2 to 36): ");
scanf("%d",&n);
printf("Result is %d \n", result);
// 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");
}

// The End Of Main Program

Mar 4 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a

jy****@gmail.com 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.
/**/ style is better in C.
#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
I haven't read all of the code. Are you sure you want 'true=1'? Should
it be 'true == 1'?
{ num[i] = getchar(); // sub. the number into string
if (num == '\n')
'num' is an array, but '\n' is a char. You can NOT compare them.
{num[i] = '\0'; // if the above statement is true , num[i] will
equal end of string
true = 0 ; }} // end
fflush(stdin); // wash away the excess char
for(i = strlen(num) - 1; i >=0; i--)
{if(num[i] == '1')
base10 = base10 + 1 * pow(2 , strlen(num)- 1 - i);
Are you sure you should use pow instead of '<<'? If so, why don't you
include math.h?
}
}

// 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;
printf("The number in %u base is : ",base);
for (i=29;i<30;i--)
{
if (temp[i]<10)
printf("%u",temp);
Suspicious.
if (temp[i]>10 || temp[i]<36)
printf("%uc",temp[i]+51);
}
}

main()
'int main(void)' is better.
{
// 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);
printf("Base M (2 to 36): ");
scanf("%d",&m);
printf("Base N (2 to 36): ");
scanf("%d",&n);
printf("Result is %d \n", result);
// 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");
'Pause' is NOT available in Linux, so that's NOT portable.
}

// The End Of Main Program
Mar 4 '07 #2

P: n/a
<jy****@gmail.comwrote:
// Base Conversion
// Aim: This program is to convert an inputted number
// from base M into base N. Display the converted
// number in base N.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 20
<snip>

The following code compiles and runs on MingW but produces the wrong answer.
I fixed some problems caused by line breaks on newsgroups and put in what
was my guess as to what you wanted in a few places. I didn't attack - or
even look at - the logic. As I understand your question you wanted to be
able to run. it runs now, for me. I didn't draw attention to changes I
made, you should be able to see what I did from the error messages you get.
No error message was far removed from the error message.

I find your user prompts confusing. How about :input base, output base and
nbr?
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
{ num[i] = getchar(); // sub. the number into string
if (num == '\n')
{num[i] = '\0'; // if the above statement is true , num[i] will
equal end of string
true = 0 ; }} // end
fflush(stdin); // wash away the excess char
for(i = strlen(num) - 1; i >=0; i--)
{if(num[i] == '1')
base10 = base10 + 1 * pow(2 , strlen(num)- 1 - i);
}
}

// 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;
printf("The number in %u base is : ",base);
for (i=29;i<30;i--)
{
if (temp[i]<10)
printf("%u",temp);
if (temp[i]>10 || temp[i]<36)
printf("%uc",temp[i]+51);
}
}

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);
printf("Base M (2 to 36): ");
scanf("%d",&m);
printf("Base N (2 to 36): ");
scanf("%d",&n);
printf("Result is %d \n", result);
// 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");
}

// The End Of Main Program

>
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
{ num[i] = getchar(); // sub. the number into string
if (num == '\n')
{num[i] = '\0'; // if the above statement is true , num[i] will
equal end of string
true = 0 ; }} // end
fflush(stdin); // wash away the excess char
for(i = strlen(num) - 1; i >=0; i--)
{if(num[i] == '1')
base10 = base10 + 1 * pow(2 , strlen(num)- 1 - i);
}
}

// 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;
printf("The number in %u base is : ",base);
for (i=29;i<30;i--)
{
if (temp[i]<10)
printf("%u",temp);
if (temp[i]>10 || temp[i]<36)
printf("%uc",temp[i]+51);
}
}

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);
printf("Base M (2 to 36): ");
scanf("%d",&m);
printf("Base N (2 to 36): ");
scanf("%d",&n);
printf("Result is %d \n", result);
// 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");
}

// The End Of Main Program

Mar 4 '07 #3

P: n/a
"osmium" <r1********@comcast.netwrote:
The following code compiles and runs on MingW but produces the wrong
answer. I fixed some problems caused by line breaks on newsgroups and put
in what was my guess as to what you wanted in a few places. I didn't
attack - or even look at - the logic. As I understand your question you
wanted to be able to run. it runs now, for me. I didn't draw attention
to changes I made, you should be able to see what I did from the error
messages you get. No error message was far removed from the error message.
No error message was far removed from the error.
Mar 4 '07 #4

P: n/a
jy****@gmail.com 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.

Mar 4 '07 #5

P: n/a
osmium wrote:
<jy****@gmail.comwrote:
// Base Conversion
// Aim: This program is to convert an inputted number
// from base M into base N. Display the converted
// number in base N.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 20

<snip>

The following code compiles and runs on MingW but produces the wrong answer.
I fixed some problems caused by line breaks on newsgroups and put in what
was my guess as to what you wanted in a few places.
<snip>

I did not notice any changes in your reposting of the OP's code.

Mar 4 '07 #6

P: n/a
"santosh" wrote:
osmium wrote:
>The following code compiles and runs on MingW but produces the wrong
answer.
I fixed some problems caused by line breaks on newsgroups and put in what
was my guess as to what you wanted in a few places.
I did not notice any changes in your reposting of the OP's code.
// Base Conversion
// Aim: This program is to convert an inputted number
// from base M into base N. Display the converted
// number in base N.
#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

^^^^^

{ num[i] = getchar(); // sub. the number into string
if (num[i] == '\n')
{num[i] = '\0'; // if the above statement is true , num[i] will

^^^^

//equal end of string

true = 0 ; }} // end
fflush(stdin); // wash away the excess char
for(i = strlen(num) - 1; i >=0; i--)
{if(num[i] == '1')
base10 = base10 + 1 * pow(2 , strlen(num)- 1 - i);
}
}

// 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;
printf("The number in %u base is : ",base);
for (i=29;i<30;i--)
{
if (temp[i]<10)
printf("%u",temp);
if (temp[i]>10 || temp[i]<36)
printf("%uc",temp[i]+51);
}
}

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);
printf("Base M (2 to 36): ");
scanf("%d",&m);
printf("Base N (2 to 36): ");
scanf("%d",&n);
printf("Result is %d \n", result);
// 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");
}

// The End Of Main Program

I'll be darned! My post was a problem to me because of the OPs character
set and I will blame it on that. Here is another cut and paste from my
compiler's editor. I have marked the two actual changes I recall, but there
may be more..
Mar 4 '07 #7

P: n/a
jy****@gmail.com wrote:
>
.... snip ...

Put your question in the body of the article in future.

Try this:

#include "dispbase.h"

/* ============================================ */
/* Mask and convert digit to hex representation */
/* Output range is 0..9 and a..f only */
int tio_hexify(unsigned int value)
{
int result;

result = (value & 0x0f) + '0';
if (result '9')
result = result + ('a' - '0' - 10);
return result;
} /* tio_hexify */

/* ========================================= */
/* convert number to string in various bases */
/* 2 <= base <= 16, controlled by hexify() */
/* Output string has ls digit first. */
void tio_basedisplay(unsigned long number, unsigned int base,
char * string, size_t maxlgh)
{
/* assert (string[maxlgh]) is valid storage */
if (!maxlgh) {
*string = '\0';
return;
}
else {
*string = tio_hexify(number % base);
if (!number) *string = '\0';
else {
tio_basedisplay(number / base, base, &string[1], maxlgh -
1);
}
}
} /* tio_basedisplay */

/* ======================= */
/* reverse string in place */
void tio_revstring(char * string)
{
char * last, temp;

last = string + strlen(string); /* points to '\0' */
while (last-- string) {
temp = *string; *string++ = *last; *last = temp;
}
} /* tio_revstring */

And the header file:

#ifndef dispbase_h_
#define dispbase_h_

#include <string.h>

/* ============================================ */
/* Mask and convert digit to hex representation */
/* Output range is 0..9 and a..f only */
int tio_hexify(unsigned int value);

/* ========================================= */
/* convert number to string in various bases */
/* 2 <= base <= 16, controlled by hexify() */
/* Output string has ls digit first. */
void tio_basedisplay(unsigned long number, unsigned int base,
char * string, size_t maxlgh);

/* ======================= */
/* reverse string in place */
void tio_revstring(char * string);

#endif
--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Mar 4 '07 #8

P: n/a
On 3月5日, 上午1時14分, CBFalconer <cbfalco...@yahoo.comwrote:
jyc...@gmail.com wrote:

... snip ...

Put your question in the body of the article in future.

Try this:

#include "dispbase.h"

/* ============================================ */
/* Mask and convert digit to hex representation */
/* Output range is 0..9 and a..f only */
int tio_hexify(unsigned int value)
{
int result;

result = (value & 0x0f) + '0';
if (result '9')
result = result + ('a' - '0' - 10);
return result;

} /* tio_hexify */

/* ========================================= */
/* convert number to string in various bases */
/* 2 <=base<= 16, controlled by hexify() */
/* Output string has ls digit first. */
void tio_basedisplay(unsigned long number, unsigned intbase,
char * string, size_t maxlgh)
{
/* assert (string[maxlgh]) is valid storage */
if (!maxlgh) {
*string = '\0';
return;
}
else {
*string = tio_hexify(number %base);
if (!number) *string = '\0';
else {
tio_basedisplay(number /base,base, &string[1], maxlgh -
1);
}
}

} /* tio_basedisplay */

/* ======================= */
/* reverse string in place */
void tio_revstring(char * string)
{
char * last, temp;

last = string + strlen(string); /* points to '\0' */
while (last-- string) {
temp = *string; *string++ = *last; *last = temp;
}

} /* tio_revstring */

And the header file:

#ifndef dispbase_h_
#define dispbase_h_

#include <string.h>

/* ============================================ */
/* Mask and convert digit to hex representation */
/* Output range is 0..9 and a..f only */
int tio_hexify(unsigned int value);

/* ========================================= */
/* convert number to string in various bases */
/* 2 <=base<= 16, controlled by hexify() */
/* Output string has ls digit first. */
void tio_basedisplay(unsigned long number, unsigned intbase,
char * string, size_t maxlgh);

/* ======================= */
/* reverse string in place */
void tio_revstring(char * string);

#endif

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
I am sorry about that are some bugs on this programe that the complier
can not run it in DVE-C++.
Since I am not sure understand the errors, I can not correct them all
by following the instructions.

Mar 5 '07 #9

P: n/a
jy****@gmail.com said:

<snip>
I am sorry about that are some bugs on this programe that the complier
can not run it in DVE-C++.
Since I am not sure understand the errors, I can not correct them all
by following the instructions.
It works fine here, once supplied with a driver.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 5 '07 #10

P: n/a
Richard Heathfield wrote:
jy****@gmail.com said:

<snip>
>I am sorry about that are some bugs on this programe that the complier
can not run it in DVE-C++.
Since I am not sure understand the errors, I can not correct them all
by following the instructions.

It works fine here, once supplied with a driver.
It's old code of mine, and has two known faults. Dependence on
Ascii, in hexify. Undefined behaviour in reverse_str on empty
string. Both unlikely to bother the OP, and fairly easily
corrected.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Mar 5 '07 #11

P: n/a
CBFalconer said:
Richard Heathfield wrote:
>jy****@gmail.com said:

<snip>
>>I am sorry about that are some bugs on this programe that the
complier can not run it in DVE-C++.
Since I am not sure understand the errors, I can not correct them
all by following the instructions.

It works fine here, once supplied with a driver.

It's old code of mine, and has two known faults. Dependence on
Ascii, in hexify.
Trivially fixed:

int tio_hexify(unsigned int value)
{
return "0123456789ABCDEF"[value & 0x0f];
} /* tio_hexify */
Undefined behaviour in reverse_str on empty
string.
Again, trivially fixed:

void tio_revstring(char * string)
{
char * last, temp;

last = string + strlen(string); /* points to '\0' */
while (last string) {
temp = *string; *string++ = *--last; *last = temp;
}
} /* tio_revstring */

Both unlikely to bother the OP, and fairly easily corrected.
Aye.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 5 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.