2

Apologies if I missed another thread, I looked through the recommended and none are quite like what I am experiencing. I am trying to display the alphabet in both upper-and-lower-case letters. Before I commit to the full thing I wanted to test with just a few:

int main()
{
    char *letters[79] = { "Aa ","Bb ","Cc ", "/0"} ;
    int pause;
    cout << "Letters are: " << letters << endl ;
    cout << "Continue (Y or N)" << endl ;
    cin >> pause;
    cout << endl;

    return 0;
}

When I attempt to compile this has been the result:

console output of above code

What I cannot understand is why I am getting what appears to be a hex output on those letters instead of the letters themself.

8
  • Why are you using arrays? Why are you using char* instead of std::string? To learn, or because you think that's the best approach? Knowing this helps others help you. :) Commented Oct 9, 2017 at 6:24
  • Learning, I am still in college for this and its pretty much my third week, this is the very edge of an assignment using arrays and pointers. Commented Oct 9, 2017 at 6:26
  • letters refer to the memory address of the first element of the array. If you want to print the elements , then use indexes like letters[0] for first element , letters[1] for second element and so on. Commented Oct 9, 2017 at 6:29
  • Side note: what't mthe purpose of "/0"? My crystall ball tells me it does not what you thinkt it does. Commented Oct 9, 2017 at 6:41
  • @MichaelWalz a secondary book I am using said to use it when I am using characters in an array to ... its considered a null character. Commented Oct 9, 2017 at 6:46

2 Answers 2

1

You missed to mention array index letters[0]

Try this:

char *letters[79] = { "Aa ","Bb ","Cc ", "/0"};
cout  << "Letters are: " << letters[0] << endl;

output:

Letters are:  Aa  

Or in loop like this :

char *letters[79] = { "Aa ","Bb ","Cc ", "/0"};
for(int i=0; i<sizeof(*letters); i++)
    cout  << "Letters are: " << letters[i] << endl;

output:

Letters are:  Aa  

Letters are:  Bb  

Letters are:  Cc  

Letters are:  /0 
Sign up to request clarification or add additional context in comments.

Comments

0

Your console output shows the address of the letters, because you are telling it to print the pointer, which points to a memory address. You need a loop to iterate over it. The rest is pointer arithmetic.

Also, "Aa" is not a char type, but "A" and "a" are. So, you should also define them separately; otherwise, it is very likely that you get a compiler warning. Please note that the last character should be "\0", not "/0". If you could try this:

int main()
{
    char *letters[79] = { "A","a","B", "b", "C", "c", "\0"} ;
    int pause;
    cout << "Letters are: ";
    for(int i = 0; i < 79; i++){
        cout << letters[i];
    }
    cout << endl << "Continue (Y or N)" << endl ;
    cin >> pause;
    cout << endl;
    return 0;
}

Then, the output is:

Letters are: AaBbCc

1 Comment

Thank, between you, aghilpro, and Micheal up above this has come along way ... now, to solve the "unhandled exception thrown write access violation _first was nullptr" error that has popped up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.