Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 3rd, 2004, 8:22 AM   #1
RosdaHale
Newbie
 
Join Date: Nov 2004
Posts: 15
Rep Power: 0 RosdaHale is on a distinguished road
Please help I am having a nightmare with this.

I need to convert a decimal number in to Hex and binary. Dec_Num is a random integer number up to 255. Then I have Bin_Num (integer) and Hex_Num (Char - as letters are needed).

I have the following.

Bin_Num =Convert_to_Dec(Dec_Num)
Hex_Num =Convert_to_Hex(Dec_Num)

Functions :

int Convert_to_Dec (int a)
{


Return(Result);
}

int (Convert_to_Hex (char a)
{

Resturn(Result);
}

Now heres my problem I have tried so many ways and cant get anyting to work. To work out binary you simply divide by 2 and store the remainder, so I set up an array to store remainder. But my returned result NEEDS to be a integer, so I converted to an integer, but the result is still always wrong.

Also Hex basiaclly the same, but obviously need to return a char result as it will contain letters. NOTE : I AM NOT ALLOWED TO USE ANY EXSISTING C++ COMMANDS TO WORK OUT THE DEC AND HEX, I MUST WRITE MY OWN FUNCTION TO DO IT. PLEASE SOMEONE HELP !!

PLUS : The returned results must be integer and char (basiaccly anything apart from ARRAY!)
RosdaHale is offline   Reply With Quote
Old Dec 4th, 2004, 9:43 AM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
Your requirements seem to keep changing. Watch and learn:
#include <iostream>

using namespace std;

// You MUST accept a string as the argument
int Convert_to_Dec ( const char *hex );

// You MUST return a string
char *Convert_to_Hex ( int dec );

int main()
{
 cout<< Convert_to_Dec ( "1B" ) <<endl;
 cout<< Convert_to_Hex ( 27 ) <<endl;
}

int Convert_to_Dec ( const char *hex )
{
 int ret = 0;

 while ( *hex != '\0' ) {
  if ( *hex >= '0' && *hex <= '9' )
   ret = 10 * ret + ( *hex - '0' );
  else {
   // Assuming ASCII
   ret = 16 * ret + ( *hex - 'A' + 10 );
  }
  ++hex;
 }

 return ret;
}

char *Convert_to_Hex ( int dec )
{
 static char hex[20];
 int i = 0, j = 0;

 // Build the string
 while ( dec != 0 ) {
  hex[i++] = "0123456789ABCDEF"[dec % 16];
  dec /= 16;
 }

 // Make it a valid string
 hex[i] = '\0';

 // Reverse the string
 while ( j < i ) {
  char temp = hex[j];
  hex[j++] = hex[--i];
  hex[i] = temp;
 }

 return hex;
}
If this isn't what you want then you'll need to specify what you're doing. Are these functions converting a single digit? Because that's the only way you can avoid using strings and arrays.
Eggbert is offline   Reply With Quote
Old Dec 4th, 2004, 11:47 AM   #3
RosdaHale
Newbie
 
Join Date: Nov 2004
Posts: 15
Rep Power: 0 RosdaHale is on a distinguished road
That great but i need to return the hex result as a string not an array, how do i change it to a string. I am not printing out the result simply holding it in a varible.

So Hex_Num=Convert_To_Hex(Dec-Num)
not cout << Convert_To_Hex(34);
RosdaHale is offline   Reply With Quote
Old Dec 4th, 2004, 3:12 PM   #4
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
>but i need to return the hex result as a string not an array, how do i change it to a string.
It is a string. C-style strings are simply arrays of char terminated by '\0'. If you want a C++ string object then take a look at the code I gave you in your other thread.

>I am not printing out the result simply holding it in a varible.
You have two options. If you don't intend to call Convert_to_Hex until after you're done working with the return value then you can simply use a pointer to char:
char *Hex_Num=Convert_To_Hex(Dec - Num);
This works because the string being used in Convert_To_Hex is static and will persist between calls. However, if you intend to do something like this:
char *Hex_Num1=Convert_To_Hex(Dec1 - Num1);
char *Hex_Num2=Convert_To_Hex(Dec2 - Num2);
Then it won't work because there is only one string and it will be overwritten with every call to Convert_To_Hex. In that case, you need to make a copy. Using C-style strings you would do it like this:
#include <cstring>

char Hex_Num[20];

strcpy ( Hex_Num, Convert_To_Hex ( Dec - Num ) );
Using C++ strings you would do this:
#include <string>

string Hex_Num ( Conver_To_Hex ( Dec - Num ) );
Eggbert is offline   Reply With Quote
Old Dec 4th, 2004, 6:56 PM   #5
RosdaHale
Newbie
 
Join Date: Nov 2004
Posts: 15
Rep Power: 0 RosdaHale is on a distinguished road
What am i doing wrong then, when i try to display the string it only comes up with one character and not the whole thing. (just says 7 or A - just one char)
RosdaHale is offline   Reply With Quote
Old Dec 4th, 2004, 7:42 PM   #6
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
It's hard to say what you're doing wrong without an example of your code.
Eggbert is offline   Reply With Quote
Old Dec 5th, 2004, 5:40 AM   #7
RosdaHale
Newbie
 
Join Date: Nov 2004
Posts: 15
Rep Power: 0 RosdaHale is on a distinguished road
Okay, this is everything I have if you would be so kind to take a look (just paste in to a new win32 console app)

What I have to do is, the comp generates a random decimal number (1-255), you have to work out Binary and Hex equiverlent and store as integer (binary) and string (hex). Computer also picks randomly which one to display, the user has to guess the other two (has 3 attempts to do it). If successful 6 points, each try you use the score gets reduced by -2. There are 10 games in total.

By the way -- The cout << Dec_Num, cout << Bin_Num and, cout << Hex_Num in WinMain.cpp is just a test to see if the numbers are working.

Functions.cpp:
#include "Globals.h"

// Function to convert DEC to BIN
int Process_Dec_To_Bin(int a)
{

	int Remainders[8]={0};
	char Convert_Array_To_Int[8];
	int Temp_Remainder;
	int Result=0;

	for(int x=7;x>=0;x--)
	{
 Temp_Remainder=a%2;
 Remainders[x]=Temp_Remainder;
 a=a/2;
	}

	for(x=0;x<8;x++)
	{
 Convert_Array_To_Int[x]=Remainders[x]+0x30;
	}
	Result=atoi(Convert_Array_To_Int);

return(Result);
}

//Function to convert DEC to HEX
char *Process_Dec_To_Hex(char a)
{
	static char hex[20];
	int i = 0, j = 0;

 // Build the string
	while ( a != 0 ) 
	{
  hex[i++] = "0123456789ABCDEF"[a % 16];
  a /= 16;
	}

	// Make it a valid string
	hex[i] = '\0';

	// Reverse the string
	while ( j < i ) 
	{
  char temp = hex[j];
  hex[j++] = hex[--i];
  hex[i] = temp;
	}
return(hex);
}

//Check the values
int Process_Input_HexBin(char a, int b)
{
 if((a==Hex_Num)&&(b==Bin_Num))
 {
 	Output=true;
 	Attempt_Number=1;
 	Total_Score=Total_Score+Score;
 	Score=6;
 	cout << "Correct";
 }
 else
 {	
 	Output=false;
 	Attempt_Number++;
 	cout << "Answer's incorrect. Please try again. Attempt Number " << Attempt_Number << ":";
 	cout << endl << endl;
 	Score=Score-2;
 }
return(0,0);
}

int Process_Input_DecHex(int a, char b)
{
 if((a==Dec_Num)&&(b==Hex_Num))
 {
 	Output=true;
 	Attempt_Number=1;
 	Total_Score=Total_Score+Score;
 	Score=6;
 }
 else
 {	
 	Output=false;
 	Attempt_Number++;
 	cout << "Answer's incorrect. Please try again. Attempt Number " << Attempt_Number << ":";
 	cout << endl <<endl;
 	Score=Score-2;
 }
return(0,0);
}

int Process_Input_DecBin(int a, int b)
{
 if((a==Dec_Num)&&(b==Bin_Num))
 {
 	Output=true;
 	Attempt_Number=1;
 	Total_Score=Total_Score+Score;
 	Score=6;
 }
 else
 {	
 	Output=false;
 	Attempt_Number++;
 	cout << "Answer's incorrect. Please try again. Attempt Number " << Attempt_Number << ":";
 	cout << endl;
 	Score=Score-2;
 }
return (0,0);
}

Globals.cpp:
#include "Globals.h"

int  Number_System=0;
int  Dec_Num=0,Bin_Num=0;
char Hex_Num=0;
int  Score=6,Total_Score=0,Attempt_Number=1;
bool Output=false;

WinMain.cpp:
#include "Globals.h"
int main()
{
	int Magic_Num;
	int GuessBin=0,GuessDec;
	char GuessHex=0;
	int Game_Number=1;
	
	do
	{
 srand((unsigned)time(NULL));
 Magic_Num=(rand()%255)+1;    //Choose random 8 bit number
 Number_System=(rand()%3)+1;    //Choose which base for the user to work out
 Attempt_Number=1;

 system("CLS");     	// Clear the screen.

 cout << "Score : " << Total_Score <<"\tGame : " << Game_Number;
 cout << endl<<endl;
 cout << "Player\t\tNumber System\t\tValue" << endl;
 cout << "______\t\t_____________\t\t_____" << endl << endl;

 Dec_Num=Magic_Num;     // Set Decimal number to the random num
 Bin_Num=Process_Dec_To_Bin(Magic_Num);  	// Work out the Binary for the random num
 Hex_Num=*Process_Dec_To_Hex(Magic_Num);    // Work out the Hex for the random num

 cout << Dec_Num << endl;
 cout << Bin_Num << endl;
 cout << Hex_Num << endl;
 cout << endl << endl;

 if(Number_System==1) // Computer Gives Decimal
 {
 	cout << "Computer\tDecimal\t\t\t" << Dec_Num << endl;
 	cout << "Player\t\tHexadecimal\t\t---" << endl;
 	cout << "Player\t\tBinary\t\t\t---" << endl << endl;
 	
 	do
 	{	
  cout << "Please enter your conversion for Hexadecimal\t: "; cin >>	GuessHex;
  cout << "Please enter your conversion for Binary\t\t: "; cin >> GuessBin;
  Process_Input_HexBin(GuessHex, GuessBin);

 	}while((Output=false)||(Attempt_Number<4));

 }
 if(Number_System==2) // Computer Give Binary
 {
 	cout << "Player\t\tDecimal\t\t\t---" << endl;
 	cout << "Player\t\tHexadecimal\t\t---" << endl;
 	cout << "Computer\tBinary\t\t\t" << Bin_Num <<endl << endl;
 	
 	do
 	{
  cout << "Please enter your conversion for Decimal\t: "; cin >> GuessDec;
  cout << "Please enter your conversion for Hexadecimal\t: "; cin >>	GuessHex;
  Process_Input_DecHex(GuessDec,GuessHex);
  	
 	}while((Output=false)||(Attempt_Number<4));

 }
 if(Number_System==3) // Computer Gives Hex
 {
 	cout << "Player\t\tDecimal\t\t\t---" << endl;
 	cout << "Computer\tHexadecimal\t\t" << Hex_Num << endl;
 	cout << "Player\t\tBinary\t\t\t---" <<endl << endl;
 	
 	do
 	{	
  cout << "Please enter your conversion for Decimal\t: "; cin >> GuessDec;
  cout << "Please enter your conversion for Binary\t\t: "; cin >> GuessBin;
  Process_Input_DecBin(GuessDec, GuessBin);

 	}while((Output=false)||(Attempt_Number<4));
 }

 Game_Number++;
 
	}while(Game_Number<11);
return(0);
}

Globals.h (header file):
#include <iostream>
#include <time.h>	
#include <stdio.h>
#include <stdlib.h>	
	
using namespace std;

int  Process_Dec_To_Bin(int a);
char *Process_Dec_To_Hex(char a);
int  Process_Input_HexBin(char a,int b);
int  Process_Input_DecHex(int a,char b);
int  Process_Input_DecBin(int a,int b);

extern int	Number_System;
extern int	Dec_Num,Bin_Num;
extern char Hex_Num;
extern int Score,Total_Score,Attempt_Number;
extern int Remainder_Array_Posistion;
extern bool Output;
RosdaHale is offline   Reply With Quote
Old Dec 5th, 2004, 7:54 AM   #8
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
The reason you're only getting the first character is because that's what you ask for. Remember that C-style strings are sequences of characters, and you pass them around with pointers to char. This is a quick fix that gets things to compile, though I didn't test it. I'll leave that up to you.
#include <iostream>
#include <time.h> 
#include <stdio.h>
#include <stdlib.h> 
#include <string.h>

using namespace std;

extern int Remainder_Array_Posistion;

int  Number_System=0;
int  Dec_Num=0,Bin_Num=0;
char *Hex_Num=0;
int  Score=6,Total_Score=0,Attempt_Number=1;
bool Output=false;

// Function to convert DEC to BIN
int Process_Dec_To_Bin(int a)
{

 int Remainders[8]={0};
 char Convert_Array_To_Int[8];
 int Temp_Remainder;
 int Result=0;

 for(int x=7;x>=0;x--)
 {
  Temp_Remainder=a%2;
  Remainders[x]=Temp_Remainder;
  a=a/2;
 }

 for(int x=0;x<8;x++)
 {
  Convert_Array_To_Int[x]=Remainders[x]+0x30;
 }
 Result=atoi(Convert_Array_To_Int);

 return(Result);
}

//Function to convert DEC to HEX
char *Process_Dec_To_Hex(char a)
{
 static char hex[20];
 int i = 0, j = 0;

 // Build the string
 while ( a != 0 ) 
 {
  hex[i++] = "0123456789ABCDEF"[a % 16];
  a /= 16;
 }

 // Make it a valid string
 hex[i] = '\0';

 // Reverse the string
 while ( j < i ) 
 {
  char temp = hex[j];
  hex[j++] = hex[--i];
  hex[i] = temp;
 }
 return(hex);
}

//Check the values
int Process_Input_HexBin(char *a, int b)
{
 if(strcmp(a, Hex_Num) == 0&&(b==Bin_Num))
 {
  Output=true;
  Attempt_Number=1;
  Total_Score=Total_Score+Score;
  Score=6;
  cout << "Correct";
 }
 else
 { 
  Output=false;
  Attempt_Number++;
  cout << "Answer's incorrect. Please try again. Attempt Number " << Attempt_Number << ":";
  cout << endl << endl;
  Score=Score-2;
 }
 return(0,0);
}

int Process_Input_DecHex(int a, char *b)
{
 if((a==Dec_Num)&&strcmp(b, Hex_Num) == 0)
 {
  Output=true;
  Attempt_Number=1;
  Total_Score=Total_Score+Score;
  Score=6;
 }
 else
 { 
  Output=false;
  Attempt_Number++;
  cout << "Answer's incorrect. Please try again. Attempt Number " << Attempt_Number << ":";
  cout << endl <<endl;
  Score=Score-2;
 }
 return(0,0);
}

int Process_Input_DecBin(int a, int b)
{
 if((a==Dec_Num)&&(b==Bin_Num))
 {
  Output=true;
  Attempt_Number=1;
  Total_Score=Total_Score+Score;
  Score=6;
 }
 else
 { 
  Output=false;
  Attempt_Number++;
  cout << "Answer's incorrect. Please try again. Attempt Number " << Attempt_Number << ":";
  cout << endl;
  Score=Score-2;
 }
 return (0,0);
}

int main()
{
 int Magic_Num;
 int GuessBin=0,GuessDec;
 char GuessHex[20];
 int Game_Number=1;

 do
 {
  srand((unsigned)time(NULL));
  Magic_Num=(rand()%255)+1;    //Choose random 8 bit number
  Number_System=(rand()%3)+1;    //Choose which base for the user to work out
  Attempt_Number=1;

  system("CLS");      // Clear the screen.

  cout << "Score : " << Total_Score <<"\tGame : " << Game_Number;
  cout << endl<<endl;
  cout << "Player\t\tNumber System\t\tValue" << endl;
  cout << "______\t\t_____________\t\t_____" << endl << endl;

  Dec_Num=Magic_Num;     // Set Decimal number to the random num
  Bin_Num=Process_Dec_To_Bin(Magic_Num);   // Work out the Binary for the random num
  Hex_Num=Process_Dec_To_Hex(Magic_Num);    // Work out the Hex for the random num

  cout << Dec_Num << endl;
  cout << Bin_Num << endl;
  cout << Hex_Num << endl;
  cout << endl << endl;

  if(Number_System==1) // Computer Gives Decimal
  {
   cout << "Computer\tDecimal\t\t\t" << Dec_Num << endl;
   cout << "Player\t\tHexadecimal\t\t---" << endl;
   cout << "Player\t\tBinary\t\t\t---" << endl << endl;

   do
   { 
    cout << "Please enter your conversion for Hexadecimal\t: "; cin >> GuessHex;
    cout << "Please enter your conversion for Binary\t\t: "; cin >> GuessBin;
    Process_Input_HexBin(GuessHex, GuessBin);

   }while((Output==false)||(Attempt_Number<4));

  }
  if(Number_System==2) // Computer Give Binary
  {
   cout << "Player\t\tDecimal\t\t\t---" << endl;
   cout << "Player\t\tHexadecimal\t\t---" << endl;
   cout << "Computer\tBinary\t\t\t" << Bin_Num <<endl << endl;

   do
   {
    cout << "Please enter your conversion for Decimal\t: "; cin >> GuessDec;
    cout << "Please enter your conversion for Hexadecimal\t: "; cin >> GuessHex;
    Process_Input_DecHex(GuessDec,GuessHex);

   }while((Output==false)||(Attempt_Number<4));

  }
  if(Number_System==3) // Computer Gives Hex
  {
   cout << "Player\t\tDecimal\t\t\t---" << endl;
   cout << "Computer\tHexadecimal\t\t" << Hex_Num << endl;
   cout << "Player\t\tBinary\t\t\t---" <<endl << endl;

   do
   { 
    cout << "Please enter your conversion for Decimal\t: "; cin >> GuessDec;
    cout << "Please enter your conversion for Binary\t\t: "; cin >> GuessBin;
    Process_Input_DecBin(GuessDec, GuessBin);

   }while((Output==false)||(Attempt_Number<4));
  }

  Game_Number++;

 }while(Game_Number<11);
 return(0);
}
Eggbert is offline   Reply With Quote
Old Dec 5th, 2004, 11:46 AM   #9
RosdaHale
Newbie
 
Join Date: Nov 2004
Posts: 15
Rep Power: 0 RosdaHale is on a distinguished road
Thanks, the HEX still comes out with weird values and sometimes nothing at all (but most of the time ok), but it works better than last time. Only have one last problem now, if the two user guesses are true then the output gets set to true but nothing happens, it just asks for the inputs again instead of going on to the next game. This is the new code as it stands. Really sorry about this, this is the last problem I PROMISE !!! and im glad of the help.
RosdaHale is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:28 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC