提示——选择存储时要始终如一。如果您使用 a std::string
for the answer
(不是显示的错误数组std::string
),那么也使用 a std::string
for input
。但是,如果您必须使用 POA(普通旧数组),那对于教育目的来说很好——您只是缺少所有自动内存管理和边界保护。
您将std::basic_string初始化为:
std::string answer { "DCDDA" }; /* initialize string (not array of strings) */
(注意:std::
命名空间标识符的使用。请参阅为什么“使用命名空间 std;”被认为是不好的做法?)
不要在代码中使用幻数。5
你坚持你的数组声明和你的循环限制是一个Magic Number。反而,
#define NCHAR 5 /* if you need a constant, #define one (or more) */
...
char input[NCHAR]; /* array of 5 char (why not string?) */
...
for (int i = 0; i < NCHAR; i++) { /* loop taking input */
当您循环获取输入时,请验证每个输入。用户很可能会生成手册EOF
以取消使用Ctrl+d(或Ctrl+z在 Windows 上)的输入。仅在验证输入后增加存储的字符数,例如
int counter = 0, points = 0; /* counter and points, initialized zero */
std::cout << "Welcome and Good luck!\n\n";
for (int i = 0; i < NCHAR; i++) { /* loop taking input */
std::cout << " " << i << ". Choose from letters A-D: ";
if (std::cin >> input[i]) /* validate input */
counter++; /* only increment on successful input */
}
'\n'
对于您的输出,请了解使用和之间的区别std::endl
,C++:“std::endl”与“\n”</a>。
当您将项目存储在容器中时(例如std::string
),您可以使用基于范围的 for 循环(自 C++11 起)来循环容器中的每个项目。要遍历存储的输入,您必须将循环限制为成功读取为输入的元素数。使用与counter
您的比较限制相同的值,例如
for (auto& item : answer) /* range based for-loop over chars in string */
for (int i = 0; i < counter; i++) /* loop over counter chars in array */
if (item == input[i]) /* if the item matches the input char */
points += 1; /* add point */
总而言之,您将拥有:
#include <iostream>
#include <string>
#define NCHAR 5 /* if you need a constant, #define one (or more) */
int main()
{
std::string answer { "DCDDA" }; /* initialize string (not array of strings) */
char input[NCHAR]; /* array of 5 char (why not string?) */
int counter = 0, points = 0; /* counter and points, initialized zero */
std::cout << "Welcome and Good luck!\n\n";
for (int i = 0; i < NCHAR; i++) { /* loop taking input */
std::cout << " " << i << ". Choose from letters A-D: ";
if (std::cin >> input[i]) /* validate input */
counter++; /* only increment on successful input */
}
std::cout << "\nYou entered:\n"; /* output stored values */
for (int i = 0; i < counter; i++) /* looping only 0 to counter */
std::cout << " input[" << i << "] : " << input[i] << '\n';
for (auto& item : answer) /* range based for-loop over chars in string */
for (int i = 0; i < counter; i++) /* loop over counter chars in array */
if (item == input[i]) /* if the item matches the input char */
points += 1; /* add point */
std::cout << "\nTotal points: " << points << '\n'; /* output total points */
}
示例使用/输出
$ ./bin/points
Welcome and Good luck!
0. Choose from letters A-D: A
1. Choose from letters A-D: B
2. Choose from letters A-D: C
3. Choose from letters A-D: D
4. Choose from letters A-D: E
You entered:
input[0] : A
input[1] : B
input[2] : C
input[3] : D
input[4] : E
Total points: 5
如果所有字符都匹配,则检查最大点数:
$ ./bin/points
Welcome and Good luck!
0. Choose from letters A-D: D
1. Choose from letters A-D: C
2. Choose from letters A-D: D
3. Choose from letters A-D: D
4. Choose from letters A-D: A
You entered:
input[0] : D
input[1] : C
input[2] : D
input[3] : D
input[4] : A
Total points: 11
看看事情,如果你有问题,请告诉我。