4

I am trying to do some homework and I am running into a problem. My homework is

•Using a for loop, print the contents of the array.

The output should appear like this:

PRINTING CONTENTS OF ARRAY

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

I have my code and it is working. However at the end of my alphabet a bunch of random characters and symbols appear. Is there anything you guys see that I am doing wrong?

Here is my code.

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
// main FUNCTION BEGIN PROGRAM EXECUTION
int main()
{
// ALPHABET ARRAY DECLARATIONS
char Letters [26] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
for (int i = 0; i < 27 ; i++)
{
cout<< "PRINTING CONTENTS OF ARRAY" << endl;
cout<< "===============================" << endl;
cout<< Letters << " ";
cout<< endl;
break;
}
return 0 ;
} // ***END main FUNCTION***

Basically the problem to put it simply is that it prints A-Z like I want it to but also prints a bunch of random symbols after Z. Any Ideas? As I am new to this I am sure it's probably something simple. Thanks in advance!

asked Feb 3, 2013 at 3:46
3
  • You have an off-by-one error. Commented Feb 3, 2013 at 3:48
  • 1
    But that's besides the point, right? Jack isn't actually looping. Doesn't the << need a null terminated character array? Commented Feb 3, 2013 at 3:49
  • 4
    You also have break; in there for no reason. You know that breaks the for loop right? Commented Feb 3, 2013 at 3:51

5 Answers 5

4

When you are passing an array of characters to an output stream, the array must be null terminated. Your array is not null terminated; that's why you get extra garbage at the end.

However, this is not the main problem of your solution: you need to index letters, like this:

cout << Letters[i] << " ";

Finally, with 26 letters, indexes go zero to 25, so the loop continuation condition should be i < 26, i != 26, or i <= 25.

answered Feb 3, 2013 at 3:49
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. Josephs comment showed me how to do what you stated! I appreciate the help!
Ah I see what you mean! I will check into that and see what happens. I got the desired print out but from what I understand it is still wrong. ^_^
I made the changes Letters[i] and i < 26 and it simply prints only A now
@Jack That's because you need to remove the break. What happens when you have a break is that the loop is effectively gone: it goes through the first iteration, and drops out immediately without going back.
1

To remove the "bunch of random characters and symbols", add a NUL to the end of Letters:

char Letters [] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 
 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 
 'U', 'V', 'W', 'X', 'Y', 'Z', 0};

This is will print the alphabet 26 times, which may not be what you want. Edit: Previous statement wrong--I missed your break;.

answered Feb 3, 2013 at 3:49

8 Comments

This actually fixed my problem Thank you so much! I switched Letters [26] to [27] added the 0 and it worked like a charm! I appreciate it!
@Jack How could this possibly fix your problem, if you end up with 27 printouts instead of just one? ;-)
@dasblinkenlight, The break statement :(
@chris Isn't it funny how two wrongs sometimes make a right? The teacher is not going to like this solution, though :)
I am not sure. I am still new. However when I added the 0 after Z in the array it gave me an error: to many intializer values. I changed the 26 to 27 and it fixed the problem. If I had to guess I would say the 27th null terminated the array as das stated above. I will put this as my accepted answer when the 9 minute time ends.
|
1

Add a null at the end of array. c functions checks for a null character to terminate.

answered Feb 3, 2013 at 3:54

Comments

0

Array index starts from 0 to N, you have 26 characters in Letters, the index starts from 0 to 25:

cout<< "PRINTING CONTENTS OF ARRAY" << endl;
cout<< "===============================" << endl;
for (int i = 0; i < 26 ; i++)
{
 cout<< Letters[i] << " ";
}
cout<< endl;

break; is normally used to break for or while loop under certain condition. You for loopfinishes smoothly, you don't need abreak` in the loop.

answered Feb 3, 2013 at 3:47

2 Comments

Thanks billz, I changed it but it is still giving me the same problem. After Z it is printing a bunch of symbols.
@Jack, sorry I had typo, I've fixed it
0

char arrays in C++ are null terminated and starts from zero based index hence when you intialise the array

char Letters [26] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

You did not take into account that you require 1 more space for '0円' to terminate the array which causes undefined behaviour

Since you print Letters[0] to Letters[26], you are again causing undefined behaviour when you write Letters[26] hence the appearance of rubbish values

answered Feb 3, 2013 at 6:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.