Turn your Mobile Apps into m-commerce apps – Learn More!

You're Browsing As A Guest! Register Now...
Become a C++ Expert!

Join 416,733 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,924 people online right now.Registration is fast and FREE... Join Now!



one code to convert decimal to binary, hex, octal i tried the code (r=i+'0') but it doesn't do hexidecimal c Rate Topic: -----

#1 BRTHTKG  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-July 07


Dream Kudos: 0

Share |

one code to convert decimal to binary, hex, octal

Posted 23 July 2007 - 05:31 PM

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

#include "EndOrderedLinkedList.cpp"

void ConvertNumber(int,int, EndOrderedList&);
//converts number to stack of characters in number base
void WriteNumber(ostream&, EndOrderedList&);
//writes the number and empties the list

int main()
{  
	
	EndOrderedList mystack;
	ifstream dat3d;
	ofstream out3d;
	int p;
	   
	dat3d.open("F:\\dat3d.txt");
	if(!dat3d)
	{
		cout<<"failure to open dat3d.txt"<<endl;
		system("PAUSE");
		return 1;
	}
	
	
	out3d.open("F:\\out3d.txt");
	if(!out3d)
	{
		cout<<"failure to open out3d.txt"<<endl;
		 system("PAUSE");
		return 1;
	}
	
dat3d>>p;
 while (dat3d)
{ 
  ConvertNumber(2, p, mystack);
	WriteNumber(out3d, mystack);
  out3d<<endl;
  ConvertNumber(16, p, mystack);
  WriteNumber(out3d, mystack);
  out3d<<endl;
  ConvertNumber(8, p, mystack);
  WriteNumber(out3d, mystack);
  out3d<<endl;
  system ("PAUSE");
  dat3d>>p;
}

dat3d.close(); out3d.close();
system ("PAUSE");

return 0;
}  


void ConvertNumber(int base,int n, EndOrderedList& mystack)
{ int i;
  char r;
  while (n>0)
  {
	  i = n % base;
	  r = i + '0';
	  mystack.push(r);
	  n = n/base;
	} 
}


void WriteNumber(ostream& out3d, EndOrderedList& mystack)
{ 
	
  while(mystack.isempty())
   { 
	 out3d<<mystack.top();
	  mystack.pop();
   }  
   
}


Was This Post Helpful? 0
  • +
  • -


#2 imamkomc  Icon User is offline

  • D.I.C Head
  • Icon

Reputation: 3
  • View blog
  • Posts: 64
  • Joined: 09-May 07


Dream Kudos: 225

Re: one code to convert decimal to binary, hex, octal

Posted 23 July 2007 - 07:38 PM

Can you expand your post with "EndOrderedLinkedList.cpp".
I think it necessary to explain your problem. Because usually at the #include stuff file header (*.h) not other like (*.cpp).
#include "EndOrderedLinkedList.cpp"
//#include "EndOrderedLinkedList.h"


Where you show error in your program ?
Was This Post Helpful? 0
  • +
  • -

#3 BRTHTKG  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-July 07


Dream Kudos: 0

Re: one code to convert decimal to binary, hex, octal

Posted 23 July 2007 - 10:54 PM

View Postimamkomc, on 23 Jul, 2007 - 08:38 PM, said:

Can you expand your post with "EndOrderedLinkedList.cpp".
I think it necessary to explain your problem. Because usually at the #include stuff file header (*.h) not other like (*.cpp).
#include "EndOrderedLinkedList.cpp"
//#include "EndOrderedLinkedList.h"


Where you show error in your program ?

Sorry there is no error in the program everything is fine. I am just having a problem finding one code for coverting decimal to binary, hex and octal. My program works fine. Can you help with the coverting problem?
[/quote]
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users