1
\$\begingroup\$

This is a simple program converting user input decimal numbers into octal ones.

#include <iostream>
using namespace std;
main()
{
    int de,oc,y,i=1,octal;
    float decimal,deci,x;
    cout<<"Enter decimal no :: ";
    cin>>decimal;
    de=decimal;
    deci=decimal-de;
    cout<<"("<<decimal<<")10 = (";
    while(de>0)
    {
        oc=de%8;
        de=de/8;
        octal=octal+(oc*i);
        i=i*10;
    }cout<<octal<<".";
    while(deci>0)
    {
        x=deci*8;
        y=x;
        deci=x-y;
        cout<<y;
    }
    cout<<")8";
}
\$\endgroup\$
  • \$\begingroup\$ thanks a lot it is a correct program i needed this program its very helpful to me \$\endgroup\$ – Muhammad Usama Nov 4 '18 at 16:27
3
\$\begingroup\$

Just, i don't understand why?

What's wrong with (for integer):

#include <iostream>
int main() {
    int input;
    std::cout << "Enter decimal number: ";
    std::cin >> std::dec >> input;
    std::cout << '\n' << std::oct << input << '\n';
}

And about your code:

  • Avoid using namespace std, it's a bad practice
  • Just main() isn't a valid prototype for the entry point, at least use `int main()
  • Define variables in the closest scope possible.
  • Try naming variable consistently
  • de=de/8; can be simplified to de /= 8;
  • floating point conversion is a bit more tricky than just doing what you do

Edit: After testing it (and correcting main() and) : Your code just doesn't work, even for an integer value

Enter decimal no :: (0)10 = (32767.)8
Enter decimal no :: (8)10 = (32776.)8
Enter decimal no :: (10)10 = (32779.)8

Test yourself

\$\endgroup\$
  • \$\begingroup\$ i have checked it on dev c++ its working 100%. and i have checked your method that was great but can you tell me how it will work for fraction. \$\endgroup\$ – Mubashir Hussain Rao Oct 29 '18 at 14:16
  • \$\begingroup\$ if you want to check my program just copy it and paste in dev c++ than tell me if there is any bug. \$\endgroup\$ – Mubashir Hussain Rao Oct 29 '18 at 14:23
  • \$\begingroup\$ which compiler do you use? which version? which flag? Allow me to doubt a bit \$\endgroup\$ – Calak Oct 29 '18 at 14:35
  • \$\begingroup\$ using namespace std; is only bad in header files, not regular code. In fact, using it is the whole point of having namespaces in the first place. Otherwise you could just rename everything in std namespace as std_something and remove namespaces from the language. \$\endgroup\$ – merlyn Oct 29 '18 at 14:56
  • \$\begingroup\$ Do you know about namespaces? Theirs purposes? You Don't want to expose the whole std namespace to use it. And here, if he really want to use it, he just have to put using namespace std;inside the main, at the top. Here he dont have collision (He dodged std::oct and std::dec), but check out this tread \$\endgroup\$ – Calak Oct 29 '18 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.