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

J

jyck91

// 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 = getchar(); // sub. the number into string
if (num == '\n')
{num = '\0'; // if the above statement is true , num 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 == '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 = number % base;
number = number / base;
}
temp = number;
printf("The number in %u base is : ",base);
for (i=29;i<30;i--)
{
if (temp<10)
printf("%u",temp);
if (temp>10 || temp<36)
printf("%uc",temp+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
 
C

Cong Wang

// 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 = getchar(); // sub. the number into string
if (num == '\n')


'num' is an array, but '\n' is a char. You can NOT compare them.
{num = '\0'; // if the above statement is true , num 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 == '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 = number % base;
number = number / base;
}
temp = number;
printf("The number in %u base is : ",base);
for (i=29;i<30;i--)
{
if (temp<10)
printf("%u",temp);
Suspicious.

if (temp>10 || temp<36)
printf("%uc",temp+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.
 
O

osmium

// 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 = getchar(); // sub. the number into string
if (num == '\n')
{num = '\0'; // if the above statement is true , num 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 == '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 = number % base;
number = number / base;
}
temp = number;
printf("The number in %u base is : ",base);
for (i=29;i<30;i--)
{
if (temp<10)
printf("%u",temp);
if (temp>10 || temp<36)
printf("%uc",temp+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 = getchar(); // sub. the number into string
if (num == '\n')
{num = '\0'; // if the above statement is true , num 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 == '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 = number % base;
number = number / base;
}
temp = number;
printf("The number in %u base is : ",base);
for (i=29;i<30;i--)
{
if (temp<10)
printf("%u",temp);
if (temp>10 || temp<36)
printf("%uc",temp+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
 
O

osmium

osmium said:
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.
 
S

santosh

// 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 = 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 == '\n'.
{num = '\0'; // if the above statement is true , num 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 == '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 = number % base;
number = number / base;
}
temp = 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<10)
printf("%u",temp);


ITYM an element of temp.
if (temp>10 || temp<36)
printf("%uc",temp+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.
 
S

santosh

osmium said:
<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.
 
O

osmium

santosh said:
osmium wrote:
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 = getchar(); // sub. the number into string
if (num == '\n')
{num = '\0'; // if the above statement is true , num 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 == '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 = number % base;
number = number / base;
}
temp = number;
printf("The number in %u base is : ",base);
for (i=29;i<30;i--)
{
if (temp<10)
printf("%u",temp);
if (temp>10 || temp<36)
printf("%uc",temp+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..
 
C

CBFalconer

.... 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
 
J

jyck91

(e-mail address removed) 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.
 
R

Richard Heathfield

(e-mail address removed) said:

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.
 
C

CBFalconer

Richard said:
(e-mail address removed) said:



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
 
R

Richard Heathfield

CBFalconer said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top