2017-04-23 23 views
1

我正在编写一个显示整型数组的程序。我设置了数组的大小,但我想知道如何向用户询问他们想要列出的数组的索引。假设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[](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]; 
} 

司机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; 
} 
+0

请提供你试过的代码。 – Billa

+0

@Billa用代码更新了问题。我做了一个嵌套的类,它应该确定索引并为我的问题创建一个异常。我不完全确定如何去做。 – NacDan

+0

接受和upvote答案,如果你认为它应该可以帮助任何一个在将来 – Billa

回答

0

我想你想显示比输入的指数值较低的所有元素用户:

让阵列[]是大小= 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; 
     } 
+0

请告诉我这是你需要的 – Billa

+0

感谢您的帮助。当我测试它时,无论用户输入多大,它都会显示0。我需要另一个循环来显示显示0-9的数组,所以我保留了相同的代码,但添加了你的建议,这就是结果。 – NacDan

+0

当然,您很快就会回复并提供帮助。不幸的是,upvote不会显示给我,但我离开它:) – NacDan

0

所有元素这不是你想要做的吗?为什么这么简单的问题有那么多代码?

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

我正在练习课程和例外。 int数组[SIZE]需要是一个不是整数的对象。因此,以这种方式做一个while循环不会工作。尽管感谢您的帮助! – NacDan

0

你可以尝试这样的事:

#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; 
} 
相关问题