输出在我的字符数组范围之外,同时使用 rand()

Output outside my char array range, while using rand()

本文关键字:rand 我的 字符 数组 范围 输出      更新时间:2023-10-16

我正在做一个记忆游戏(到目前为止进展不佳),我遇到了这个问题,我的随机函数或我在new_game函数中的循环似乎很混乱。我是 c++ 的初学者,所以任何帮助将不胜感激。我知道代码有点杂乱无章和笨拙,更不用说没有优化了。对这一切都是新的。

#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <fstream.h>
#include <conio.h>
#include <ctype.h>
void main_menu();
////////////NECESSARY VARIABLES////////////
int score=0;
int highscore;
//////////NECESSARY FILE HANDLERS//////////
fstream filehandlerHS;
//^^^HIGH SCORE STATS^^^//
//////////FUNCTION TO CREATE RANDOM STRING//////////
char vocab[]={0,1,2,3,4,5,6,7,8,9};////POSSIBLE CHARACTERS
char random()
{
    return vocab[rand() %10];
}
///////////////NEW GAME/////////////
void New_Game()
{
    cout<<"......Welcome To The Memory Game!.......";
    cout<<"A series of random characters will be shown to youn"
        <<"For a specific period of time, you have to remember itn"
        <<"And once the text dissapears, enter it, correct answers gives 5        pointsn"
        <<"Wrong answer means game over! ready? n";
    system("Pause");
    system("cls");
    for(int i=0; i<10;i++)
    {
        char entry[9];
        char term[9];
        for(int j=0; j<i;j++)
        {
            term[j]=random();   
        }
        cout<<term;
        system("pause");
        system("cls");
        cin>>entry;
        if(entry==term)
        {
            score+=5;
        }
        if(entry!=term)
        {
            cout<<"Ooops! Wrong Guess!!! GAME OVER!!!";
            if(score>highscore)
            {
                cout<<"High Score! your record has been saved!";
                filehandlerHS.close();
                filehandlerHS.open("C:/Users/bhati/Documents/C-Free/Projects/GuessGame/HighScore.dat", ios::trunc);
                filehandlerHS<<score;
            }               
            break;
        }
    }
    cout<<"Your Score  :: "<<score;
    system("pause");
    main_menu();
}
////////////////////////TO VIEW HIGHSCORE//////////////
void High_Score()
{
    system("cls");
    cout<<"............**HIGH SCORE DETAILS**.........";
    cout<<"nnn";
    cout<<"Your highest score in game is...... n";
    filehandlerHS>>highscore;
    cout<<highscore<<"!!!";
    cout<<"n Awesome Job!!";
}
////////////TO RESET SCORE//////////////////
void High_Reset()
{
    char ch;
    system("cls");
    cout<<"Are you sure? all your glory will be lost.Y/N n";
    cin>>ch;
    if(ch=='Y'||ch=='y')
    {
        filehandlerHS.close();
        filehandlerHS.open("C:/Users/bhati/Documents/C-Free/Projects/GuessGame/HighScore.dat", ios::trunc);
        cout<<"DONE!!!!!!!!!!!!nn";
    }
    cout<<"Phew!! going back to main menu!";
    system("pause");
    main_menu();
}
////////////////Main Menu/////////////////
void main_menu()
{
    system("cls");
    int main_menu_choice=0;
    cout<<"tt******************MAIN MENU******************n";
    cout<<"tt* 1. New Game                               *n";
    cout<<"tt* 3. High Score                             *n";
    cout<<"tt* 4. Reset High Score                       *n";
    cout<<"tt* Please Enter your Choice(1-4)....         *n";
    cout<<"tt*********************************************n";
    cin>>main_menu_choice; 
    switch(main_menu_choice)
    {
        case 1 : New_Game();
                 break;
        case 3 : High_Score();
                 break;
        case 4 : High_Reset();
                 break;
        default : cout<<"Invalid Choice, please try again.";
                  break;    
    }
}
int main()
{
    filehandlerHS.open("C:/Users/bhati/Documents/C-Free/Projects/GuessGame/HighScore.dat");
    filehandlerHS>>highscore;
    main_menu();
    return 0;
}

问题是

cout << term;

不做你想的那样。由于term被声明为char数组,它认为它是一个C风格的字符串,而不是一个数字数组。因此,它将数组的内容视为字符代码,并期望它以空字节结尾。你得到垃圾,因为09的数字是控制字符。

首先,vocab应包含要显示的字符,而不是数字:

char vocab[]={'0','1','2','3','4','5','6','7','8','9'};////POSSIBLE CHARACTERS

或者更简单地说:

char vocab[] = "0123456789";

然后,您应该使用 std::string 而不是 char 数组。

std::string entry;
std::string term;
for (var j = 0; j < i; j++) {
    term.push_back(random());
}
cin >> entry;
if (entry == term) {
    ...
}

您也不能将数组与 == 进行比较。如果要判断两个数组是否包含相同的值,则需要遍历它们,逐个元素比较它们。但是使用std::string进行termentry也可以解决这个问题。