-3

I'm trying to create an array where the user goes out-of-bounds and executes an error message. I wanted to access each element of the array and then once I go past the last element it will execute the message. This is what I have so far

using namespace std;
class safeArray{      //line1     
public: int array[];  //line2
};

void outofBounds(int array[],int sizeofArray);   //line3

int main() {             //line4
int array [3]={2,4,6};   //line5

outofBounds (int array[],int sizeofArray){    //line6
    for (int i=0;i<sizeofArray;i++){        //line7
        i++                                //line8
    }
    if (int i=0;i>sizeofArray){                                 //line9
     cout<<"safeArray array (" <<list[0]<<","<<array[3]<<endl;  //line10
    }
}
return 0;}

I'm getting confused because line 6 is showing up as an error? It's asking for a ( in front of sizeofArray. Why is that?

Edit:

Made some edits. Still getting an error.

#include <iostream>
using namespace std;

class safeArray{
public:
void outofBounds(int,int);
int yourArray[3];
int i;
};

void outofBounds(int,int);
int yourArray[3];
int i;

 int main() {
 void outofBounds(int,int);
 int yourArray[3];   //Error: Unused Variable 'yourArray'
 return 0;
 };

void outofBounds(int yourArray[],int sizeofArray) {

for (i=0;i<sizeofArray;i++){
cout<<"Please enter integer";
cin >>yourArray[i];
yourArray[i]++;

for (i=0;i>sizeofArray;){
cout<<"safeArray yourArray ("<<yourArray[0]<<","<<yourArray[3]<<")"<<endl;
  }
 }    
}
Jen
  • 91
  • 1
  • 3
  • 12

1 Answers1

2
  1. You are not using your class
  2. When you use arrays you have to either put the values in it, or give it a size
  3. You can not make a function in another function
  4. When you have an array say array[3]={2,4,6}, saying array[3], does not make sense, arrays start from 0th index 0->2, 1->4, 2->6
  5. You're not calling your function from anywhere, in order to use the function you need to call it somewhere
  6. Doesn't look like you need a class

Didn't test the code, as I do not understand exactly what you are trying to achieve, however this looks better

void outofBounds(int myArray[],int sizeofArray); 

int main()
 {             
    int myArray [3]={2,4,6};   

    return 0;
}

outofBounds (int myArray[],int sizeofArray)
{    
    for (int i=0;i<sizeofArray;i++)
    {        
        if (int i==0 && i>sizeofArray)
        {                                
         std::cout<<"safeArray array" <<list[0]<<","<<myArray[2]<<std::endl; 
        }

        i++
    }
}
phuclv
  • 37,963
  • 15
  • 156
  • 475
BlooB
  • 955
  • 10
  • 23
  • I made some changes and implemented some edits.I'm still learning cpp and I forgot that the main () is a function. I tried to make the array 3 elements in order to place a boundary. So whenever the user access the "out-of-bounds"element. In this case, I was trying to only have [0][1][2] accessible. Making the user go through a loop to make them go out of bounds. I needed to make a class, it was required.Thanks for your help much appreciated. – Jen Apr 26 '17 at 17:42
  • I am not an expert, however I do not think you can. An out of bounds access to an array causes undefined behavior in C++, it won't throw an exception, if you are really lucky it might crash. If you have to do this use 'std::Array', the 'at' functionality, or use 'vectors' – BlooB Apr 26 '17 at 17:53