Thursday, April 15, 2010

C++ exercises: OctIn


Q: Write a function
   void OctIn(unsigned int& n);
that reads a base 8 (octal) number and assigns it to n. Use OctIn in a main program that reads the following octal numbers and prints the decimal equivalents:
   7, 177, 127, 7776, 177777

#include <iostream>

using std::cin;
using std::cout;
using std::endl;
using std::oct;

void OctIn(unsigned int& n)
{
   cin >> oct >> n;
}

int main()
{
   unsigned int n;
   cout << "Input an octal number: ";
   OctIn(n);
   cout << n << endl;

   return 0;
}

No comments:

 
Get This <