hi.. :) i'm a first year college student and just starting with turbo c. We have a project that is to be pass on August 29. I hope someone can help me cause i'm really having a hard time with this. I have to make an exe. of the program that gives three options to the user. First is to convert decimal to binary, hexadecimal to decimal, and octal to binary. After converting, the user must have an option whether to [1] try again, [2] go back to the main menu [3]exit the program. I also tried to include conio.h but it says there "unable to open include file conio.h". what must be the reason for that error?

please help me :(
THANKS ALOT

>>i'm a first year college student and just starting with turbo c.
OMG go to a different college immediately because you won't learn anything where you are at. That compiler has been obsolete for at least 15 years that I know of.

>>unable to open include file conio.h
Check the compiler's include directory to see if that file really exists or not.

conio.h is not supported in Unix C. Are you using Unix C?
You must be using conio.h probably for getch() I guess, if so you may use getchar().

I doubt very much if he has a lot of choices right now (after the school year has started), in what compiler the class is using, or what college to attend based on the compiler being used.

Turbo C is no problem, but it sounds like you need to use getchar(), instead of getch(), if you don't have conio.h, or the file is somehow corrupt. This was mentioned above. I can send you the conio.h file, if your file is damaged.

The heart of the matter is doing the conversions. Have you learned how to do these conversions? If so, post up your code to do them, and I'll help you get the flow of your program's menu's, working.

Note that I have no intention of helping you with doing the conversions. If you didn't read your book, or stay awake during the lecture, you're out of luck with me.

I know how to make the flow of the program, my only problem now is the conversion from hex to dec and oct to bin.

I know how to make the flow of the program, my only problem now is the conversion from hex to dec and oct to bin.

Do you, youself, know how to convert from hex to dec and from oct to bin? If you know, then program your program to do as what you would do in real life.

#include <stdio.h>
#include <conio.h>
void main ()
{
  int n, a[30],i=0;
  printf("\nEnter a decimal Integer:");
  scanf("%d",&n);
  while(n!=0)
  {
    a[i]=n%2;
    n=n/2;
    i++;
  }
  printf("\nBinary number is ");
  for(--i;i>=0;--i)
    printf("%d",a[i]);
}

that is the code for decimal to binary... God bless...

Comments
What grade are you getting on his homework?

>that is the code for decimal to binary...
That's pretty awful. At least it kind of works.

>#include <conio.h>
You don't even use anything from this header, why destroy portability by including it?

>void main()
Here is the only correct definition of main with no parameters:

int main ( void )
{
  /* Your code here */

  return 0;
}

The return value can be any integer you want, but the only portable ones are 0, EXIT_SUCCESS, and EXIT_FAILURE. The latter two are macros defined in <stdlib.h>. Anything except what I've described is non-portable. For a definition of main that takes parameters, the following is the only correct definition:

int main ( int argc, char *argv[] )
{
  /* Your code here */

  return 0;
}

There are only two parameters. The first is an integer and the second is a pointer to the first element of an array of pointers to char. Any equivalent type is allowed, and the identifiers can be whatever you want (though argc and argv are conventional), for example:

typedef int FOO;

int main ( FOO ac, char **av )
{
  /* Your code here */

  return 0;
}

Anything else is non-portable.

>int n, a[30],i=0;
I'm gradually becoming more and more hostile toward this practice. It's a great way to hide variable declarations from readers, and it's also a great way to make maintenance more difficult. I've reached this conclusion after having to change the type on a large number of variables defined this way. I feel it's better to place a single variable definition on a line by itself.

>printf("\nEnter a decimal Integer:");
A leading newline is unnecessary, it's also a good idea to add a space after the colon to distinguish the input from the prompt. Finally, it's not guaranteed that the prompt will be visible before scanf starts blocking for input because output streams in C are only flushed in three cases:

1) The internal buffer fills up and must be flushed for the stream to continue working as intended (but you can't determine when this happens).
2) A newline is printed to the stream, in which case the buffer is automatically flushed.
3) fflush is called explicitly

Your code is relying on #1 to happen, or unwittingly relying on a implementation extension that flushes automatically whether you wanted to or not.

>scanf("%d",&n);
Always check input for failure.

>while(n!=0)
What if the user enters 0? Is that not a valid decimal integer? Don't forget your edge cases.

>n=n/2;
There's an operator for this specific case: /=.

>for(--i;i>=0;--i)
If you do the same thing in the initialization and update clauses, you might as well just turn the loop into a while and update there.

Finally, printf is a very heavy function (I feel I'm repeating myself...), so if you're printing just a string literal or a character literal, you're better off using something in the puts or putchar families. Here's your code with my fixes:

#include <stdio.h>

int main ( void )
{
  int a[30] = {0};
  int i = 0;
  int n;

  fputs ( "Enter a decimal integer: ", stdout );
  fflush ( stdout );

  if ( scanf ( "%d", &n ) == 1 ) {
    fputs ( "The binary value is ", stdout );

    if ( n == 0 )
      ++i;

    while ( n != 0 ) {
      a[i++] = n % 2;
      n /= 2;
    }

    while ( --i >= 0 )
      printf ( "%d", a[i] );

    putchar ( '\n' );
  }

  return 0;
}

Thanks but I like to do things on my way but still Tnx...

If what you meant is that you like to learn at your own pace, and experiment and prove the conclusions you come with, as true, then I commend you for it. Just a word of caution: it is not necessary to re-invent the wheel to learn.

However, your statement is ambiguous at best. Every time the word "but" is used, whatever came before that "but" is negated or made unimportant, as irrelevant.
Like "thanks, but not thanks".
In that case, I'll say to you: Narue's advise is not an option. It is the way you should learn.

This question has already been answered. Start a new discussion instead.