输出在我的字符数组范围之外,同时使用 rand()
Output outside my char array range, while using rand()
我正在做一个记忆游戏(到目前为止进展不佳),我遇到了这个问题,我的随机函数或我在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风格的字符串,而不是一个数字数组。因此,它将数组的内容视为字符代码,并期望它以空字节结尾。你得到垃圾,因为0
到9
的数字是控制字符。
首先,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
进行term
和entry
也可以解决这个问题。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 我的字符计数代码计算错误.为什么
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- cmake在我的项目中所需的所有静态库都不成功
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 为什么我的for循环不能正确获取argv
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 0-1背包代码中的错误.我的代码中有什么错误
- 当我的阵列太大时出现分段错误
- 为什么 rand 不在我的代码中生成随机数?
- 为什么 rand() 给我的数字超出了我定义的间隔?(昨天工作正常)
- 为什么我的 rand() 会产生负数
- 输出在我的字符数组范围之外,同时使用 rand()
- 为什么我的 rand() 数字显示为 0
- rand() 即使在我的 Mac 上播种时也会给出序列号
- rand() 返回的 int 比我的最大 C++ 高
- rand%以可预测的模式生成我的所有实体
- OpenMP,更多线程导致减速的原因?(没有分享/没有rand()(我认为))