Function return address, not value from array

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?

Thanks in advance:)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <cstdlib> 
#include <iostream>
#include <string>
#include <cmath>
#include <cassert>

using namespace std;

class PhoneBook{

      private:
              int totalNumbers;
              int numbers[];
              int numbersUsed[5][7];

      public:
             void incrementDay(int& numberUsed, int, int);
             int getNumbers(int);
                          
             PhoneBook();
};

int main() { 
  PhoneBook book;
  cout << book.getNumbers(2);
  
system("pause");
return(0);
}

PhoneBook::PhoneBook(){
                     totalNumbers = 5;
                     int numbers[5] = {123,345,678,345,234};
}

int PhoneBook::getNumbers(int number){
    return numbers[number];}
Hi cantog,

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <cstdlib> 
#include <iostream>
#include <string>
#include <cmath>
#include <cassert>

using namespace std;

class PhoneBook{

      private:
              int totalNumbers;
              int numbers[3];
              int numbersUsed[5][7];

      public:
             void setNumbers();
             void incrementDay(int& numberUsed, int, int);
             int getNumbers(int);
                          
             PhoneBook();
       
};

int main() 
{ 

  PhoneBook book;
  cout << book.getNumbers(2);
  
system("pause");
return(0);
}

PhoneBook::PhoneBook(){
                       int specified[3] = {345,789,345};
                       for(int i = 0; i < sizeof(specified)/sizeof(int) ; i++){ 
                       numbers[i] = specified[i];}
}

int PhoneBook::getNumbers(int number){
    return numbers[number];}
    
void PhoneBook::setNumbers(){}
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. const int *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);).

You can read Duoas' excellent article on Why system() is evil http://www.cplusplus.com/forum/articles/11153/
Last edited on
closed account (S6k9GNh0)
Instead of using system, you could just as easily do:
1
2
std::cout << "Please press any key to continue..." << endl;
getchar();
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?





Topic archived. No new replies allowed.