CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: c

  1. #1
    Guest

    c

    This is my program and when i try to convert from decimal to octal the first it give me the right answer, the second time it give me the new answer plus the first answer. for example when i type 10 it give me 12 then the second time i type in 10 it give me 1212. could you please help me.

    #include <stdio.h>

    void decimaltobinary (void);
    void decimaltooctal (void);
    void decimaltohexa (void);

    char binary[33]="000000000000000000000000";
    char octal[12]="00000000000";
    int remainder;
    unsigned long decimal;
    int n=0;
    int i;
    int choice;
    unsigned long dec;

    void main (void)
    {

    int choice;

    do{
    do{
    printf("1. decimal to binary\n2. decimal to octal\n3. decimal to hexa\n4. Quit\n");
    printf("please enter a number: ");
    scanf("%d", &choice);
    printf("\n");
    }while (choice<1||choice>4);

    switch (choice)
    {
    case 1: decimaltobinary ();
    break;
    case 2: decimaltooctal ();
    break;
    case 3: decimaltohexa ();
    break;
    }
    }while(choice!=4);
    printf("\n");
    }

    void decimaltobinary (void)
    {
    printf("enter a decimal number to be converted to binary: ");
    scanf("%d", &decimal);

    dec=decimal;
    printf("decimal is %ld: ", dec);

    while(dec!=0)
    {
    remainder=dec%2;
    dec=dec/2;
    if (remainder==1)
    {
    binary[32-n]='1';
    }
    else
    {
    binary[32-n]='0';
    }
    ++n;
    }

    for (i=0; binary[i]=='0'; ++i)
    ;// nothing here
    for (; i<33; i++)
    {
    if ((i-1)%4==0)
    {
    printf(" ");
    }
    printf("%c", binary[i]);
    }
    printf(" in binary\n\n");
    }

    void decimaltooctal (void)
    {
    char octal[12]="00000000000";
    printf("enter a decimal number to be converted to octal: ");
    scanf("%d", &decimal);

    dec=decimal;
    printf("decimal is %ld: ", dec);

    while(dec!=0)
    {

    remainder=dec%8;
    dec=dec/8;
    switch (remainder)
    {
    case 0: octal[11-n]='0';
    break;
    case 1: octal[11-n]='1';
    break;
    case 2: octal[11-n]='2';
    break;
    case 3: octal[11-n]='3';
    break;
    case 4: octal[11-n]='4';
    break;
    case 5: octal[11-n]='5';
    break;
    case 6: octal[11-n]='6';
    break;
    case 7: octal[11-n]='7';
    break;
    }
    ++n;
    }

    for (i=0; octal[i]=='0'; ++i)
    ;// nothing here
    for (; i<12; i++)
    {
    printf("%c", octal[i]);
    }
    printf(" in octal\n\n");
    /* octal[0]=' ';
    octal[1]=' ';
    octal[2]=' ';
    octal[3]=' ';
    octal[4]=' ';
    octal[5]=' ';
    octal[6]=' ';
    octal[8]=' ';
    octal[9]=' ';
    octal[10]=' ';
    octal[11]=' ';
    octal[12]=' ';
    octal[13]=' ';
    octal[14]=' ';
    octal[15]=' ';*/
    }

    void decimaltohexa (void)
    {
    printf("enter a decimal number to be converted to hex: ");
    scanf("%d", &decimal);

    dec=decimal;
    printf("decimal is %ld: ", dec);

    while(dec!=0)
    {
    remainder=dec%16;
    dec=dec/16;
    switch (remainder)
    {
    case 0: octal[11-n]='0';
    break;
    case 1: octal[11-n]='1';
    break;
    case 2: octal[11-n]='2';
    break;
    case 3: octal[11-n]='3';
    break;
    case 4: octal[11-n]='4';
    break;
    case 5: octal[11-n]='5';
    break;
    case 6: octal[11-n]='6';
    break;
    case 7: octal[11-n]='7';
    break;
    case 8: octal[11-n]='8';
    break;
    case 9: octal[11-n]='9';
    break;
    case 10: octal[11-n]='A';
    break;
    case 11: octal[11-n]='B';
    break;
    case 12: octal[11-n]='C';
    break;
    case 13: octal[11-n]='D';
    break;
    case 14: octal[11-n]='E';
    break;
    case 15: octal[11-n]='F';
    break;
    }
    ++n;
    }

    for (i=0; octal[i]=='0'; ++i)
    ;// nothing here
    for (; i<12; i++)
    {
    printf("%c", octal[i]);
    }
    printf(" in octal\n\n");
    }


  2. #2
    Join Date
    Sep 1999
    Location
    Michigan
    Posts
    215

    Re: c

    The previous respondent is correct on the changes needed to correct the symptoms that you describe. Since you traverse from right to left in filling the octal string, when you start another number, you start filling the new octal string from the last place of the previous number. This does not deserve a negative evaluation ...

    - Mike


  3. #3
    Guest

    Re: c

    Thanks a lot.


  4. #4
    Guest

    Re: c

    Thanks alot


  5. #5
    Join Date
    Dec 2004
    Location
    Bahrain
    Posts
    15

    Re: c

    The same program but by using recursive function named DecimalToBinary that accepts an integer number n as parameter

    C++

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: c

    [ Moved thread ]

    Just an aside note for OP:

    When posting code it's not necessary to replace '<' and '>' with '&lt;' and '&gt;' respectively.
    However, to be easier to read, place it between [CODE] tags.
    Pease take a look here to see the usage.
    Last edited by ovidiucucu; October 4th, 2005 at 03:00 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: c

    Talk about bringing an ancient thread back to life
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  8. #8
    Join Date
    Dec 2004
    Location
    Bahrain
    Posts
    15

    Re: c

    recursive function named DecimalToBinary that accepts an integer number n as parameter

    C++

    It's just like a practice

  9. #9
    Join Date
    Sep 2005
    Location
    Cluj-Napoca, Romania
    Posts
    161

    Re: c

    maybe a better exercise would be to transform from any base to any base (including those different from 2, 8, 10 or 16, for example from base 22 to base 13). these bases and the initial number should be read from input, and the input should be checked for errors (for example inputing a base bigger than all the letters and digits could express, or inputing the number FFA when the initial base is 14, etc). the input number should be case insensitive (ffA and FFA should lead to the same result).

  10. #10
    Join Date
    Dec 2004
    Location
    Bahrain
    Posts
    15

    Re: c

    #include<iostream.h>
    void decimalToBinary(int);


    void main(){
    int num;

    cout<<"Enter decimal number: ";
    cin>>num;
    cout<<endl;
    cout<<"The binary is: ";

    decimalToBinary(num);
    cout<<endl;
    }

    void decimalToBinary(int n)
    {
    if (n==0)
    return;
    else{
    decimalToBinary(n/2);
    cout<<n%2;
    }


    }


    That's it

  11. #11
    Join Date
    Sep 2005
    Location
    Cluj-Napoca, Romania
    Posts
    161

    Re: c

    well, now do it the way I proposed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured