I have this assignment due this weekend and I was able to figure it out but for some reason the last part of my output is going up to 38 and I have no clue to way it would be doing that any thoughts to way.

my output:
output:

PRINTING CONTENTS OF ARRAY

ABCDEFGHIJKLMNOPQRSTUVWXYZ

This is the title to your Program related to the alphabet.

Select the number that coincides with the alphabet.

For example, the number 7 should display the letter G.

Enter a number between 1 and 26: 2

The number you selected: 2
The letter related to this number: B

PRINTING CONTENTS OF ARRAY and adding x to every other element

AxCxExGxIxKxMxOxQxSxUxWxYx x

PRINTING CONTENTS OF ARRAY USING THE MOD Option

The even numbered element = 0 Contents of element within array = A
The even numbered element = 2 Contents of element within array = C
The even numbered element = 4 Contents of element within array = E
The even numbered element = 6 Contents of element within array = G
The even numbered element = 8 Contents of element within array = I
The even numbered element = 10 Contents of element within array = K
The even numbered element = 12 Contents of element within array = M
The even numbered element = 14 Contents of element within array = O
The even numbered element = 16 Contents of element within array = Q
The even numbered element = 18 Contents of element within array = S
The even numbered element = 20 Contents of element within array = U
The even numbered element = 22 Contents of element within array = W
The even numbered element = 24 Contents of element within array = Y
The even numbered element = 26 Contents of element within array =
The even numbered element = 28 Contents of element within array = /
The even numbered element = 30 Contents of element within array = /
The even numbered element = 32 Contents of element within array = !
The even numbered element = 34 Contents of element within array = b
The even numbered element = 36 Contents of element within array = D
The even numbered element = 38 Contents of element within array = /
Press any key to continue . . .

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main()
{

    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' } ;

    cout << "PRINTING CONTENTS OF ARRAY" << endl;
    cout << "==================================" << endl;

    for (int i = 0; i <= 0; ++i)
    {
    cout << letters << " ";
    }

    cout << endl;
    cout << endl;

    cout << "This is the title to your Program related to the alphabet." << endl;
    cout << endl;
    cout << "Select the number that coincides with the alphabet." << endl;
    cout << endl;
    cout << "For example, the number 7 should display the letter G." << endl;
    cout << endl;
    cout << "Enter a number between 1 and 26: ";

    int number;

    cin >> number;
    cout << endl;
    cout << "The number you selected: " << number << endl;

    if ((number > 0) && (number <= sizeof(letters)))
    {
        cout << "The letter related to this number: " << letters[number-1] << endl;
    }

        else
        {

            cout << "Sorry, you must choose a number between 1 and 26." << endl;
            cout <<endl;
        }

    cout << "PRINTING CONTENTS OF ARRAY and adding x to every other element" << endl;
    cout << "===============================================================" << endl;

    int j=0;

    for (int i = 1; i < 2; i+=2)
    {
        letters[i] = 'x';

        for(int j = 0; j < 26; j+=2)

        cout << letters[j] << letters [i];
        cout << endl;
    }

        cout <<endl;
        cout << "PRINTING CONTENTS OF ARRAY USING THE MOD Option" << endl;
        cout << "=====================================================" <<endl;

    for(int i=0; letters[i] !='\0' ; i++)  
    {

        if ((letters[i] & 1) == 0)           

              i=i+1;

             cout <<endl;
             cout <<"The even numbered element = "<<i<<"    Contents of element within array = "<<letters[i];
             cout <<endl;
    }

system ("pause");

return 0;

}

Recommended Answers

All 2 Replies

Since you're incrementing by two, you're basically skipping the final \0. Look at when you get to the 'Y'. You print that out, and then you go two more, which puts you one past the \0. So your test (letters[i] != '\0') never sees the \0. It sees the one before it, and whatever is in memory after it, and it eventually stops when it happens to see another \0 in memory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.