C++ outputs strange characters instead of numbers (Windows)

Viewed 639

I need to program a lotto generator for my education that will randomly roll numbers and check for duplicate entries and replace them otherwise. When I start the program there are no error messages and the program runs but I only see strange characters instead of numbers. A picture of the problem

What is wrong with my code?

#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);
}
4 Answers

"char" is a type that is used to store characters, and the output stream will interpret it as such in your for-loop. So if you have value 65, it will actually be displayed as a capital A (which has ASCII value 65). To display numbers, you should use a type that the output stream recognizes as a number, such as "int".

There are several ways of doing what you want, printing char as integer/decimal value:

  1. using casging int():

    std::cout << int(lottoGlueck) << "\n";
    
  2. using good old (C style) printf(), some would say do not use this, but there are advantages and disadvantages to using printf().

    printf("%d\n", lottoGlueck);
    
  3. As suggested, you can use std::to_string(), I personally do not recommend this for printing a single character, simply because it converts a character to a string to print out an integer.

In production code I use number 1, in debugging I use 2. There are disadvantages/advantages to using both, but you can read this to better understand those.

When it comes to pinging strings as decimal values, you have std::to_string() and also std::cout << std::dec << string << "\n".

you are printing non printable characters: https://upload.wikimedia.org/wikipedia/commons/d/dd/ASCII-Table.svg the ones between [] are not printable characters.

if you write: int i = 5 and then std::cout << i

it will print the corresponding character, with value 5. But the value 5 is not the character '5', so if you expect it to be a printable number, you need to convert it: std::cout << std::to_string(i)

(not sure if this was your intention though :) )

In addition to the answers to your question, you can check whether your value is printable or not by using isprint().

std::cout << isprint(lottoGlueck) << std::endl;

This will print 0 (false) if your value is non-printable.

Related