Parameters??

This is a discussion on Parameters?? within the C Programming forums, part of the General Programming Boards category; hi everyone can you help me how can i upgrade my program I want to input a octal and binary ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    61

    Parameters??

    hi everyone can you help me how can i upgrade my program I want to input a octal and binary number and convert them in base 2, 8, 10, 16..

    how can I write the scanf with the right parameter in it??
    Code:
    scanf ("%x",&i);
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    
    int main()
    {
      int i;
      char buffer [33];
      printf ("Enter a number(in hexadecimal): ");
      scanf ("%x",&i);
      
      itoa (i,buffer,10);
      printf ("Decimal: %s\n",buffer);
      itoa (i,buffer,8);
      printf ("Octal: %s\n",buffer);
      itoa (i,buffer,2);
      printf ("Binary: %s\n",buffer);
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    If the input could be using any radix, you have little choice but to use string input for the number, which would then be converted to the correct representation.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    61
    I'm sorry I don't understand you..

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    Do you want the user to type either binary or octal and have the program figure out which it is?

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    61
    No, there is a separate input for octal and binary..

  6. #6
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    Quote Originally Posted by programmerc View Post
    No, there is a separate input for octal and binary..
    Then you will have two different paths of execution. One can call scanf to read an octal value since that is built into scanf, and the other must use string input because there is no native support for binary I/O in C. What this means is that you must manually read the binary value as a string, validate that it is legal, and then convert it to other representations. I would suggest converting to decimal first, then all of the other bases are trivial:
    Code:
    #include <errno.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "util.h"
    
    int valid_binary(const char *num, const size_t bits);
    int binary_to_int(const char *num);
    
    int main(void)
        {
        int retcode = EXIT_SUCCESS;
        char *bin;
    
        fputs("binary value> ", stdout);
        fflush(stdout);
    
        errno = 0;
    
        if ((bin = dgetline(stdin)) == NULL || errno == ENOMEM)
            {
            fputs("missing or invalid input\n", stderr);
            retcode = EXIT_FAILURE;
            }
        else
            {
            /* Ensure the value is binary and in the range of int */
            if (!valid_binary(bin, CHAR_BIT * sizeof(int)))
                {
                fputs("not a valid binary value\n", stderr);
                retcode = EXIT_FAILURE;
                }
            else
                {
                int x = binary_to_int(bin);
    
                /* At this point x can easily be converted to other bases */
                printf("'%s' in binary is %d in decimal\n", bin, x);
                }
            }
    
        free(bin);
    
        return retcode;
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Are these parameters??
    By dayalsoap in forum C++ Programming
    Replies: 2
    Last Post: 03-30-2011, 02:57 AM
  2. How to get OS parameters
    By smboucou in forum C++ Programming
    Replies: 6
    Last Post: 11-26-2008, 12:15 PM
  3. Help With Parameters
    By peckitt99 in forum C++ Programming
    Replies: 30
    Last Post: 10-29-2006, 03:18 PM
  4. Parameters
    By zach0616 in forum C++ Programming
    Replies: 2
    Last Post: 08-19-2004, 04:26 PM
  5. Parameters
    By gvector1 in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2003, 12:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21