0

I am very new to C++ and appreciate all the help, so please be patient with me! I was tasked with writing a C++ program which essentially emulates a simple CPU.

My overall goal is to have the user input various 3 digit numbers into an array I have created called "memory". The array "memory" will have 100 available locations and the user is allowed to load their input into any available spots (the array is sized [100][2] because i want the last two digits to be treated as a single number).

The variable "programCounter" will represent the beginning location for where the user input will be stored within the array. So, for example, if the user inputs the value "20" for programCounter, then the user's input will be stored starting from the 21st location in the array and beyond, until their input is finished. What I have written below does not work and the loop for the user input into "memory" never ends. Is there another way to have the user input their values into the array, and then provide some sort of exit code to let the program know that the user is finished inputting?

Here is my code:

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main (void)
{
    string name;
    char inputCardResponse;
    ifstream inFile;
    ofstream outFile;
    int memory [100][2] = {001};     // sets the value of the first memory spot to "001"
    int inputCard [16][2];                      
    int outputCard [16][2];                     
    int instructionRegister;                    
    double accumulator;                         
    int programCounter;                        


    cout << "Hello! Welcome to Simple Computer Version 1.0.\nWhat is your name? \n";
    getline(cin, name);
    cout << "Thank you for using my Simple Comuter "<<name<<"!\n";
    cout << "Let's get started!\n";
    cout << "Below is the table of Opcodes and their functions:";
    cout << endl << endl;
    {
        cout << setw(9) << "|  Opcode" << setw(20) << setfill('-') << "Function" << setw(12) << "|" << endl;
        cout << setw(9) << "|  ------" << setw(20) << setfill(' ') << "-------" << setw(12) << "|" << endl;
        cout << setw(5) << "|  0_ _" << setw(20) << setfill('-') << "Input" << setw(14) << "|" << endl;
        cout << setw(5) << "|  1_ _" << setw(21) << setfill('-') << "Output" << setw(13) << "|" << endl;
        cout << setw(5) << "|  2_ _" << setw(18) << setfill('-') << "Add" << setw(16) << "|" << endl;
        cout << setw(5) << "|  3_ _" << setw(23) << setfill('-') << "Subtract" << setw(11) << "|" << endl;
        cout << setw(5) << "|  4_ _" << setw(22) << setfill('-') << "Load AC" << setw(12) << "|" << endl;
        cout << setw(5) << "|  5_ _" << setw(23) << setfill('-') << "Store AC" << setw(11) << "|" << endl;
        cout << setw(5) << "|  6_ _" << setw(19) << setfill('-') << "Jump" << setw(15) << "|" << endl;
        cout << setw(5) << "|  7_ _" << setw(22) << setfill('-') << "Test AC" << setw(12) << "|" << endl;
        cout << setw(5) << "|  8_ _" << setw(23) << setfill('-') << "Shift AC" << setw(11) << "|" << endl;
        cout << setw(5) << "|  9_ _" << setw(19) << setfill('-') << "Halt" << setw(15) << "|" << endl;
    }
    cout << endl << endl;

    //Input section
    cout << "Please plan your program out. This emulator requires the user to enter a starting value";
    cout << "for the program counter (typically cell 20 is chosen)\n";
    cout << "When you are ready, please enter the starting cell you have chosen for the program counter: ";
    cin >> programCounter;                      // Initializes the program counter value
    cout << "Now that you have chosen a starting cell, please start entering your program: \n";

    // This loop stores the user's program into the array named "memory". What happens if input<100??
    for(;programCounter < 100; programCounter++)
    {
        cin >> memory[programCounter][2];

    }

    cout << "Do you have any information to store in the input card?\n";
    cout << "(Please input uppercase Y for Yes or N for No \n";
    cin.get(inputCardResponse);
    if(inputCardResponse == 'Y')
    {
        cout << "There are 15 input slots available. Please keep this in mind when inputting: \n";
        for (int inputCounter=0; inputCounter < 15; inputCounter++)
        {
            cin >> inputCard[inputCounter][2];
        }
    }
    else{
        cout << "Most programs require inputs.\n";
        cout << "Please come back when you are ready with a file!\n";
    }
    return 0;

}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • You are trying to use value of 2 for indexing the second dimension of `memory` and `inputCard` when this index should be less than 2. Array indices start from 0 in C/C++. So the only valid values for this index are 0 and 1. Also, what do you mean by "(the array is sized [100][2] because i want the last two digits to be treated as a single number)." Can you give an example and explain why you think this array size would help? – crayzeewulf Nov 07 '13 at 00:09
  • to reiterate what @crayzeewulf said, arrays in C/C++ and most if not all programming languages use a `n - 1` based array index system. As in the example `arr[2]` you want to access the 3rd element of the array `arr`. An array's range goes from `0 - (n - 1)` n here being the initial size of the array i.e. `int arr[10]` in this case `n = 10` so this array's range is `0 - 9` – PurityLake Nov 07 '13 at 00:13
  • @crayzeewulf - the user will input a 3 digit number (such as 345). The first digit "3" will represent an opcode and the remaining digits "45" will represent either the value to be manipulated, or the location of a spot in the array "memory". That's why I was thinking of making the array have 100 rows and 2 columns. Is there another way to accomplish this? Thank you! – user2962702 Nov 07 '13 at 00:41

2 Answers2

0

C/C++ arrays are 0-indexed. So if you have array, your indexes will be from 0 to (array size-1)

int a[5]; //You initialized array of 5 integers. You can only access this elements: a[0], a[1], a[2], a[3], a[4]

You are accessing an element out of bounds. In these lines:

cin >> memory[programCounter][2];
cin >> inputCard[inputCounter][2];

Change 2 to 0 or 1, so you don't access element which is out of bounds. Also, you don't have any variable which represents you the limit of input. Your limit is 100 so if you input a small number it will require lot of input numbers from you, so maybe that's why you think it will never stop input (but it actually will when it reaches 100). You should make a variable which represents you a limit of input, so you don't have this:

for(;programCounter < 100; programCounter++)
{
    cin >> memory[programCounter][2];

}

but this:

for(;programCounter < inputLimit; programCounter++)
{
    cin >> memory[programCounter][2];

}

Hope this helps!

Luka Tiger
  • 298
  • 2
  • 4
  • 10
0

This answer will not solve your problem but will get you on the path. Also, this is a response to the following comment:

@crayzeewulf - the user will input a 3 digit number (such as 345). The first digit "3" will represent an opcode and the remaining digits "45" will represent either the value to be manipulated, or the location of a spot in the array "memory". That's why I was thinking of making the array have 100 rows and 2 columns. Is there another way to accomplish this? Thank you!

I now see what you meant in your original question. However, assigning values to or reading values via the >> operator into variables does not work the way you might be expecting. When you declare an array like the following:

int memory[100][2] ;

you are allocating space for 200 integers that you can imagine being arranged in a table with 100 rows and 2 columns. The first row has index 0 and first column has index 0. The last row has an index 99 and last column has index 1:

       +------------+------------+
       |  Column 0  |  Column 1  |
       +------------+------------+
Row  0 |            |            |
       +------------+------------+
Row  1 |            |            |
       +------------+------------+
                   ...
       +------------+------------+
Row 99 |            |            |
       +------------+------------+

Each cell in this table may only store an int value. When you initialize memory[][] using a statement such as the following (I am using 123 instead of 001 for a reason, see more comments later):

int memory[100][2] = {123} ;

the program simply puts the integer 123 in column 0 of row 0:

       +------------+------------+
       |  Column 0  |  Column 1  |
       +------------+------------+
Row  0 |    123     |      0     |
       +------------+------------+
Row  1 |      0     |      0     |
       +------------+------------+
                   ...
       +------------+------------+
Row 99 |      0     |      0     |
       +------------+------------+

The compiler or resulting program has no way of knowing that the number needs to be split into 1 and 23. It does not put 1 in column 0 and 23 in column 1 of the first row, for example. You will have to figure out a way to do this yourselves.

Going back to your original code, it used a value of 001 to initialize the array:

int memory[100][2] = {001} ;

C++ treats integer literals that start with 0 very differently. A leading 0 in literals causes C/C++ compilers to treat them as octal values. So, be very careful when you write literals that have leading zeroes. For example 012 is not equal to 12 in C++.

Finally, if you try to put something in column 2 of this table you are asking for a lot of trouble. The table only contains columns 0 and 1. When you try to place values into the non-existing column 2, the values may end up at unexpected locations in the memory being used by your program. The behavior of the program is unpredictable in this case and will most likely be entirely different from what you expected.

As you are trying to learn C++, I would suggest you read about how arrays and variables work from one or more of the books listed here. Once you have a better handle on the basic concepts, a great way to learn more advanced and application-specific concepts is to look at existing code that does something similar. If you search for similar codes on Google or StackOverflow, you will find several ways of approaching this assignment. Carefully study the codes that you find, make sure you understand them, run them, tweak them if you can and see what happens, etc. Now that you are armed with this additional knowledge, write your own code from scratch. Hope that gets you the answer. I also highly recommend following the suggestions of this answer before you ask your question on SO. :) Cheers!

Community
  • 1
  • 1
crayzeewulf
  • 5,840
  • 1
  • 27
  • 30