![]() |
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!) |
Your requirements seem to keep changing. Watch and learn:
:
#include <iostream> |
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); |
>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);:
char *Hex_Num1=Convert_To_Hex(Dec1 - Num1);:
#include <cstring>:
#include <string> |
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)
|
It's hard to say what you're doing wrong without an example of your code.
|
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"Globals.cpp: :
#include "Globals.h"WinMain.cpp: :
#include "Globals.h"Globals.h (header file): :
#include <iostream> |
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> |
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.
|
| All times are GMT -5. The time now is 2:52 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC