我正在制作一个记忆游戏(到目前为止还不顺利)并且我遇到了这个问题,我的随机函数或我在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 you\n"
<<"For a specific period of time, you have to remember it\n"
<<"And once the text dissapears, enter it, correct answers gives 5 points\n"
<<"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<<"\n\n\n";
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!!!!!!!!!!!!\n\n";
}
cout<<"Phew!! going back to main menu!";
system("pause");
main_menu();
}
////////////////Main Menu/////////////////
void main_menu()
{
system("cls");
int main_menu_choice=0;
cout<<"\t\t******************MAIN MENU******************\n";
cout<<"\t\t* 1. New Game *\n";
cout<<"\t\t* 3. High Score *\n";
cout<<"\t\t* 4. Reset High Score *\n";
cout<<"\t\t* Please Enter your Choice(1-4).... *\n";
cout<<"\t\t*********************************************\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;
}
答案 0 :(得分: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
也可以解决这个问题。