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

时间:2015-12-23 01:40:06

标签: c++ arrays pointers

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

  1. 首先,输出一个字母数组,我知道了;

  2. 然后,它应该提示用户选择与数组中字母表字母相关的数字,虽然它实际上选择了索引号但我得到了这个数字;

  3. 接下来,我正在尝试编写一个输出,输出字母表,其他所有字母都是' x',即A x C x E x G x I x..........,但是我得到的是A x x x x x x x x x...... {{ 1}}

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

  5. 感谢任何帮助。这就是我所拥有的:

    #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
    

1 个答案:

答案 0 :(得分:0)

#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

我不确定从哪里开始。但是在这里,从原始帖子中我可以看出,这是自上而下的预期行为。