Number System !!!

This is a discussion on Number System !!! within the C++ Programming forums, part of the General Programming Boards category; I want to do like this: cin >> hex >> num; cout << num ; cout << dec << num; ...

  1. #1
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104

    Number System !!!

    I want to do like this:

    cin >> hex >> num;
    cout << num ;
    cout << dec << num;

    cin >> oct >> n;
    cout << n;

    cin >> base(3) >> nnn;
    cout << base(19) << nn;

    What I have to do? Have you any code ???
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You can search the board and probably find some reasonable approaches. When I did this I set up a system whereby I could convert any base to base 10 and then from base 10 to any base, but I'm sure there are different approaches.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I wrote an example program for this, but I don't have it here at work... I can post it this evening (California time).

    The decimal, hex, octal input/output is easy using cout. There is a standard function for getting input in (almost) any base [strtoul() = string-to-long]. There is a non-standard function with the Microsoft compiler to display in (almost) any base... I don't remember what it's named.

  4. #4
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104
    Hello!

    Code:
    #include<iostream.h>
    #include<iomanip.h>
    #include<stdlib.h>
    #include<string.h>
    
    int main()
    {
       char str[20];
       unsigned long int n;
    
       while(cin.getline(str,20))
       {
       	if(str[0]=='0' && str[1]=='x')
          {
             char s[20];
    
             for(int i=2;i<strlen(str);++i)
             	s[i-2]=str[i];
    
          	n = atol(s);
             cout << dec << n << endl;
          }
          else if(str[0]=='-')
          {
          	return 0;
          }
          else
          {
          	n = atol(str);
             cout << "0x";
             cout << hex << n << endl;
          }
          str[0]=0;
          n=0;
       }
    
    	return 0;
    }
    It was working but when the first segment converting hex to dec is not working as it only takes dec form and converts it to oct or hex. any suggestion. ???
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>n = atol(s);
    That won't work. It only works for base 10. You're gonna have to write your own function to go from hex->decimal.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Here's a base converter I just threw together quickly. It can convert between bases 2-36.
    Attached Files Attached Files
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Here's my example.

    Have fun!
    Attached Files Attached Files

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This has a C example, but the strtol() function can be used in C++ too.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  4. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  5. Basic C menu
    By smilk in forum C Programming
    Replies: 17
    Last Post: 05-13-2002, 09:09 AM

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