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!
5 Answers 5
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.
4 Comments
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.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;.
8 Comments
Add a null at the end of array. c functions checks for a null character to terminate.
Comments
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.
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
<<need a null terminated character array?break;in there for no reason. You know that breaks the for loop right?