Hey there, I'm having trouble getting my program to read every instruction to then execute it, as well as displaying everything correctly. Right now my program displays mainly garbage and for now I simply want it to be able to successfully run a simple addition program, so any advice or hints, or even any links to send me for info would be great.
Here's my program so far:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

const int READ = 10;
const int WRITE = 11;

const int LOAD = 20;
const int STORE = 21;

const int ADD = 30;
const int SUBTRACT = 31;
const int DIVIDE = 32;
const int MULTIPLY = 33;

const int BRANCH = 40;
const int BRANCHNEG = 41;
const int BRANCHZERO = 42;

const int HALT = 43;

bool ERROR;
//# define Msize 100;

class Simpletron{
	private:
		int accumulator;
		int instructionCounter;
		int operationCode;
		int operand;
		int instructionRegister;
		int memory[];

	public:
		Simpletron(){};
		void LoadMemory();
		void DisplayMemory();
		void Execute();
		
};

void Instructions(void);

int main(){
	Simpletron mycomputer;

	Instructions();

	mycomputer.LoadMemory();	
	mycomputer.Execute();
	
	system ("pause");
	return 0;
}

// Functions

void Instructions(){
	cout << "*** Welcome to Simpletron! ***" << endl;
	cout << "*** Please enter your program one instruction ***" << endl;
	cout << "*** (or data word) at a time.  I will type the ***" << endl;
	cout << "*** location number and a question mark (?). ***" << endl;
	cout << "*** You then type the word for that location. ***" << endl;
	cout << "*** Type the sentinel -99999 to stop entering ***" << endl;
	cout << "*** your program. ***" << endl;
}

bool FileExists(string file){
// Funcion para saber si un curso existe o no existe.
    ifstream MySimpletron;
    MySimpletron.open(file.c_str(),ifstream::in);
    
    if(MySimpletron.good()){
        MySimpletron.close();
        return true ;
    }
        return false;
    }

void Simpletron::DisplayMemory(){
	int memory[100];
	int Msize = 100;	

	cout << "\nREGISTERS: " << endl;

	cout << "accumulator        \t";
	if (accumulator < 10){
		cout << "000";
	}
	else if(accumulator < 100 && accumulator > 9){
		cout << "00";
	}
	else if(accumulator <= 999 && accumulator > 99){
		cout << "0";
	}
	cout << accumulator << endl;

	cout << "instructionCounter \t";
	if (instructionCounter < 10)
		cout << "0";
	cout << instructionCounter << endl;

	cout << "instructionRegister\t"; 
	if (instructionRegister < 10){
		cout << "000";
	}
	else if(instructionRegister < 100 && instructionRegister > 9){
		cout << "00";
	}
	else if(instructionRegister <= 999 && instructionRegister > 99){
		cout << "0";
	}

	cout << instructionRegister << endl;

	cout << "operationCode      \t";
	if (operationCode < 10)
		cout << "0";
	cout << operationCode << endl;

	cout << "operand            \t";
	if (operand < 10)
		cout << "0";
	cout << operand << endl;

	cout << "\nMEMORY: " << endl;
		ifstream MySimpletron;
		for(int i = 0; i < Msize;i++){
		MySimpletron >> instructionRegister ;
				memory[Msize] = instructionRegister;
				operationCode = instructionRegister/100;
				operand = instructionRegister%100;
				cout << setw(5) << memory[operand] << setw(5);
		}				
}

void Simpletron::LoadMemory(){
	ifstream MySimpletron; // To read the file named MySimpletron.
	string file; // To store the name of the file.
	float memory[100];
	accumulator = 0;
	instructionCounter = 0;
	instructionRegister = 0;
	operationCode = 0;
	operand = 0;
    
	cout << "\nEnter the name of the file: " << endl;
    cin >> file; 
     
    MySimpletron.open(file.c_str(),ifstream::in); // To open the file.
    int Msize = 0; // To clean the array when it opens the file again. 
               
	if(MySimpletron){ // This displays the contents of the file, if it was able to open.    
		cout << "\nThe instructions contained in the file " << file << " are the following: " << endl;
         
		MySimpletron >> instructionRegister ;
			while (!MySimpletron.eof()){ // It keeps reading the file until it ends.
				memory[Msize] = instructionRegister;
				MySimpletron >> instructionRegister ;
				cout << setw(5) << memory[Msize] << setw(5);
				instructionCounter++;
				operationCode = instructionRegister/100;
				operand = instructionRegister%100;
				Msize++;
				
			}
     
		cout << "\nThe information in the file has been read." << endl; 
               
	}
	else if(MySimpletron.fail()){ // This indicates that the file was not able to open.
			cout << "\nThe file was not able to be opened because it does not exist. " << endl;
	}   
               
	MySimpletron.close(); // To close the file.

}


void Simpletron::Execute(){
	Simpletron mycomputer;

	do{
		instructionRegister = memory[instructionCounter];
	
		mycomputer.DisplayMemory();
		
		switch(operationCode){
			case READ:
				cout << "Enter an integer: " << endl;
				cin >> memory[operand];
				instructionCounter++;
			break;
			case WRITE:
				cout << memory[operand] << endl;
				instructionCounter++;
			break;
			case LOAD:
				accumulator = memory[operand];
				instructionCounter++;
			break;
			case STORE:
				memory[operand] = accumulator;
				instructionCounter++;
			break;
			case ADD:
				accumulator += memory[operand];
				instructionCounter++;
			break;
			case SUBTRACT:
				accumulator -= memory[operand];
				instructionCounter++;
			break;
			case MULTIPLY:
				accumulator *= memory[operand];
				instructionCounter++;
			break;
			case DIVIDE:
				if (memory[operand] == 0){
					cout << "*** Attempt to divide by zero ****" << endl;
					cout << "*** Simpletron execution abnormally terminated ***" << endl;
					exit(1);
					break;
				}
				accumulator /= memory[operand];
				instructionCounter++;
			break;
			case BRANCH:
				instructionCounter = operand;
				instructionCounter++;
			break;
			case BRANCHNEG:
				if(accumulator < 0)
					instructionCounter = operand;
				instructionCounter++;
			break;
			case BRANCHZERO:
				if(accumulator == 0)
					instructionCounter = operand;
				instructionCounter++;
			break;
			case HALT:
				cout << "*** Simpletron execution terminated ***" << endl;
				system("pause");
				exit (0);
			break;
			default:
				cout << "\nERROR: Invalid SML instruction -- Program terminating" << endl;
				system("pause");
				exit (1);
			break;
		}
	}while (instructionRegister != HALT && instructionRegister != 1);

}

Recommended Answers

All 4 Replies

So you have to make conclusions and go through a dry run/debugger.


And I did a peer review of your code.It contains the data duplication and
redundancy errors.

and in the line number 131:139 you are using a ifstream before opening it.

Check those for me , I'll help you.

Hey, well my partner and I changed it up a bit, but now the program does not exit due to HALT, instead it is because of ERROR. Also, the last instruction won't display, and could you give a couple of examples of redundancy errors/data duplication? Anyhow, here's the update and thanks for your help:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

const int READ = 10;
const int WRITE = 11;

const int LOAD = 20;
const int STORE = 21;

const int ADD = 30;
const int SUBTRACT = 31;
const int DIVIDE = 32;
const int MULTIPLY = 33;

const int BRANCH = 40;
const int BRANCHNEG = 41;
const int BRANCHZERO = 42;

const int HALT = 43;

bool ERROR;
//# define Msize 100;

class Simpletron{
	private:
		int accumulator;
		int instructionCounter;
		int operationCode;
		int operand;
		int instructionRegister;
		int memory[];

	public:
		Simpletron(){};
		void LoadMemory();
		void DisplayMemory();
		void Execute();
		
};

void Instructions(void);

int main(){
	Simpletron mycomputer;

	Instructions();

	mycomputer.LoadMemory();	
	mycomputer.Execute();
	
	return 0;
}

// Functions

void Instructions(){
	cout << "*** Welcome to Simpletron! ***" << endl;
	cout << "*** Please enter your program one instruction ***" << endl;
	cout << "*** (or data word) at a time.  I will type the ***" << endl;
	cout << "*** location number and a question mark (?). ***" << endl;
	cout << "*** You then type the word for that location. ***" << endl;
	cout << "*** Type the sentinel -99999 to stop entering ***" << endl;
	cout << "*** your program. ***" << endl;
}

bool FileExists(string file){
// Funcion para saber si un curso existe o no existe.
    ifstream MySimpletron;
    MySimpletron.open(file.c_str(),ifstream::in);
    
    if(MySimpletron.good()){
        MySimpletron.close();
        return true ;
    }
        return false;
    }

void Simpletron::DisplayMemory(){
	int memory[100];
	int Msize = 0;
	int size = 0;
	ifstream MySimpletron;
	string file;

	cout << "\nEnter the name of the file: " << endl;
    cin >> file; 

	cout << "\nREGISTERS: " << endl;

	cout << "accumulator        \t";
	if (accumulator < 10){
		cout << "000";
	}
	else if(accumulator < 100 && accumulator > 9){
		cout << "00";
	}
	else if(accumulator <= 999 && accumulator > 99){
		cout << "0";
	}
	cout << accumulator << endl;

	cout << "instructionCounter \t";
	if (instructionCounter < 10)
		cout << "0";
	cout << instructionCounter << endl;

	cout << "instructionRegister\t"; 
	if (instructionRegister < 10){
		cout << "000";
	}
	else if(instructionRegister < 100 && instructionRegister > 9){
		cout << "00";
	}
	else if(instructionRegister <= 999 && instructionRegister > 99){
		cout << "0";
	}

	cout << instructionRegister << endl;

	cout << "operationCode      \t";
	if (operationCode < 10)
		cout << "0";
	cout << operationCode << endl;

	cout << "operand            \t";
	if (operand < 10)
		cout << "0";
	cout << operand << endl;

	cout << "\nMEMORY: " << endl;

		MySimpletron.open(file.c_str(),ifstream::in); // To open the file.
		
		MySimpletron >> instructionRegister ;
			while (!MySimpletron.eof()){ // It keeps reading the file until it ends.
				memory[Msize] = instructionRegister;
				MySimpletron >> instructionRegister ;
				cout << setw(5) << memory[Msize] << setw(5);
				Msize++;
			}

/*
		MySimpletron >> instructionRegister;
			while ( MySimpletron.eof()){ // It keeps reading the file until it ends.
				for(int i = 0; i < Msize; i++){
					MySimpletron >> instructionRegister ;
					memory[Msize] = instructionRegister;
					operationCode = instructionRegister/100;
					operand = instructionRegister%100;
					cout << setw(5) << memory[Msize] << setw(5);
				}
			}
*/		
		MySimpletron.close(); // To close the file.
}

void Simpletron::LoadMemory(){
	ifstream MySimpletron; // To read the file named MySimpletron.
	string file; // To store the name of the file.
	int memory[100];
	int instructions;

	accumulator = 0;
	instructionCounter = 0;
	instructionRegister = 0;
	operationCode = 0;
	operand = 0;
    
	cout << "\nEnter the name of the file: " << endl;
    cin >> file; 
     
    MySimpletron.open(file.c_str(),ifstream::in); // To open the file.
    int size = 0; // To clean the array when it opens the file again. 
               
	if(MySimpletron){ // This displays the contents of the file, if it was able to open.    
		cout << "\nThe instructions contained in the file " << file << " are the following: " << endl;
         
		MySimpletron >> instructions ;
			while (!MySimpletron.eof()){ // It keeps reading the file until it ends.
				memory[size] = instructions;
				MySimpletron >> instructions ;
				cout << setw(5) << memory[size] << setw(5);
				size++;
			}

			/*while (!MySimpletron.eof()){ // It keeps reading the file until it ends.
				memory[Msize] = instructionRegister;
				MySimpletron >> instructionRegister ;
				cout << setw(5) << memory[Msize] << setw(5);
				instructionCounter++;
				operationCode = instructionRegister/100;
				operand = instructionRegister%100;
				Msize++;
				
			}*/
     
		cout << "\nThe information in the file has been read." << endl; 
               
	}
	else if(MySimpletron.fail()){ // This indicates that the file was not able to open.
			cout << "\nThe file was not able to be opened because it does not exist. " << endl;
	}   
               
	MySimpletron.close(); // To close the file.

}


void Simpletron::Execute(){
	Simpletron mycomputer;

	do{
		//instructionCounter = 0;
		//for(;;){
		instructionRegister = memory[instructionCounter];
		operationCode = instructionRegister/100;
		operand = instructionRegister%100;
	
	//	instructionCounter++;
		mycomputer.DisplayMemory();
		
		switch(operationCode){
			case READ:
				cout << "Enter an integer: " << endl;
				cin >> memory[operand];
				instructionCounter++;
			break;
			case WRITE:
				cout << memory[operand] << endl;
				instructionCounter++;
			break;
			case LOAD:
				accumulator = memory[operand];
				instructionCounter++;
			break;
			case STORE:
				memory[operand] = accumulator;
				instructionCounter++;
			break;
			case ADD:
				accumulator += memory[operand];
				instructionCounter++;
			break;
			case SUBTRACT:
				accumulator -= memory[operand];
				instructionCounter++;
			break;
			case MULTIPLY:
				accumulator *= memory[operand];
				instructionCounter++;
			break;
			case DIVIDE:
				if (memory[operand] == 0){
					cout << "*** Attempt to divide by zero ****" << endl;
					cout << "*** Simpletron execution abnormally terminated ***" << endl;
					exit(1);
					break;
				}
				accumulator /= memory[operand];
				instructionCounter++;
			break;
			case BRANCH:
				instructionCounter = operand;
				instructionCounter++;
			break;
			case BRANCHNEG:
				if(accumulator < 0)
					instructionCounter = operand;
				instructionCounter++;
			break;
			case BRANCHZERO:
				if(accumulator == 0)
					instructionCounter = operand;
				instructionCounter++;
			break;
			case HALT:
				cout << "\n*** Simpletron execution terminated ***" << endl;
				system ("pause");
				exit (0);
			break;
			default:
				cout << "\nERROR: Invalid SML instruction -- Program terminating" << endl;
				system ("pause");
				exit (1);
			break;
		}
		//}// end for the For

	}while (instructionRegister != HALT && instructionRegister != 1);

}

data duplication means the errors like this.
for a example in the LoadMemory function you again define memory array while
memory is still there also on the class. So the local variable memory array
will be filled (because of scope resolution) and when it comes end of the scope
it will destroy the array and the memory in the class object will not be
filled.

I understand you got a partner, that's good so you can do peer review.
for a example he should be able to read and understand and describe
what the code/segment of code(sey function LoadMemory())does.Do code
review until understand the functionality of the code.And don't give
hints more than C++ comments to your friend when he is reviewing your
code.You can review your friends code too.This is a good way of finding the
bugs.specially the logical error.

Can anyone else revise this code? I changed the revised version but i still cant get it to work all the way... Your help would be much apriciated :) thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.