Files: Working on Function Definitions

I'm not entire sure what's wrong with this program that I've worked in class. I got it to work when everything was located in the main body, but not when I use prototypes and user-defined-functions. A point at the right direction would be greatly appreciated. Just to note: It doesn't even run, it only gives me errors that I'm unfamiliar with.

//THIS PROGRAM READS TWO HEXADECIMAL NUMBERS FROM A FIL, HEX.TXT,
//AND PRINTS OUT THE SYM OF THE TWO NUMBERS IN HEXADECIMAL.

//PREPROCESSOR
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>

using namespace std;

//PROTOTYPE
int converter(char a []);

int main()
{
//VARIABLES
ifstream hexafile;
char* hexadec1 [4];
char* hexadec2 [4];
int total;
int i;

//OPENING THE FILE CALLED HEXAFILE
hexafile.open("hex.txt");

//WHEN UNABLE TO OPEN THE FILE
if (!hexafile)
{
cout << "ERROR OPENING FILE." << endl;

system ("pause");
return 0;
}

//USING THE WHILE LOOP FOR THE HEXADECIMALS
while (!hexafile.eof())
{
hexafile >> hexadec1 [4] >> hexadec2 [4]; //READING THE HEXADECIMALS FROM FILE

total = converter(hexadec1 [i]);
total += converter(hexadec2 [i]);
}

cout << total << endl;

//ENDING
system ("pause");
return (0);
}

//CHANGING THE CHARACTERS INTO AN INT FOR THE ARRAYS
int converter(char a [])
{
int i, total = 0;
int b [4];

for ( i=0; i<4; i++)
{
switch (a [i])
{
case '1':
b [i] = 1;
break;
case '2':
b [i] = 2;
break;
case '3':
b [i] = 3;
break;
case '4':
b [i] = 4;
break;
case '5':
b [i] = 5;
break;
case '6':
b [i] = 6;
break;
case '7':
b [i] = 7;
break;
case '8':
b [i] = 8;
break;
case '9':
b [i] = 9;
break;
case 'A':
case 'a':
b [i] = 10;
break;
case 'B':
case 'b':
b [i] = 11;
break;
case 'C':
case 'c':
b [i] = 12;
break;
case 'D':
case 'd':
b [i] = 13;
break;
case 'E':
case 'e':
b [i] = 14;
break;
case 'F':
case 'f':
b [i] = 15;
break;
}

total += (b [i] * 16 * pow(16,3-i));
}

return total;
}






The file: "hex.txt" has the input:

45AF 12B3
Last edited on
I don't have access to a compiler atm, so I can't run it, but I don't think you can read directly read into an array.

1
2
 hexafile >> hexadec1 [4] >> hexadec2 [4];	 //READING THE HEXADECIMALS FROM FILE
 



When you do that, you're putting the whole 45AF into just the 4th position of the array hexadec1. And since it's a char array, that position can only take in 1 thing.

I'd try to store the input into a string, because a string is already an array of characters. So you would use 2 strings, instead of the 2 char arrays.
For example:

1
2
3
 string word = "hello";

//word[1] = e  


Idk, does the fstream part work? It seems like it shouldn't.
Hey, just wanted to let you know that you helped me a lot. I got rid of the character arrays and just used a string variable. So the changes were:

//USES A STRING VARIABLE AND TAKES EACH ALLOCATED ARRAY OF IT
//AND CONVERTING IT TO AN ACTUAL NUMBER
int converter(string a)
{
.
.
.
}

and also

//USING THE WHILE LOOP FOR THE HEXADECIMALS
while (!hexafile.eof())
{
hexafile >> shex1 >> shex2; //READING THE HEXADECIMALS FROM FILE


total = converter(shex1);
total += converter(shex2); //TOTAL OF LEFT AND RIGHT HEXADECIMAL
}

Also, my calculation had an extra 16 on there, so just took it off and it would look like:
total += (b[i] * pow(16,3-i));


You helped me out a lot bino1. Thank you so much!
Topic archived. No new replies allowed.