thirty_0 2021-01-29 10:39 采纳率: 100%
浏览 41
已采纳

为什么在字符指针开辟空间后会有空格

我直接上源代码吧,在stringguess函数里面,开辟空间之后第一个是有空格存在,第二个是在开辟四个空间之后,后面就有乱码。

#include<iostream>
#include<stdlib.h>
#include<ctime>
#include<windows.h>
#include<string>
#include<fstream>
using namespace std;
class guessGame
{
    int Diffcluty,i;
    char* systemChar=0, * userChar=0;
    string userName;
public:
    guessGame(int n) :Diffcluty(n),i(1) {};
    char Random(int Difficulty);/*生成随机数*/
    void func();
    void GameInterface();/*游戏界面*/
    void StringGuess();
    void RankingList();/*游戏排行榜*/
    ~guessGame()
    {
        if (systemChar != NULL)
        {
            delete[]systemChar;
            systemChar = NULL;
        }
        if (userChar != NULL)
        {
            delete[]userChar;
            userChar = NULL;
        }
    }
};
char guessGame::Random(int Difficulty)
{
    
    char randomInt, randomChar, randomCharSup, randomAll;
    randomInt = rand() % 9 + 48;
    randomChar = rand() % 25 + 65;
    randomCharSup = rand() % 25 + 97;
    randomAll = rand() % 93 + 33;
    switch (Difficulty)
    {
    case 1:/*纯数字*/
    {
        return randomInt;
        break;
    }
    case 2:/*数字字母组合*/
    {
        int i = rand() % 2 + 1;
        if (i == 1)
        {
            return randomInt;
        }

        else if (i == 2)
        {
            return randomChar;
        }

        else
        {
            return randomCharSup;
        }
        break;
    }
    case 3:/*全键盘字符*/
    {
        return randomAll;
        break;
    }
    }

}
void guessGame::func()
{
    GameInterface();
    StringGuess();
    RankingList();
}
void guessGame::GameInterface()
{
    getchar();
    cout << "游戏难度选择已完成!\t按下回车键开始游戏";
    getchar();
    cout << "游戏开始倒计时3秒!\n";
    for (int i = 0;i < 3;i++)
    {
        /*Sleep(1000);*/
        cout << 3 - i << '\n';
    }
    cout << "游戏开始!";
    system("cls");
}
void guessGame::StringGuess()
{
    while (1)
    {
        cout << "第" << i << "轮的字符是:\n";
        systemChar = new char[i];
        userChar = new char[i];
        for (int n = 0;n < i;n++)
        {
            systemChar[n]=Random(Diffcluty);
            cout << systemChar[n];
        }
        Sleep(1000);
        system("cls");
        cout << "请输入你记住的字符";
        for (int n = 0;n < i;n++)
        {
            cin >> userChar[n];
        }
        if (!strcmp(systemChar, userChar))
        {
            getchar();
            cout << "恭喜您猜对了^_^!\n输入回车键进行下一轮";
            delete[]systemChar;
            systemChar = 0;
            delete[] userChar;
            userChar = 0;
            i++;
            getchar();
            system("cls");
        }
        else
        {
            cout << "猜错了×_×!正确答案是:" << systemChar;
            cout << "\n您一共进行了" << i<< "轮\n请输入你的名字:";
            cin >> userName;
            break;
        }
    }
}
void GameOperation() /*游戏说明书*/
{
    system("cls");
    cout << "--------------------游戏说明--------------------\n";
    cout << "玩法:程序会随机显示字符,在规定时间后消失,凭记忆输入这些字符\n";
    cout<<"在每次正确输入后,字符数会增加一个,直到用户输入错误(只要有一个字符不对就算错),这一轮游戏结束\n";
    cout << "本程序具有记录用户成绩排行榜功能。\n";
    cout << "\t简单模式:由随机的纯数字组成\n";
    cout << "\t中等模式:由随机的数字与大小写字母组合而成\n";
    cout << "\t困难模式:由随机的全键盘字符组合而成\n";
    cout << "\t重新查看游戏说明请按0\t简单难度输入1\t中等难度输入2\t困难难度输入3\n请选择:";
}
int main()
{
    //问题一:内存分配时无效
    //问题二:文件读取被覆盖
    srand(time(NULL));  //随机数放到主函数中防止产生重复随机数
    int Diffculty;
    cout << "\t查看游戏说明请按0\t简单难度输入1\t中等难度输入2\t困难难度输入3\n请选择:";
    cin >> Diffculty;
        while (1)
        {
            if (Diffculty == 0)
                GameOperation();
            else if (Diffculty < 0 || Diffculty>3)
            {
                cout << "输入错误,请重新输入!";
            }
            else break;
            cin >> Diffculty;;
    }
    guessGame sSs(Diffculty);//适当调整时间 当每超过2个字符时增加1秒
    sSs.func();
}

  • 写回答

1条回答 默认 最新

  • include_iostream_ 2021-01-29 11:40
    关注

    注意到guessGame::StringGuess的随机串生成逻辑。new得到的字符串**并不是**自动清零的,分配得到的空间自然会存在乱码,而且你的程序有一个致命错误:第i轮有i个字符,但你new得到的数组也只有i长度,没有null字符的位置(记得吗?C语言基础:要存储长度为x的字符串,必须分配长度为x+1的char数组),你分配的长度是不足的。你也没有手动添加null字符结尾,所以strcmp在比较时,会因为遇到不是null的字符而认定字符串没有结束,但输入调用是会自动添加null字符的。两个字符串连长度都不同,自然判定为不同。

    总之结论就是:new的时候不能new char[i]而是new char[i+1],而且在guessGame::StringGuess的for循环随机生成逻辑后,需要手动加一个null字符。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探