扩展我的阵列
Extending my array
根据我从书中读到的内容以及我从书中所做的先前的例子来看,这就是我想出的。 我很感激额外的建议,但我正在尝试学习本章试图向我展示的内容,以便我可以继续学习基础知识,然后再尝试我以前从未见过的代码。我希望用户键入 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
分配给变量 keepgoing
。 keepgoing
值的第二次检查是 true
。 这是一个非常普遍的问题,许多新程序员(偶尔是老程序员(都会出差。 :-)
相关文章:
- 当我的阵列太大时出现分段错误
- 我想反转我的阵列.为什么这段代码给出垃圾值?
- 当我的阵列达到一定大小时,Qt 不起作用
- 无法弄清楚为什么我的打印阵列要替换元素
- 我需要将阵列样式的邻接矩阵转换为矢量样式(以使其看起来更好)
- 我的 2D 阵列无法读取文本文件并正确输出
- 为什么我的 2D 阵列不旋转?
- 我如何将整数转换为存储在字符阵列中的十六进制字节
- 我如何使我的循环转到char*阵列的尽头,并将每个城市都放在链接的列表中(城市被划分为白色的空间)
- 打印我的字符串阵列时,会遇到细分错误
- 我的OpenGL顶点阵列将其抽象成课程不再起作用
- 如何找到我的阵列使用的内存
- 扩展我的阵列
- 我的 2D 阵列已关闭,需要左下角为 (0,0)
- 如何在我的指针和阵列功能中修复未解决的外部错误-LNK2019和LNK1120
- MEMSET FUCTION在我的C 动态阵列初始化中不起作用
- 为什么我的阵列没有更新
- 为什么当我到达我的 2D 阵列时它会崩溃?out_of_range
- 将我的阵列移动到 Mat 并使用打开的 CV 显示图像
- 我在我的阵列中在哪里