\$\begingroup\$
\$\endgroup\$
1
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";
}
asked Oct 29, 2018 at 11:00
-
\$\begingroup\$ thanks a lot it is a correct program i needed this program its very helpful to me \$\endgroup\$Muhammad Usama– Muhammad Usama2018年11月04日 16:27:23 +00:00Commented Nov 4, 2018 at 16:27
1 Answer 1
\$\begingroup\$
\$\endgroup\$
9
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 tode /= 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
answered Oct 29, 2018 at 12:22
-
\$\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– Mubashir Hussain Rao2018年10月29日 14:16:03 +00:00Commented Oct 29, 2018 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– Mubashir Hussain Rao2018年10月29日 14:23:41 +00:00Commented Oct 29, 2018 at 14:23
-
\$\begingroup\$ which compiler do you use? which version? which flag? Allow me to doubt a bit \$\endgroup\$Calak– Calak2018年10月29日 14:35:00 +00:00Commented Oct 29, 2018 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 instd
namespace asstd_something
and remove namespaces from the language. \$\endgroup\$merlyn– merlyn2018年10月29日 14:56:55 +00:00Commented Oct 29, 2018 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 putusing namespace std;
inside the main, at the top. Here he dont have collision (He dodgedstd::oct
andstd::dec
), but check out this tread \$\endgroup\$Calak– Calak2018年10月29日 15:18:22 +00:00Commented Oct 29, 2018 at 15:18
Explore related questions
See similar questions with these tags.
lang-cpp