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;

}
4

2 回答 2

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!

于 2013-11-07T00:26:39.567 回答
0

这个答案不会解决您的问题,但会让您走上正轨。此外,这是对以下评论的回应:

@crayzeewulf - 用户将输入一个 3 位数字(例如 345)。第一个数字“3”将代表一个操作码,其余数字“45”将代表要操作的值或数组“内存”中某个点的位置。这就是为什么我想使数组有 100 行和 2 列。还有另一种方法可以做到这一点吗?谢谢!

我现在明白你在原来的问题中的意思了。但是,通过运算符将值分配给变量或将值读取>>到变量中并不像您期望的那样工作。当您声明如下数组时:

int memory[100][2] ;

您正在为 200 个整数分配空间,您可以想象这些整数排列在 100 行和 2 列的表中。第一行的索引为 0,第一列的索引为 0。最后一行的索引为 99,最后一列的索引为 1:

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

此表中的每个单元格只能存储一个int值。当您memory[][]使用如下语句进行初始化时(我使用123而不是001出于某种原因,稍后请参阅更多评论):

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

该程序只是将整数放在第1230 行的第 0 列中:

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

编译器或生成的程序无法知道该数字需要拆分为123。例如,它不会放在第一行的第1023列和第 1 列中。你必须自己想办法做到这一点。

回到你的原始代码,它使用一个值001来初始化数组:

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

0C++ 对待以非常不同的开头的整数文字。文字中的前导0导致 C/C++ 编译器将它们视为八进制值。因此,在编写带有前导零的文字时要非常小心。例如在 C++012等于。12

最后,如果您试图在该表的第 2 列中添加一些内容,您会遇到很多麻烦。该表仅包含第 0 列和第 1 列。当您尝试将值放入不存在的第 2 列时,这些值可能会最终出现在程序正在使用的内存中的意外位置。在这种情况下,程序的行为是不可预测的,并且很可能与您的预期完全不同。

当您尝试学习 C++ 时,我建议您从此处列出的一本或多本书籍中了解数组和变量的工作原理。一旦您更好地掌握了基本概念,学习更高级和特定于应用程序的概念的一个好方法是查看执行类似操作的现有代码。如果您在 Google 或 StackOverflow 上搜索类似的代码,您会发现几种处理此作业的方法。仔细研究你找到的代码,确保你理解它们,运行它们,如果可以的话,调整它们,看看会发生什么,等等。现在你已经掌握了这些额外的知识,从头开始编写你自己的代码。希望能给你答案。我也强烈建议您遵循此建议在您提出关于 SO 的问题之前先回答。:) 干杯!

于 2013-11-07T05:43:29.840 回答