扩展我的阵列

Extending my array

本文关键字:阵列 我的 扩展      更新时间:2023-10-16

根据我从书中读到的内容以及我从书中所做的先前的例子来看,这就是我想出的。 我很感激额外的建议,但我正在尝试学习本章试图向我展示的内容,以便我可以继续学习基础知识,然后再尝试我以前从未见过的代码。我希望用户键入 0 以结束循环,但由于某种原因循环继续? 我想我可能错过了一些阻止它停止的东西。

// Ex4_08.cpp
// Initializing pointers with strings
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
    bool keepgoing = true;
    int answer;
    while (keepgoing = true)
    {
        const char* pstr[]{ "Aa",                                       // Initializing a pointer array
            "Bb",
            "Cc",
            "Dd",
            "Ee",
            "Ff",
            "Gg",
            "Hh",
            "Ii",
            "Jj",
            "Kk",
            "Ll",
            "Mm",
            "Oo",
            "Pp",
            "Qq",
            "Rr",
            "Ss",
            "Tt",
            "Uu",
            "Vv",
            "Ww",
            "Ss",
            "Yy",
            "Zz",
        };
        const char* pstart{ "Your letter is " };
        int dice{};
        cout << endl
            << "Enter a number between 1 and 26 " << _countof(pstr) << ": ";
        cin >> dice;
        cout << endl;
        if (dice >= 1 && dice <= _countof(pstr))                            // Check input validity
            cout << pstart << pstr[dice - 1];                               // Output star name
        else
            cout << "Sorry, you haven't selected a correct number.";        // Invalid input

        cout << "Do you want to do this again? Type 0 for no: " << endl;
        cin >> answer;
        if (answer == 0)
        {
            keepgoing = false;
        }
    }

    cout << endl;
    return 0;
}

我已经修改了您的初始代码示例,使用更C++且更易于使用的向量和字符串:

#include <iostream>
#include <string> // for string
#include <vector>  // for vector
using std::cin;
using std::cout;
using std::endl;
int main()
{
    std::vector<std::string> pstr;
    for (char c = 'a'; c <= 'z'; c++)  // cycle over the 26 ASCII letters
    {
        std::string temp;     // 
        temp += c - 32;       //  add capital character (e.g A)
        temp += c;            //  add character (e.g. a)
        pstr.push_back(temp); // push the new string (e.g. Aa) to the vector 
    }
    const char* pstart{ "Your letter is " };
    int dice{};
    while (true)
    {
        cout << endl
            << "Enter a number between 1 and 26 " << pstr.size() << ": ";
        cin >> dice;
        if (dice == 0)
        {
            break; //break if the user enters 0
        }
        cout << endl;
        if (dice >= 1 && dice <= pstr.size())                            // Check input validity
            cout << pstart << pstr[dice - 1];                               // Output star name
        else
            cout << "Sorry, you haven't selected a correct number.";                    // Invalid input
    }
    cout << endl;
    return 0;
}

你的代码在这里是错误的:

while (keepgoing = true) {
  ...
  if (answer == 0) {
    keepgoing = false;
  }
}

您将keepgoing设置为 false,但在while条件下,您讨厌它以true .您必须使用 == 运算符(如 while(keepgoing == true) 中所示(或删除它(while(keepgoing) (。

否则,您可以使用while(true)for (;;)和中断而不是keepgoing = false

for (;;) { // or while (true); to me, that's only a matter of preference.
  ...
  if (answer == 0) {
    break; // exit the loop
  }
}

它们确实会产生无限循环,直到您进入break条件。

更改此行:

while (keepgoing = true)

对此:

while (keepgoing == true)

第一个将值true分配给变量 keepgoingkeepgoing 值的第二次检查是 true 。 这是一个非常普遍的问题,许多新程序员(偶尔是老程序员(都会出差。 :-)