4

Okay, I think I fixed most of this, but it doesn't like me passing the constants I think. Any help would be greatly appreciated, thank you.

also, with the !inputFile part, I'm not sure how to pull off a return (EXIT_FAILURE) like my teacher suggested..

Also, as to your suggestion with only using one part of the array, would that still allow me to use the whole thing in the function?

The program is supposed to take a file like this: ex: NOT 11010001 and it's supposed to read the command as a string, read the binary in as an array, then perform the command on the binary.

The code here is only main function, just don't want to send a wall of it all at once, if this looks okay, then I will happily add the rest. Also, the void Operate () function pretty much calls all of the other functions in one way or another... I'm not sure if that's what's causing it or what. The print table only cout's a table to put all of the info on.

and they headers are wonky for me on here, so just assume those are right.

/* ========================================================================== */

/* Prototypes */

int Power (int, int);

int ReadFile (ifstream inputFile);

void Operate (const int, ifstream&, string, int, int, int);

void CommandNot (const int, int, int);

void CommandAnd (const int, int, int, int);

void CommandOr (const int, int, int, int);

int CommandConvert (const int, int, int);

void CommandLshift (const int, int, int, int);

void PrintTable ();

void PrintOperand (const int &, int);

int main ()
{

//Constants

const int BIT_SIZE = 8;


//Variables


string fileName = "binaryData.txt";

    int operandOne [BIT_SIZE];

    int operandTwo [BIT_SIZE];

    int operandResult [BIT_SIZE];

    ifstream inputFile;

    PrintTable ();
    Operate (BIT_SIZE, inputFile, fileName, operandOne[BIT_SIZE], operandTwo[BIT_SIZE], operandResult[BIT_SIZE]);

    return 0;
}

void PrintTable ()
{

    cout << "=================================================" << endl;
    cout << "=      Eight Bit Binary Number Manipulator      =" << endl;
    cout << "=================================================" << endl << endl;

    cout << setw(14) << "COMMAND" << "Operand #1" << "Operand #2" << "Shift" << "Result" << endl;
    cout << "----------------------------------------------------------------------" << endl;

}

void Operate (const int BIT_SIZE, ifstream& inputFile, string fileName, int operandOne[], int operandTwo[], int operandResult[])
{
    //Variables
    int count, shift;
    char myChar;
    string command;

    const int SIZE = BIT_SIZE;  //rename constant

    inputFile.open (fileName);

    if ( !inputFile )       //Check if file opened sucessfully
    {
        cout << "Error: Data file could not be opened" << endl;
    }

    while (inputFile)       //Read file, and apply commands
    {
        inputFile >> command;
        cout << command << endl;
        for ( count = 0;  count < SIZE; count++ )
        {
            inputFile >> myChar;
            operandOne[count] = myChar - '0';
        }

        if (command == "NOT")
        {
            CommandNot (BIT_SIZE, operandOne[BIT_SIZE], operandResult[BIT_SIZE]);
            PrintOperand (BIT_SIZE, operandResult[BIT_SIZE]);
        }
        else if (command == "AND")
        {
            count = 0;
            for ( count = 0;  count < SIZE; count++ )
            {
                inputFile >> myChar;
                operandTwo[count] = myChar - '0';
            }

            CommandAnd (BIT_SIZE, operandOne[BIT_SIZE], operandTwo[BIT_SIZE], operandResult[BIT_SIZE]);
            PrintOperand (BIT_SIZE, operandResult[BIT_SIZE]);
        }
        else if (command == "OR")
        {
            count = 0;
            for ( count = 0;  count < SIZE; count++ )
            {
                inputFile >> myChar;
                operandTwo[count] = myChar - '0';
            }

            CommandOr (BIT_SIZE, operandOne[BIT_SIZE], operandTwo[BIT_SIZE], operandResult[BIT_SIZE]);
            PrintOperand (BIT_SIZE, operandResult[BIT_SIZE]);
        }
        else if (command == "CONVERT")
        {
            CommandConvert (BIT_SIZE, operandOne[BIT_SIZE], operandResult[BIT_SIZE]);
            PrintOperand (BIT_SIZE, operandResult[BIT_SIZE]);
        }
        else if (command == "LSHIFT")
        {
            inputFile >> shift;
            CommandLshift (BIT_SIZE, operandOne[BIT_SIZE], operandResult[BIT_SIZE], shift);
            PrintOperand (BIT_SIZE, operandResult[BIT_SIZE]);
        }

        else
        {
            command = "INVALID";
            PrintOperand (BIT_SIZE, operandOne[BIT_SIZE]);
            cout << "--- ERROR! Invalid Command ---";
        }
    }

    inputFile.clear();
    inputFile.close();

    return ;
}

void CommandNot (const int BIT_SIZE, int operandOne[], int operandResult[])
{

    int count;
    const int SIZE = BIT_SIZE;

    for ( count = 0;  count < SIZE; count++ )
    {
        if (operandOne[count] == 0)
        {
            operandResult[count] = 1;
        }
        else
        {
            operandResult[count] = 0;
        }
    }

}

void CommandAnd (const int BIT_SIZE, int operandOne[], int operandTwo[], int operandResult[])
{

    int count;
    const int SIZE = BIT_SIZE;

    for ( count = 0;  count < SIZE; count++ )
    {
        if ((operandOne[count] == 1) && (operandTwo[count] == 1))
        {
            operandResult[count] = 1;
        }
        else
        {
            operandResult[count] = 0;
        }
    }

}

void CommandOr (const int BIT_SIZE, int operandOne[], int operandTwo[], int operandResult[])
{

    int count;
    const int SIZE = BIT_SIZE;

    for ( count = 0;  count < SIZE; count++ )
    {
        if ((operandOne[count] == 0) && (operandTwo[count] == 0))
        {
            operandResult[count] = 0;
        }
        else
        {
            operandResult[count] = 1;
        }
    }

}

int CommandConvert (const int BIT_SIZE, int operandOne[])
{

    int count;
    const int SIZE = BIT_SIZE;
    int baseTenResult = 0;
    int place;

    for ( count = 0;  count < SIZE; count++ )
    {
        place = SIZE - (count + 1);
        if (operandOne[count] == 1)
        {
            baseTenResult = baseTenResult + Power (2, place);
        }
        else 
        {
            continue;
        }
    }

    return baseTenResult;
}

void CommandLshift (const int BIT_SIZE, int operandOne[], int operandResult[], int shift)
{

    int count;
    const int SIZE = BIT_SIZE;
    int shiftStart = SIZE - shift;

    for ( count = 0;  count < SIZE-shift; count++ )
    {
        operandResult[count] = operandOne[count + shift];
    }

    for ( count = SIZE - shift; count < SIZE; count++ )
    {
        operandResult[count] = 0;
    }

}

int Power (int base, int power)
{
    int count;
    int result = 1;

    for ( count = 0; count < power; count++ )
    {
        result = result * base;
    }

    return result;
}

void PrintOperand (const int BIT_SIZE, int operandResult[])
{

    int count;
    const int SIZE = BIT_SIZE;

    for ( count = 0;  count < SIZE; count++ )
    {
        cout << operandResult[count];
    }

}

1 Answers1

4

You need to call the functions from main. You can't do this by sort-of redeclaring them:

void PrintTable();
void Operate (const int BIT_SIZE, ifstream& inputFile, string fileName, int operandOne[], int operandTwo[], int operandResult[]);

Instead, you need to call them:

PrintTable();
Operate(BITSIZE,inputFile,fileName,operandOne,operandTwo,operandResult);

Note, however that there is another problem: Operate requires three integer arguments at the end, but you seem to try to use integer arrays as arguments. You'll need to select one element of each array and submit only that as argument.


(EDIT) A few things have changed in your question, and I don't understand enough to tell what the best overall structure for your data and functions should be, but if you are dealing with integer arrays and you'd like to pass an entire array to a function, you can do it in this way (I've simplified it to a function that takes only one array of integers. Of course you can have more than one function argument):

#include <iostream>

const int SIZE = 3;

/* This is a simpified version of 'operate'. It
   takes one argument, which is an array of integers. */    
void operate(int a[])
{
  /* The only thing I do is to iterate through
     all elements of the array and print them. */
  int i = 0;
  while (i < SIZE) {
    std::cout << a[i] << std::endl;
    ++i;
  }

  /* IMPORTANT: The length of the array is defined by a
     global constant SIZE. Alternatively, you can pass
     along the size of the array as a separate argument
     to the function. */
}

int main()
{
  /* Main program. Here is our array: */
  int my_array[SIZE] = { 1,2,3 };

  /* And here we call our function: */
  operate(my_array);
  return 0;
}

However, all of this is a bit complicated and not really as conventient as you could have it in C++ (as opposed to C). In all likelihood, you'll be much better of not using arrays at all, and replacing them with std::vector. Best check on cppreference for examples of how to use it (also click on some of the member functions, such as the constructor and push_back to get specific code examples.)

jogojapan
  • 68,383
  • 11
  • 101
  • 131
  • Oh, so the integer arrays don't count as integers for this? If I only use one element like I tried here, would I still be able to use the whole array in the function? – user1828561 Nov 16 '12 at 05:27
  • I've added some more information about how you can pass arrays to functions. Hope it helps. – jogojapan Nov 16 '12 at 05:43
  • I would if I could. The class requires Arrays this once. Thanks for your help, I think I fixed most of it. The only problem now is it keeps saying that I have an unresolved external symbol now.. I have no idea what that means – user1828561 Nov 16 '12 at 06:04
  • It typically refers to a function that you declared and call somewhere, but you haven't defined it (or you defined it in a separate .cpp file that you don't link to). The first thing I'd do is to check whether the prototype of each function definition (i.e. the arguments it takes, its return type etc.) matches exactly with the function declaration. If this doesn't help, the next steps depend on how you build your project. Is this all in one .cpp file? Or do you have headers and multiple .cpp files? – jogojapan Nov 16 '12 at 06:08
  • The unresolved symbol may also refer to a global variable (i.e. a variable not declared inside main or inside any of the functions). Do you have such variables? – jogojapan Nov 16 '12 at 06:09
  • Only the one for the arrays. – user1828561 Nov 16 '12 at 14:08
  • What do you mean? According to your code, the arrays as well as `BIT_SIZE` are all declared inside main. If there is a global variable, what kind of variable is that? Also, is all of your code in one big .cpp file, or do you have multiple files? – jogojapan Nov 16 '12 at 14:11