C ++用户输入操作

时间:2013-11-06 23:55:49

标签: c++ arrays

我对C ++很陌生并感谢所有的帮助,所以请耐心等待我!我的任务是编写一个C ++程序,它实际上模拟了一个简单的CPU。

我的总体目标是让用户在我创建的名为“memory”的数组中输入各种3位数字。阵列“存储器”将具有100个可用位置,并且允许用户将其输入加载到任何可用的点(阵列的大小为[100] [2],因为我希望将最后两位数字视为单个数字)。

变量“programCounter”将表示用户输入将存储在数组中的起始位置。因此,例如,如果用户为programCounter输入值“20”,则用户的输入将从数组中的第21个位置开始存储,直到输入完成为止。我在下面写的内容不起作用,用户输入“内存”的循环永远不会结束。是否有另一种方法让用户将其值输入到数组中,然后提供某种退出代码以让程序知道用户已完成输入?

这是我的代码:

#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;

}

2 个答案:

答案 0 :(得分:0)

C / C ++数组是0索引的。因此,如果您有数组,则索引将从0到(数组大小-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]

您正在访问一个超出范围的元素。 在这些方面:

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

将2更改为0或1,因此您不会访问超出范围的元素。 此外,您没有任何表示输入限制的变量。你的限制是100,所以如果你输入一个小数字,你将需要很多输入数字,所以也许这就是为什么你认为它永远不会停止输入(但实际上它会达到100)。你应该创建一个代表你输入限制的变量,所以你没有这个:

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

}

但是这个:

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

}

希望这有帮助!

答案 1 :(得分: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} ;

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

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

编译器或生成的程序无法知道该号码需要分为123。例如,1放在第0列,将23放在第1行的第1列中。你必须想办法自己做这件事。

回到原始代码,它使用值001初始化数组:

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

C ++以不同的方式处理以0开头的整数文字。文字中的前导0会使C / C ++编译器将其视为octal values。因此,在编写具有前导零的文字时要非常小心。例如,012 等于C ++中的12

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

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