Put the code you need help with here.
#include <iostream>
#include <fstream>
#include <stdlib.h>
usingnamespace std;
//A function that will convert a hexadecimal into decimal and returns the integer back to main.
//The Parameters is the array of chars.
int hexToDecimal(constchar* hex);
constint MAX_SIZE = 15;
int main()
{
ifstream hexa;
hexa.open("hexNums.txt");
char* hexarray = newchar[MAX_SIZE];
while (!hexa.eof())
{
for (int i = 0; i <= MAX_SIZE; i++)
{
hexa >> hexarray[i];
cout << hexarray[i];
}
}
delete[] hexarray;
hexa.close();
return 0;
}
I am trying to read this text file: aef301
AA
BA
-6353
-cf
-CF
0
1
-10
11
111111
-111111
CABDF6
-cabdf6
defc
-D
in to the char array. But it comes out as one line and not separated by enters. How would I implement each element of the array to each row in the text file. Please help. THank you.
Well, without the endl; the output is
"aef301AABA-6353-cf-CF01-1011111111-111111CABDF6-cabdf6defc-DDF6-". So with the endl;, i think you know what will happen.
It has to be separated like the ones in the text file. Thank you for replying though. >.<
Wow! It worked. Can u explain to me why? what does this function do? THank you! Also, with this, will I be able to convert each line or row in to decimal properly? Cause get() seems to just copy the txt file and not organize each row in to an element in to the array. I am not too sure. Thank you.
! row is an array. If you want to hold 20 arrays you need:
1 2 3 4 5 6
char *array[max_rows];
array[0] = newchar (MAX);
etc.
Since you are using c++ you could create array of strings for ease of use:
[code]std::string array[MAX]
>> didn't work because it moves formatted data. If you will do:
1 2
char asd[] = "asd1\nasd\n";
cout << asd;
Program won't print \n and \0. In your case end-of-the-line sign was ignored. Get() move unformatted data instead with all signs.
Oh wow. getline is really useful. Hmm. I can explore more with this. Thank you. Sorry, but I have one more question. If I want to convert each of these hexidecimals into intergers recursively, what kind of a strategy should I think about? I am currently learning recursive functions and its not really making sense. Thanks again.
You should apply method you find the most fit, not the otherwise.
a41 ..
is
16^0 * 1 + 16^1 * 4 + 16^2 *10 ... etc
So depending on array length you need to do above. Sum of all powers will give you desired number.
To do something recursively you need to find part of algorithm that is repeating itself. Write on sheet of paper what is need to be done to do above counting for some numbers and you will find a way.
The recursion should return to the hexadecimal to decimal conversion where everytime it returns, it increments by 1 so that the 16^x will go to the next character.
1 2 3 4 5
16^n * hexarray[j];
return n++;
But how would I find the array length for each element?