Code returns memory address instead of expected output

Here is my code:
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
#include <iostream>
#include <vector>

char RandomUpperChar()
{

    int charOne;
    int charTwo;
    int charThree;
    int charFour;
    const std::string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    srand (time(nullptr)); // Initialize random seed
    charOne = rand() % 26 + 1; // Generate a random number from 1-26, representing the alphabet
    charTwo = rand() % 26 + 1;
    charThree = rand() % 26 + 1;
    charFour = rand() % 26 + 1;
    int P[4] = {charOne, charTwo, charThree, charFour};

    std::cout << P;
   

}

int main()
{
    RandomUpperChar();
    return 0;
}



I'm expecting a different output, but my output is the following:
 
0x7fff586bcaf0


Why is this happening?
Last edited on
Why is this happening?

Remember P is an array, so when you try to print P you get the address of the array.

By the way what were you expecting?


Last edited on
jlb, I was expecting the output of charOne...charFour, separated by commas. How can I print that value?
Use a for loop to loop through the array and print it.

1
2
3
4
for(int i = 0; i < 4; i++)
{
    cout << P[i];
}


Since P is an array, you are just printing the address of the array, or the address of P[0].
Here is what I have now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
char RandomUpperChar()
{
    int charOne;
    int charTwo;
    int charThree;
    int charFour;
    const std::string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    srand (time(nullptr)); // Initialize random seed
    charOne = rand() % 26; // Generate a random number from 1-26, representing the alphabet
    charTwo = rand() % 26;
    charThree = rand() % 26;
    charFour = rand() % 26;
    int P[4] = {charOne, charTwo, charThree, charFour};
    for (int i = 0; i < 4; i++) {
        std::cout << characters[P[i]] << std::endl;

    }


}


I get the expected output. Now what I want to do is push each letter generated to one array. In JavaScript, it would be something like push(). Is there something similar I should use? What's the best way to approach this?

Also, I feel as if using charOne, charTwo etc to generate these characters can be written more eloquently. How should I go about that?
Last edited on
C++ doesn't allow extra elements to be added to an array once it's initialized. You may want to check out vectors.

http://www.cplusplus.com/reference/vector/vector/

As for the generation of the characters, there's no saying, since I don't exactly know what the program is trying to do. What you did is fine though. Everyone has their own way of design.
Thanks for the help dasani. I decided to take this approach:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
#include <array>
int RandomUpperChar()
{
    srand (time(nullptr));
    int i;
    int arr[i];
    int P[4];
    const std::string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for (int i = 1; i <=4; i++) {
        arr[i] = rand() % 26;
        std::cout << characters[arr[i]] << std::endl;
    }
};


int main()
{
    RandomUpperChar();
    return 0;
}


Which appears to give me the same output with fewer lines of code (preferable).

I'll say this is "solved".
Good on rewriting the code so that you don't have to initialize it one by one, BUT...

1
2
3
4
5
6
7
8
    int i; // i is actually not initialized here
    int arr[i]; // who knows what array size you are actually creating?
    int P[4];
    const std::string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for (int i = 1; i <=4; i++) {
        arr[i] = rand() % 26; // What you're doing here is actually very dangerous
                                        // You're accessing memory that should not be accessible
                                        // which leads to memory leaks. 
Last edited on
Topic archived. No new replies allowed.