Thread: octal number problem ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    141

    octal number problem ...

    hi everyone, i would like to convert int to base 8 (octal)...

    this is wat i think of my integer = 29 and the octal will be35

    the process is cout<<oct<<29; this way i couldn't output the

    and also this method
    Code:
        cout.flags(ios_base::oct);
        return day;
    is there any other method please.....or any hints.??

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> cout<<oct<<29;

    that should work.

    >> cout.flags(ios_base:ct);

    that function is for testing the flags. use setf.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    do you mean make it to like this

    cout.setif(ios_base:ct);
    return day;

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    other than the typo, yes.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    hey can you give me ur email address. i love yo discuss this privately
    please

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    i done tat. but the solution change the integer/...

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    alright i will post it up here..

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I've misinformed you quite a bit:

    >> that function is for testing the flags. use setf.

    wrong. it does set the flags.

    >> cout.setif(ios_basect);

    should be:

    cout.setf(ios:ct, ios::basefield);
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    this is my code and the solution should be like this
    input 29
    output 35

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    #include <sstream>	//for stringstreams
    #include <string>	//for strings
    #include <bitset>
    using namespace std;
    int octalDay(int day);
    int main()
    {
        int month;
        int year, day;
    
    //prompt user input    
        cout<<"Please enter your date of birth [dd mm yyyy]: ";
        cin>>day>>month>>year;
        
    //display the function from question A - E    
        cout<<octalDay(day)<<endl;
        
    
    //make those integers into string  
    
        int a = octalDay(day);
    
    
    // Create/initialize a stringstream
        stringstream sstr;
        sstr << a ;
    
    // Convert stringstream to a string
        string myString = sstr.str();
    
    // Output string
        cout << myString;
    	
        system ("pause");
        return 0;
    }    
    
    //find the base 8 of the day
    int octalDay(int day)
    {
        cout.setf(ios_base::oct);
        return day;
    }
    the first time i cout it, the solution is 35 [correct]
    but when i make it to string.. it cout 4..
    can someone tell me why ?

  10. #10
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Octal is just a number format for writing text.

    a function like
    Code:
    int octalDay(int day)
    {
        cout.setf(ios_base::oct);
        return day;
    }
    does nothing. an int is an int is an int!

    Code:
    int x = 255 // == 0xFF == 0377
    if you want to convert a int (binary value) to an octal string (text representation) use a stringstream or other way

    Code:
    stringstream str;
    string test;
    
    int x = 255;
    str << x;
    str >> oct >> test; // test == "0377"
    
    str << x;
    str >> hex >> test; // test == "0xFF"
    
    str << oct << "10";
    str >> x; // x == decimal 8
    str << x;
    str >> dec >> test; // test == "8"
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    i am sorry that i dont understand what you mean but instead of using setf

    can i use flags
    Code:
    int octalDay(int day)
    {
        cout.flags(ios_base::oct);
        return day;
    }

  12. #12
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    i want to make the integer into string .. but what i bein saying on the top is
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    #include <sstream>	//for stringstreams
    #include <string>	//for strings
    #include <bitset>
    using namespace std;
    int octalDay(int day);
    int main()
    {
        int month;
        int year, day;
    
    //prompt user input    
        cout<<"Please enter your date of birth [dd mm yyyy]: ";
        cin>>day>>month>>year;
        
    //display the function from question A - E    
        cout<<octalDay(day)<<endl;
        
    
    //make those integers into string  
    
        int a = octalDay(day);
    
    
    // Create/initialize a stringstream
        stringstream sstr;
        sstr << a ;
    
    // Convert stringstream to a string
        string myString = sstr.str();
    
    // Output string
        cout << myString;
    	
        system ("pause");
        return 0;
    }    
    
    //find the base 8 of the day
    int octalDay(int day)
    {
        cout.setf(ios_base::oct);
        return day;
    }
    the first time i cout it, the solution is 35 [correct] orange colour
    but when i make it to string.. it cout 4. = yellow colour

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> cout.flags(ios_base::oct);
    All that does is change how cout outputs the number. That is why it works when you output the int with cout. However, sstr is a completely different stream than cout, and so any changes in how you make cout format the number don't affect sstr.

    Your octalDay function doesn't change day at all, it just modifies cout so that the next time you output a number it will display in octal format. Remember that the stream flags affect the way the number is displayed, it doesn't change the actual number or how it is stored in memory.

    To get your stringstream to use octal format, you would use the same technique that worked for cout.

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Stringstreams... bah.

    Code:
    string dec_to_oct(int n)
    {
    	string str;
    	for(int i = 0, t = 0; n >> i; i += 3)
    	{
    		t = ((n >> i) & 1) * 1 + ((n >> (i + 1)) & 1) * 2 + ((n >> (i + 2)) & 1) * 4;
    		str = (char)(t + 0x30) + str;
    	}
    	return str;
    }
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  15. #15
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    hi, the problem come out with this
    cannot convert `std::string' to `int' in return

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Random Number Range Problem.
    By xamlit in forum C Programming
    Replies: 11
    Last Post: 01-26-2006, 12:55 PM
  3. small problem
    By ucfmustang in forum C Programming
    Replies: 1
    Last Post: 02-10-2003, 04:55 PM
  4. number ouput problem...
    By o0obruceleeo0o in forum C++ Programming
    Replies: 7
    Last Post: 11-17-2002, 06:56 AM
  5. problem with my prime number program
    By datainjector in forum C Programming
    Replies: 4
    Last Post: 07-12-2002, 12:30 PM