I can't make my array print. The first element prints but it will not print the remaining. Need out of bounds error on negative index?
-
#include <iostream> using namespace std; class myArray { public: myArray(int n) { start = 0; arrayLength = n; array = new int[n, 0]; for (int i = 0; i ...show more
-
Answer:
First, it makes no sense to test if index < 0 after it already was tested to be greater than 0 on the first if statement... Second, you shouldn't be using int for indexes you should be using size_t which is an unsigned integral value... there are no negatives All the object has to do is keep track of size (your arrayLength variable), if an index comes in larger than the size then throw an error, otherwise return the value at that position. EDIT: Same applies to your constructor... use size_t or else you'll need to check for negative values there too, and everywhere else. EDIT2: oh and for the love of everything holy please please please add a destructor and delete[] your array EDIT3: as far as allocation goes... if someone passes a negative number the actual value is HUGE and will through a bad_alloc error surround your new allocation with a try catch block to see if someone is trying to allocate more than the OS will allow or set an arbitrary max size and check it before attempting to allocate the memory.
HTSXKH2LA35JJDGUJANQC4MMOY at Yahoo! Answers Visit the source
Related Q & A:
- Does anyone know why sometimes I pig out for a few days, then other days i can't eat anything at all?Best solution by Yahoo! Answers
- I can't see what people are typing in the chat rooms ever since I was booted? Help.Best solution by answers.yahoo.com
- I can't open my e-mails. how do I fixed.Best solution by Yahoo! Answers
- I can't download MSN messenger, it says I already have it.Best solution by Yahoo! Answers
- What will happen if I can't pay back the student loan?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.