繁体   English   中英

控制台输出中奇怪的非ASCII字符

[英]Weird non-ASCII Characters in Console Output

下面是一个例子,其中几个例子似乎是在前一天晚上随机出现的,导致在控制台中显示了奇怪的ascii字符。 例如,player.level显示一个笑脸ascii字符。 有些只显示几个空格,另一些只显示单个奇怪的ascii字符。

图片示例:

该代码工作正常,我遇到了一些已解决的编译器问题。 大约在同一时间,我将很多“ int”数据类型更改为uint8_t等。修复了编译器问题后,我开始看到这个新问题。

语言:C ++(11)(CodeBlocks IDE)

//randstats.cpp

#include "encounter.h"
#include "player.h"
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <iostream>
#include "createchar.h"
#include "randstats.h"

boost::random::mt19937 gen;

player_vars randStats()
{
    /*
        Randomize new characters stats if the player chooses to do so rather than manually assign them.
        Also going to use this to test against random encounters for balance purposes.
    */

    player.str = 8;
    player.dex = 8;
    player.con = 8;
    player.intel = 8;
    player.wis = 8;
    player.cha = 8;
    uint8_t remaining_points = 25; // starting pts to hand out freely
    uint8_t total_possible = player.str + player.dex + player.con + player.intel + player.wis + player.cha + remaining_points; // max points to assign

    /*
        Basically the whole purpose of this is to pick random numbers and spread them out among random stats.
        The stats will not be higher than total_possible + remaining points (assigned above).
    */

    while (remaining_points > 0)
    {

        boost::random::uniform_int_distribution<> dist13(1, 3);
        uint8_t random_number = dist13(gen);

        uint8_t x = 0;
        // Get current or total points currently assigned
        uint8_t totalpts = player.str + player.dex + player.con + player.intel + player.wis + player.cha;

        //if we're in range and won't go over
        if ((totalpts < total_possible) && ((random_number + totalpts) < total_possible))
        {
            x = random_number;
            // remaining_points - x;
        }
        //if we're going to go over, only assign the number of points from random that will stop us at 73 points
        else if ((random_number + totalpts) > total_possible)
        {
            // Since we're going over the max total points, how many do we assign to a stat?
            uint8_t y = totalpts - total_possible;
            x = abs(y);
            remaining_points = 0; //force this because we exceeded
        }

        //random numbering choosing which stat the points go to
        boost::random::uniform_int_distribution<> dist16(1, 6);

        switch(dist16(gen))
        {
        case 1 :
            player.str += x;
            break;
        case 2 :
            player.dex += x;
            break;
        case 3 :
            player.con += x;
            break;
        case 4 :
            player.intel += x;
            break;
        case 5 :
            player.wis += x;
            break;
        case 6 :
            player.cha += x;
            break;
        default :
            break;
        }

    }

    //print the new results
    std::cout << "\nNew Character Stats: \n";
    std::cout << "Strength: [" << player.str << "]" << std::endl;
    std::cout << "Dexterity: [" << player.dex << "]" << std::endl;
    std::cout << "Constitution: [" << player.con << "]" << std::endl;
    std::cout << "Intelligence: [" << player.intel << "]" << std::endl;
    std::cout << "Wisdom: [" << player.wis << "]" << std::endl;
    std::cout << "Charisma: [" << player.cha << "]" << std::endl;
    std::cout << "Total Points Assigned: " << player.str + player.dex + player.con + player.intel + player.wis + player.cha << std::endl;

    std::cout << "Accept these results? [y/n]: ";
    char selection;
    std::cin >> selection;

    switch(selection)
    {
    case 'Y' :
    case 'y' :
        createPlayer();
        break;
    case 'N' :
    case 'n' :
        randStats();
        break;
    default:
        std::cout << "\nInvalid response. Keeping results.\n";
        createPlayer();
    }

    return player;
}//close function


   //player.h
   #ifndef PLAYER_H_INCLUDED
   #define PLAYER_H_INCLUDED

   #include <string>
   #include <cstdint>

   struct player_vars {
    std::string alias;
    uint8_t current_level;
    bool is_alive; // alive or dead
    uint8_t str;
    uint8_t dex;
    uint8_t con;
    uint8_t intel;
    uint8_t wis;
    uint8_t cha;
    uint32_t gold;
    std::string profileName; // may use
    int16_t hitpoints; // current HP dependent on class, level, feats, con, etc.
    uint8_t size_id; // character size
    uint8_t class_id; // check comments
    uint8_t base_attack_bonus; // dependent on player's class
    uint8_t weapon_id; // check comments

    uint8_t getHitPoints();

    }; extern player_vars player;

uint8_t通常typedef倒是到unsigned char 这在C语言中效果很好。

C ++的流运算符正在查看您的unsigned char值,并试图将它们打印为字符,而不是将整数转换为字符串。 因此,您所看到的是特定终端/控制台决定为该字符代码显示的内容。

省去麻烦,只需要坚持int 我们只说了4个字节而不是1个字节,也不必担心255的微小限制。

如果您确实想将数据存储为uint_8 ,则在将数据流传输到cout之前,需要将其转换为int

std::cout << "Strength: [" << (int)player.str << "]" << std::endl;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM