输入要在阵列中显示的数量

uid 发布于 2019-10-09 arrays 最后更新 2019-10-09 22:59 10 浏览

我正在编写一个显示整型数组的程序。我设置了数组的大小,但我想知道如何向用户询问他们想要列出的数组的索引。说const SIZE = 10,用户希望看到数组中的前三个。我还想写一个异常,如果用户输入超出数组大小,就会捕获错误。如果您需要查看一些代码,请告诉我。任何帮助表示赞赏! 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;  // 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
{
    if (sub < 0 || sub >= arraySize)
        subscriptError();
    return aptr[sub];
}
驱动程序file.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;
}
已邀请:

zvelit

赞同来自:

我想你想要显示低于用户输入的索引值的所有元素: 令array []为size = 10的数组名称,您可以从用户获取索引值(例如l)并在for loop中使用该值来打印索引中低于l的所有元素

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

snihil

赞同来自:

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

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

eomnis

赞同来自:

你可以尝试这样的事情:

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