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!



swith case c++ change dec2hex2bin Rate Topic: -----

#1 speedyexe  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-January 09


Dream Kudos: 0

Share |

swith case

Posted 07 January 2009 - 10:44 AM

hi i found something like that
#include <iostream>

template < typename T >
inline T highbit(T& t)
{
return t = (((T)(-1)) >> 1) + 1;
}

template < typename T >
std::ostream& bin(T& value, std::ostream &o)
{
for ( T bit = highbit(bit); bit; bit >>= 1 )
{
o << ( ( value & bit ) ? '1' : '0' );
}
return o;
}


int main()
{
unsigned long value;
std::cout << "Enter number: " << std::endl;
std::cin>>value;
std::cout << "hex: " << std::hex << value << std::endl;
std::cout << "dec: " << std::dec << value << std::endl;
std::cout << "oct: " << std::oct << value << std::endl;
std::cout << "bin: ";
bin(value, std::cout);
std::cout << std::endl;
system("pause");
return 0;
}



its working verry well but...
i wanna change that and make this in swith case and all that stuff i started like that
#include <iostream>
#include <conio.h>

using namespace std;
float a,n;
char wybor;
int main(void)

{
	 system("cls");
 
   
	cout << " Dziesietne na binarne oraz dziesietne na szesnastkowe :P " << endl;
	cout<<"Podaj liczbe:";
	cin>>a;
	cout  <<"Wybierz działanie: dziesietne na binarne(b),dziesietne na szesnastkowe(6)";
	cin >> wybor;

	 switch(wybor)
	{
	  case 'b' :{
			  string LongToBinary(long int n)
			  return n==0 ? "" : LongToBinary(n/2) + ((n%2)?"1":"0");
			  cout<<LongToBinary(a)<<endl;
			  }
			  break;
 
   
   
   
   
   
	  case '6':{
			 cout<< "Zapis szesnastkowy: "<< hex << a << endl;
		 
			 }
		   
   
			 
	 };
	 getch();
	 }





but still i cant compile thet HELP! :wub:
im using devc++
and that stuff
 
	  //****************************************************************************//
  
	  // //
  
	  // Program Name : ConvDec.cpp //
   
	  // //
   
	  // Programmer : K.Angel //
   
	  // : Novice C++ //
   
	  // //
  
	  // Description : The user is asked to input a number in either Hex, Octal or //
   
	  // Decimal, which is then converted to Hex, Octal & Decimal. //

	  // The user can exit the program from the MAIN MENU or by //
 
	  // the Escape key after each convertion. //
  
	  // //
  
	  // Compiler : Borland Turbo C++ v4.5. //

	  // Date : 16/03/2006 //
  
	  // //
  
	  //****************************************************************************//
  
	   
  
	  #include <iostream.h>
  
	  #include <conio.h> // for clrscr() & getche() functions...
  
	  #include <stdlib.h> // for exit() function...
  
	   
  
	  const int ESC = 27; // Used for escape sequince
  
	   
  
	  int main()
  
	  {
  
	  int key = 0;
  
	  int choise = 0;
  
	  long number;
  
	   
  
	  while(key != ESC)
  
	  {
  
	  cout << "Choose Your Number Base\n";
  
	  cout << "\n\t1. Hexadecimal.";
  
	  cout << "\n\t2. Octal.";
  
	  cout << "\n\t3. Decimal.";
  
	  cout << "\n\t4. Exit.\n\t ";
  
	  cin >> choise;
  
	   clrscr();
  
	  switch(choise)
  
	  {
  
	  case 1: 
  
	  cout << "Enter a Hex number: ";
  
	  cin >> hex >> number;
  
	  cout << "\n value in octal = "
  
	  << oct << number << endl;
  
	  cout << " value in decimal = "
  
	  << dec << number << endl;
  
	  break;
  
	  case 2: 
  
	  cout << "Enter a Octal number: ";
  
	  cin >> oct >> number;
 
	  cout << "\n value in hex = "
  
	  << hex << number << endl;
  
	  cout << " value in decimal = "
  
	  << dec << number << endl;
  
	  break;
  
	  case 3: 
  
	  cout << "Enter a dec number: ";
  
	  cin >> dec >> number;
  
	  cout << "\n value in octal = "
  
	  << oct << number << endl;
  
	  cout << " value in hex = "
  
	  << hex << number << endl;
 
	  break;
 
	  case 4: 
  
	  cout << "Program terminated by user...";
  
	  exit(0);
  
	  break; //Unreachable code....
  
	  default : 
 
	  cout << "ERROR ~ Invalid selection\n\n";
  
	  break;
  
	  }
  
	  cout << "Press any key to continue or 'Esc' to exit";
 
	  key = getche();
  
	  clrscr();
  
	  }
  
	  cout << "Program terminated by user...";
  
	  return 0;
  
	  }



is not working :(

i wanna do this in that switch and case.... but i cant please help if u can
and the other bigger problem is to make program that shows all possible moves of king on chess board i cant find in google that kind of stuff and need help
best regards
Speedy
Was This Post Helpful? 0
  • +
  • -


#2 bodom658  Icon User is offline

  • Villiage Idiom
  • Icon

Reputation: 85
  • View blog
  • Posts: 1,028
  • Joined: 22-February 08


Dream Kudos: 350

Re: swith case

Posted 07 January 2009 - 12:35 PM

the code is unreachable because your program is gonna exit when exit() is called. the break is not needed
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

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