-
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.??
-
>> cout<<oct<<29;
that should work.
>> cout.flags(ios_base::oct);
that function is for testing the flags. use setf.
-
do you mean make it to like this
cout.setif(ios_base::oct);
return day;
-
other than the typo, yes. ;)
-
hey can you give me ur email address. i love yo discuss this privately
please
-
i done tat. but the solution change the integer/...
-
alright i will post it up here..
-
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_base:oct);
should be:
cout.setf(ios::oct, ios::basefield);
-
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 ?
-
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 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;
}
-
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
-
>> 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.
-
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;
}
-
hi, the problem come out with this
cannot convert `std::string' to `int' in return