C++中的指针和数组也改变元素

C++中的指针和数组也改变元素,c++,arrays,pointers,C++,Arrays,Pointers,我正试图为大学编写一个程序: 首先,输出一个字母表数组,我得到了 然后,它应该提示用户选择一个与数组中字母相关的数字,虽然它实际上选择了索引数字 接下来,我尝试编写一个输出代码,该输出将输出字母表,每一个字母都是“x”,即A x C x e x G x I x………..,但我得到的是x x x x x 最后,该程序应提示用户选择字母作为C、E、G、I。。。。。。。。或B D F H J。。。。。。。。。。。。;当我执行时,它似乎是计算机语言的无限循环 感谢您的帮助。这就是我所拥有的: #incl

我正试图为大学编写一个程序:

首先,输出一个字母表数组,我得到了

然后,它应该提示用户选择一个与数组中字母相关的数字,虽然它实际上选择了索引数字

接下来,我尝试编写一个输出代码,该输出将输出字母表,每一个字母都是“x”,即A x C x e x G x I x………..,但我得到的是x x x x x

最后,该程序应提示用户选择字母作为C、E、G、I。。。。。。。。或B D F H J。。。。。。。。。。。。;当我执行时,它似乎是计算机语言的无限循环

感谢您的帮助。这就是我所拥有的:

#include <iostream> 
#include <cstdio>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;

int i = 0;
const int size = 27;
char newline = '\n';
char *pnumber = 0;
int selection = 0;

int main()
{
    cout << "PRINGING CONTENTS OF ARRAY" << endl;
    cout << "====================================================" << endl;
    char alphabet[size] = { '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 };

    for (int column = 0; column < size; column++) {
        cout << alphabet[column] << " ";
    }
    {
        cout << newline;
        cout << newline;

        cout << "This is the title to your Program related to the alphabet."
            << endl;
        cout << endl;
        cout << "Select the index that coincides with the alphabet."
            << endl;
        cout << "For example, the number 6 should display the letter G"
            << endl;
        cout << endl;
        cout << "Enter a number between 0 and 25: ";
        cin >> i;
        cout << "The number you selected: " << i;
        cout << newline;
        cout << "The letter related to this number: " << alphabet[i];
        cout << endl; }
        {
            cout << newline;
            cout << newline;
            cout << "PRINTING CONTENTS OF ARRAY and adding x to every other element"
            char alphabet[size] = { '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 };
            char* pnumber(nullptr);
            pnumber = &alphabet[1];
            for (int i = 0; i < sizeof(alphabet); ++i) {
                *(pnumber + i) = 'x';
            }

            cout << alphabet[i] << " ";
        }
    }

    {
        cout << newline;
        cout << newline;
        cout << "PRINTING CONTENTS OF ARRAY USING THE MOD Option" << endl;
        cout << "=========================================================
        char alphabet[size] = { '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 << "Do you want the letters of the alphabet starting from index
            0, A, or index 1, B: ";
        cin >> selection;
        if (selection = 0)
            for (int i = 0; i < size; i + 2) {
                cout << alphabet[27] << " ";
            }
        else
            for (int i = 1; i < size; i + 2) {
                cout << alphabet[27] << " ";
            }
        return 0;
    }
} //End of Int Main

我不太确定从哪里开始。但在这里,这是自上而下运行的,正如我从最初的帖子中所看到的那样。

这个例子甚至没有编译你重新定义了两次字母表??为什么你希望代码的第二部分打印一个x C x E x G x…?为什么你到处输出字母表[27]?太完美了,谢谢你。我碰巧从一个朋友那里学到了模数,但在课堂上还没有学会。有没有办法让用户在第二部分中选择1和26,而不是0和25?所以字母在字母表中的实际位置而不是索引中对齐?我知道如何做我想做的事,这是一个无知的问题。但现在我遇到的问题是:cin>>选择;如果i=0时选择=0;i<尺寸;i+=2{如果这是最后一部分,它会继续输出bdfhj………但不会以AI开始。可以让程序打印或不打印,但不能通过if语句打印正确的输出。
#include <iostream> 
#include <cstdio>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;

int main()
{

int i = 0;
const int size = 27;
char newline = '\n';
char *pnumber = 0;
int selection = 0;


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

char alphabet[size] = { '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 };

for (i = 0; i < size; i++) 
    {
        cout << alphabet[i] << " ";
    }

cout << newline;
cout << newline;

cout << "This is the title to your Program related to the alphabet." << endl;
cout << endl;

cout << "Select the index that coincides with the alphabet." << endl;
cout << "For example, the number 6 should display the letter G" << endl;
cout << endl;

cout << "Enter an index between 0 and 25: ";
cin >> i;
cout << "The number you selected: " << i;

cout << newline;
cout << "The letter at this index: " << alphabet[i];
cout << endl;

cout << newline;
cout << newline;

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

pnumber = &alphabet[1];

for (i = 0; i < size; i += 1)
{
    if (i % 2 == 0)
    {
        cout << alphabet[i] << " ";
    }
    else
    {
        cout << "x ";
    }
}

cout << newline;
cout << newline;

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

cout << "Do you want the letters of the alphabet starting from index 0, A, or index 1, B: ";
cin >> selection;

if (selection = 0)
    for (i = 0; i < size; i += 2) 
    {
        cout << alphabet[i] << " ";
    }
else
    for (i = 1; i < size; i += 2) 
    {
        cout << alphabet[i] << " ";
    }

cout << endl;
cout << endl;
system("pause");
} //End of Int Main