Help !!!

This is a discussion on Help !!! within the C++ Programming forums, part of the General Programming Boards category; //Where am I going wrong? //I am trying to convert numbers from one base to // any other base. // ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    Unhappy Help !!!

    //Where am I going wrong?
    //I am trying to convert numbers from one base to
    // any other base.


    // Converts numbers from any base to any other base.


    #include <iostream.h>


    int main()

    {


    int i;


    do
    {
    cout << "Enter a number:" << endl;
    cin >> i ;
    dec(cout); // Set output to decimal
    cout << '\" << i << '\" << " = " << i;
    oct(cout); // Set out put to octal
    cout << " =0" << i;
    hex(cout); // Set output to hex
    cout << " = 0x" << i << '\n';

    }while ( i != 0);
    return 0;
    }

  2. #2
    Registered User bljonk's Avatar
    Join Date
    Oct 2001
    Posts
    70

    ios::dec need it

    you should include the <imanip.h> header file to use the "ios class" to manipulate the I/O stream.

    read about it.

  3. #3
    Banned maes's Avatar
    Join Date
    Aug 2001
    Posts
    744
    >> cout << '\" << i << '\" << " = " << i;
    I don't really understand why you are using '\'
    If it is just for layout you have to use '\\'
    you can't mix ' and "
    Change this line with :
    cout << '\\' << i << '\\' << " = " << i;
    and it should work.

    bye

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    I have an addition to problem with HELP!!!

    Originally posted by maes
    >> cout << '\" << i << '\" << " = " << i;
    I don't really understand why you are using '\'
    If it is just for layout you have to use '\\'
    you can't mix ' and "
    Change this line with :
    cout << '\\' << i << '\\' << " = " << i;
    and it should work.

    bye
    #include <iostream.h>
    void to_binary(int i);

    int main()

    {


    int i;



    {
    cout << "Enter a number:" << endl;
    cin >> i ;
    dec(cout); // Set output to decimal
    cout << '\\' << i << '\\' << " = " << i;
    oct(cout); // Set output to octal
    cout << " = 0" << i;
    hex(cout); // Set output to hex
    cout << " = 0x" << i << '\n';
    //bin(cout); // Set output to binary
    //cout << " = 0" << i << '\n';

    }
    return 0;
    }

    {
    void to_binary(int n) //recursive function
    }
    {
    int r;
    r = n % 2;

    if (n >= 2)
    to_binary(n / 2);
    putchar('0' + r);
    return;
    }

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    You picked the one wacky character for your format. The '\' is special character that creates escapes sequences. If you want to output a \ then you need to output '\\' this says output a backslash. '\' This says I've created an incomplete escape sequence.

    \a = bell
    \b = backspace
    \f = formfeed
    \n = newline
    \r = carriage return
    \r = horizontal tab
    and so on...

  6. #6
    Banned maes's Avatar
    Join Date
    Aug 2001
    Posts
    744
    if you want to put a \ onthe screen you have to use '\\'
    because \ lets the compiler know that a special character will follow example: \n \t etc. \ is also a special character so you need to use '\\'

    when you write a function, you don't have to put it between {}only the code.
    but do it like this:
    void function()
    {
    //here comes the code
    }
    also include <stdio.h> for putchar (but you had this the first time )
    here 's the code

    #include <stdio.h>
    #include <iostream.h>
    void to_binary(int i);

    int main()

    {


    int i;



    {
    cout << "Enter a number:" << endl;
    cin >> i ;
    dec(cout); // Set output to decimal
    cout << '\\' << i << '\\' << " = " << i;
    oct(cout); // Set output to octal
    cout << " = 0" << i;
    hex(cout); // Set output to hex
    cout << " = 0x" << i << '\n';
    to_binary(i);
    //bin(cout); // Set output to binary
    //cout << " = 0" << i << '\n';

    }
    return 0;
    }


    void to_binary(int n) //recursive function
    {
    int r;
    r = n % 2;

    if (n >= 2)
    to_binary(n / 2);
    putchar('0' + r);
    return;
    }

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    Need HELP Again

    How can I use the cout and cin function to enter an integer whose input base is 2 and the value is 10000110, then the value in base 10 is 136?

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    We'll always hold data as binary. It is only how we choose to represent the data that changes. So the focus should be on cout to output the value as decimal. And you're done that.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    HELP again

    This I know I have the output for decimal but I would like to know how to cout the input from on base to another in binary. Any suggestions?

  10. #10
    Banned maes's Avatar
    Join Date
    Aug 2001
    Posts
    744
    Do you want to input as binary value and display it as a decimal number?

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    Reply

    I want to input it as a decimal and a binary and cout it as a decimal binary oct and hex numbers.
    I believe I am on the right track, yet i cannot get the right code to input it as a binary and output it as all four in a different base. And vise versa. Thanks for the help.

  12. #12
    Banned maes's Avatar
    Join Date
    Aug 2001
    Posts
    744
    I don't know how to read it directly as a binary number, but you can also read iy as a string of characters and then convert it.
    like this:

    #include <stdio.h>
    #include <iostream.h>
    #include <math.h>
    #include <string.h>
    #include <conio.h>

    int main()

    {
    int i,bin=0,k=0;
    char string[20];
    cout<<"Give a binary number";
    cin>>string;
    i=strlen(string);//get the stringlegth
    for(int j=i;j>0;j--)//start with the last number
    { // which is the least significant bit
    if(string[j-1]=='1')
    bin=bin+pow(2,k);
    k++;
    }
    cout<<bin;
    return 0;
    }

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    Onather question?

    How can i do this ? To add to the output.

    cout<<" base 2:" << bin << endl;

    cout<<" Deciaml:" << bin << endl;
    cout<<" Octal:" << bin << endl;
    cout<<" Hexadecimal:" << bin << endl;

  14. #14
    Banned maes's Avatar
    Join Date
    Aug 2001
    Posts
    744
    Maybe it's my english, but I don't realy understand this question.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    ok

    Sorry,
    what I am asking is: how can I get the input from one base and give the value in other bases. For example, base 2, 8, 10, 16. Also I would like to take an integer and output it as a binary number in a different base. Is all this possible and is mycode on the right track. I have a problem joining the two different codes into one program.
    Thanks

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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