I have an array consisting 5 int values. When I try to get the value from one of the elements in the array I just get something that looks like the address.
Can anyone please point out what's wrong with the code?
In line 13, you should put in the number of elements in your array when declaring it.
In line 33, the int numbers[5] is a locally defined array that exists only in the constructor. It is not the same as the numbers array member of your class PhoneBook.
Hi, and thank you for the reply. I don't want to set numbers to constant, only use some specified numbers if the user does not input any. The tip about line 33 was very good though, obviously I can't declare numbers twice.
I have changed the code and it does what I want, but I don't know if it's the best way to do it.
When I ran your code, the loop from line 37-39 only looped once. This is due to your for loop condition in line 37.... while your idea is good, your implementation is not correct.
sizeof(specified) is not the sizeof(int) * number of elements in specified.
This is because specified is actually a constant integer pointer, i.e. constint *specified
So sizeof(specified) gives you the size of an int pointer rather than the size of the array. I would recommend just hard coding the 3 in.
And you should avoid using line 31 system("pause"); It is not portable, slow, dangerous and there are alternatives (I would recommend getline(cin, throwAwayString);).
Hard coding solves my problem in this case because I know the length, but how do I solve it if I don't know? I know that I can use vectors, but I want to be familiar with arrays (which I'm not..).
Thank you for telling med about system(), I choose your solution.
If you're going to use arrays you would always know the length since all arrays are fixed at the size you declare them at. Unless you intend to only use your array partially, then you probably would need to keep track of the furthest element you used...
Ok, I think I understand. I can never increase the length of an array after it's declared. The only solution is declaring it with 100+ elements, and keep track of the last element I would like to access (element++ for each "input" to the array). I just found something on dynamic arrays, guess it's worth a look.
Hard coding solves my problem in this case because I know the length, but how do I solve it if I don't know? I know that I can use vectors, but I want to be familiar with arrays (which I'm not..).
I recommend that you use the tool that makes the most sense. Don't try to invent a purpose for a tool that you don't need. It sounds to me like a std::vector is actually the tool that you need and its user interface allows it to work in a way that is nearly identical to the way that you would use a C array anyway.
I think that your getNumbers function is counter-intuitive. It is pluralized but you are only returning one specific number and not the entire array. It should also verify that the input is valid otherwise throw an exception or return a value that can be detected as invalid by the caller.
In your constructor it would take three lines of code to init the 3 elements. Why bother creating an array on the stack and a for loop which results in more complex code and more memory usage with no benefit whatsoever?
I recommend that you use the tool that makes the most sense. Don't try to invent a purpose for a tool that you don't need
Point taken, but I'm doing this in order to know the limitations and possibilities with arrays. I have an examn in july, and sometimes we are asked to do things in a specific manner.
In your constructor it would take three lines of code to init the 3 elements. Why bother creating an array on the stack and a for loop which results in more complex code and more memory usage with no benefit whatsoever?
Done!
I think that your getNumbers function is counter-intuitive. It is pluralized but you are only returning one specific number and not the entire array.
The plan is to lookup a specific number based upon a name, this is not done yet. How can I return the whole array when it's private?