【问题标题】:Inputting the amount you want displayed in the array输入要在数组中显示的数量
【发布时间】:2017-09-19 21:17:53
【问题描述】:

我正在编写一个显示整数数组的程序。我设置了数组的大小,但我想知道如何询问用户他们想要列出的数组的索引。说 const SIZE = 10,用户希望看到数组中的前三个。我还想编写一个 exception,如果用户输入超过数组的大小,它会捕获错误。如果您需要查看一些代码,请告诉我。任何帮助表示赞赏!

intergerarray.h

class IntArray
{
private:
    int *aptr;                     // Pointer to the array
    int arraySize;                 // Holds the array size
    void subscriptError();         // Handles invalid subscripts
public:
    class OutOfBoundException
    {
    public:
        int index;
        OutOfBoundException(){};
        int getInde() { return index; }

    };
    IntArray(int);                 // Constructor
    IntArray(const IntArray &);    // Copy constructor
    ~IntArray();                   // Destructor

    int size() const               // Returns the array size
    {
        return arraySize;
    }

    int &operator[](const int &);  // Overloaded [] operator
};

IntergerArray.cpp

IntArray::IntArray(int s)
{
    arraySize = s;
    aptr = new int[s];
    for (int count = 0; count < arraySize; count++)
        *(aptr + count) = 0;
}


IntArray::IntArray(const IntArray &obj)
{
    arraySize = obj.arraySize;
    aptr = new int[arraySize];
    for (int count = 0; count < arraySize; count++)
        *(aptr + count) = *(obj.aptr + count);
}


IntArray::~IntArray()
{
    if (arraySize > 0)
        delete[] aptr;
}


void IntArray::subscriptError()
{
    cout << "ERROR: Subscript out of range.\n";
    exit(0);
}


int &IntArray::operator[](const int &sub)
{
    if (sub < 0 || sub >= arraySize)
        subscriptError();
    return aptr[sub];
}

驱动文件.cpp

    int main()
{
    int SIZE = 10;
    //int index;
    //cout << "enter an index";
    //cin >> index;
    IntArray table(SIZE);

    for (int x = 0; x < SIZE; x++)
        table[x] = x;

    for (int x = 0; x < SIZE; x++)
        cout << table[x] << " ";
    cout << endl;

    //table[SIZE + 1] = 0;

    return 0;
}

【问题讨论】:

  • 请提供您尝试过的代码。
  • @Billa 用代码更新了问题。我创建了一个嵌套类,它应该确定索引并为我的问题创建一个异常。我不完全确定该怎么做。
  • 如果您认为某个答案对未来的任何人都有帮助,请接受并投票赞成

标签: c++ arrays class exception


【解决方案1】:

你可以试试这样的:

#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>

void print_numbers( const std::vector<int>& array, int nNumbers, const char* pszSeparator )
{
    if ( nNumbers > static_cast<int>(array.size()) )
    {
        throw std::exception();
    }

    std::copy( array.begin(), array.begin() + nNumbers, std::ostream_iterator<int>( std::cout, pszSeparator ) );

}

int main()
{
    std::vector<int> array( 10 );

    //Just for testing
    {
        int n = 0;
        auto generator = [n]() mutable
        {
            return n++;
        };
        std::generate_n( array.begin(), array.size(), generator );
    }

    try
    {
        print_numbers(array, 11, "\n");
    }
    catch ( std::exception e )
    {
        std::cout << "Error message..." << std::endl;
    }
    return 0;
}

【讨论】:

    【解决方案2】:

    我认为您希望显示所有低于用户输入的索引值的元素:

    令array[]为size=10的数组名,你可以从用户那里得到一个索引值(比如l),并在for loop中使用这个值来打印索引低于@987654323的所有元素@

     int array[size] 
    void disp_in(int l)
      {
       if(l>=size) // if l greater than or equal to size (as index start at 0)
          throw l;
       else
           {
    
            cout<<"Elements : ";
            for(int i=0;i<=l;i++) //if we have say l=2 ,array values in indexes 0,1and 2 will be displayed
                cout<<array[i];
    
             }
        }
    int main ()
          {
           int l;
           cout<<"Enter index : "; 
           cin>>l;  //till this index value, the array value will be displayed
           try
              {
               disp_in(l);
                }
           catch(int e)
              {
              cout<<"Index value greater than max index"; 
                }
             return 0;
             }
    

    【讨论】:

    • 请告诉我这是你需要的
    • 感谢您的帮助。当我测试它时,无论用户输入有多大,它都会显示 0。我需要另一个 for 循环来显示显示 0-9 的数组,所以我保留了相同的代码,但添加了您建议的内容,结果就是这样。
    • 您肯定会快速响应并乐于助人。不幸的是,赞成票不会显示给我,但我留下了它:)
    • 现在完美了,请检查并接受(点击答案左上角附近的勾号)。
    • 我正在使用一个对象来访问数组。像这样显示数组: cout
    【解决方案3】:

    这不是你想要做的吗?为什么这么简单的问题要写这么多代码?

        const int arraySize = 10;
        int array[arraySize];
    
        int elementToDis;
        do
        {
            std::cout << "Number of array elements to display: ";
            std::cin >> elementToDis;
        } while (elementToDis > arraySize || elementToDis < 0); // this is your exeption
    
        for (int ccc(0); ccc < elementToDis; ++ccc)
            std::cout << "Index " << ccc << ": " << array[ccc] << '\n';
    

    【讨论】:

    • 我正在练习课程和例外。 int array[SIZE] 需要是一个对象而不是整数。所以以这种方式做一个while循环是行不通的。不过感谢您的帮助!
    猜你喜欢
    • 2022-11-04
    • 2019-01-25
    • 2017-11-16
    • 1970-01-01
    • 2021-10-06
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多