gpt4 book ai didi

C++ 数组显示字母和数字?

转载 作者:行者123 更新时间:2023-11-30 02:36:40 24 4
gpt4 key购买 nike

我正在开发一个彩票应用程序,其中计算机生成 5 个随机数并将它们存储到下标中。出于某种原因,我的数组在我的代码中输出字母和数字。谁能告诉我为什么会这样?

#include <iostream>
#include <iomanip>
#include <random>
#include <ctime>
#include <time.h>

using namespace std;

int main(){
const int digits = 5;
int winningDigits[digits];



srand(time(0));

winningDigits[0] = rand() % 10 + 1;
winningDigits[1] = rand() % 10 + 1;
winningDigits[2] = rand() % 10 + 1;
winningDigits[3] = rand() % 10 + 1;
winningDigits[4] = rand() % 10 + 1;

cout << winningDigits;

最佳答案

cout << winningDigits;

正在打印数组的地址而不是数组的内容。您看到字母和数字的原因是地址显示在 hexidecimal 中。

您可以使用 ranged based for loop 打印出您的数组喜欢:

for (const auto& e : winningDigits)
cout << e << " ";

关于C++ 数组显示字母和数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32465618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com