Decimal

Oct 14, 2012 at 1:02am
Program that would output the decimal number of the octal number you have entered. This code is working but it is not correct :/


int main()
{

int input;
cout << "Enter an octal number:";
cin >> input;
cout << "\nDecimal number = " <<dec<< input << "\n";

system("pause>0");
return 0;
}
Oct 14, 2012 at 1:23am
The default input/output mode is decimal. cin >> input; inputs a number in decimal. You have to tell it to use octal.
Oct 14, 2012 at 1:33am
It still dont display the decimal number of the octal number that i have put..
Oct 14, 2012 at 1:41am
What is your current code?
Oct 14, 2012 at 1:47am
here...
int main()
{
int octal;
int num;
cout << "Enter an octal number:";
cin >> octal;
cout << "\nDecimal number ="<<num<<octal << "\n";
cin >> num;
system("pause>0");
return 0;
}

I tried to delete 'cin >> num;' but it still wont work.
Last edited on Oct 14, 2012 at 1:47am
Oct 14, 2012 at 1:49am
cin >> octal;
Just because the variable is named octal does not mean it actually takes input as octal and converts it to binary representation in memory. This statement still takes input in decimal format.
Oct 14, 2012 at 1:56am
Oh? so... what should i put insted of cin >> octal?
Oct 14, 2012 at 2:00am
http://www.cplusplus.com/reference/iostream/manipulators/oct/
For example,
1
2
std::cin >> std::oct >> MyIntVariable;
std::cout << MyIntVariable; //std::dec is default, you don't need to specify it 
Topic archived. No new replies allowed.