提问者:小点点

C++输出奇怪字符而不是数字(Windows)


我需要为我的教育编程一个乐透生成器,它将随机滚动号码,检查重复的条目,否则替换它们。 当我启动程序时,没有错误信息,程序运行,但我只看到奇怪的字符,而不是数字。 问题的一览表

我的代码有什么问题?

#include <iostream>
#include <array>
#include <time.h>

std::array<unsigned char, 6> lottoZahlen = {0, 0, 0, 0, 0, 0};

void arrayFuellen();
unsigned char checkDuplikate(unsigned char);
void arraySortieren();

int main()
{
    arrayFuellen();
    arraySortieren();

    std::cout << "\n---- Ihre Glueckszahlen lauten: ----" << std::endl;

    for (unsigned char lottoGlueck : lottoZahlen)
    {
        std::cout << lottoGlueck << std::endl;
    }

    std::cout << "---- Glueckszahlen Ende ----" << std::endl;
}


void arrayFuellen()
{
    srand(time(NULL));

    unsigned char wuerfelZahl = 0;
    unsigned char wuerfelZahlChecked = 0;

    for (unsigned char i = 0; i < sizeof(lottoZahlen); i++)
    {
        wuerfelZahl = rand() % 45 + 1;
        wuerfelZahlChecked = checkDuplikate(wuerfelZahl);
        lottoZahlen[i] = wuerfelZahlChecked;
    }
}

unsigned char checkDuplikate(unsigned char checkZahl)
{
    srand(time(NULL));
    bool dublette = false;

    do
    {
        dublette = false;

        for (unsigned char j = 0; j < sizeof(lottoZahlen); j++)
        {
            if (checkZahl == lottoZahlen[j])
            {
                checkZahl = rand() % 45 + 1;
                dublette = true;
            }
        }
    } while (dublette);

    return checkZahl;
}

void arraySortieren()
{
    unsigned char merker = 0;
    bool vertauscht = false;

    do
    {
        vertauscht = false;

        for (unsigned char i = 1; i < sizeof(lottoZahlen); i++)
        {
            if (lottoZahlen[i - 1] > lottoZahlen[i])
            {
                merker = lottoZahlen[i];
                lottoZahlen[i] = lottoZahlen[i - 1];
                lottoZahlen[i - 1] = merker;
                vertauscht = true;
            }
        }
    } while (vertauscht);
}

共3个答案

匿名用户

有几种方法可以执行您想要的操作,将char打印为整数/十进制值:

>

  • 使用casgingint():

    std::cout << int(lottoGlueck) << "\n";
    

    使用旧的(C样式)printf(),有些人会说不要使用这个,但是使用printf()有优点也有缺点。

    printf("%d\n", lottoGlueck);
    

    正如建议的那样,您可以使用std::to_string(),我个人并不建议将此用于打印单个字符,因为它将字符转换为字符串以打印出整数。

    在生产代码中,我使用数字1,在调试中,我使用数字2。 使用这两种方法都有缺点/优点,但您可以阅读本文以更好地理解这些缺点/优点。

    当涉及到ping作为十进制值的字符串时,您有std::to_string()std::cout<<; std::dec<<; 字符串<<; “\n”

  • 匿名用户

    “char”是一种用于存储字符的类型,输出流将在for循环中将其解释为这样的类型。 所以如果你有值65,它实际上会显示为大写a(它有ASCII值65)。 若要显示数字,应使用输出流识别为数字的类型,如“int”。

    匿名用户

    您正在打印不可打印的字符:https://upload.wikimedia.org/wikipedia/commons/d/dd/ascii-table.svg[]之间的字符不可打印。

    如果您写:int i=5,然后写std::cout<<<; i

    它将打印相应的字符,值为5。 但值5不是字符“5”,因此如果您希望它是一个可打印的数字,则需要将其转换为:std::cout<<<; std::to_string(i)

    (不确定这是否是您的意图:))

    相关问题


    MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++|输出|奇怪|字符|数字|windows)' ORDER BY qid DESC LIMIT 20
    MySQL Error : Got error 'repetition-operator operand invalid' from regexp
    MySQL Errno : 1139
    Message : Got error 'repetition-operator operand invalid' from regexp
    Need Help?