Ok, I think I fixed most of this, but I donโt like that I pass the constants that I think. Any help would be greatly appreciated, thanks.
also, with a piece! inputFile, I'm not sure how to withdraw the return (EXIT_FAILURE), as suggested by my teacher.
Also, as for your suggestion using only one part of the array, will it still allow me to use all this in a function?
It is assumed that the program accepts such a file: for example: NOT 11010001 and it should read the command as a string, read the binary file as an array, and then execute the command in binary format.
The code here is just the main function, you just donโt need to send a wall right away, if thatโs good, then Iโll be happy to add the rest. Also, the void Operate () function pretty much calls all the other functions anyway ... I'm not sure if this caused it or what. The print table contains only a table in which all information is placed.
and the headings are uncomfortable for me here, so just assume they 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]; } }