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.
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;
}
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.