The problem in C ++ arrays

I am trying to do my homework and I have a problem. My homework

β€’ Using the for loop, print the contents of the array.

The output should look like this:

PRINTING MASS CONTENTS

ABCDEFGH I JKLMNOPQRSTUVWXYZ

I have a code and it works. However, at the end of my alphabet a bunch of random characters and characters appear. Is there something that you guys see that I'm 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 is that it prints AZ as I want, but also prints a bunch of random characters after Z. Any ideas? Since I'm new to this, I'm sure this is probably something simple. Thanks in advance!

+4
source share
5 answers

To remove a bunch of random characters and characters, add a NUL to the end of the 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 will print the alphabet 26 times, which may not be what you want. Edit: invalid previous statement - I missed your break; .

+1
source

When you pass an array of characters to the output stream, the array should have a zero end. Your array is not null terminated; why do you get excess garbage at the end.

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

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

Finally, with 26 letters, the indices go from zero to 25, so the condition for the continuation of the cycle should be i < 26 , i != 26 or i <= 25 .

+4
source

Add zero to the end of the array. c checks for null termination.

+1
source

The array index starts from 0 to N , you have 26 characters in the 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; usually used to interrupt a for or while under certain conditions. You for loop finishes smoothly, you don't need a break` in a loop.

0
source

char arrays in C ++ have a zero end and start with an index based on zero, so when you enter an 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 didn’t take into account that you need 1 more place for '\ 0' to complete the array, which causes undefined behavior

Since you print the letters [0] on the letters [26], you again cause undefined behavior when writing the letters [26], hence the appearance of garbage values

0
source

All Articles