dimanche 22 janvier 2017

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

I'm making a memory game(not going good so far) and I've encountered this problem, my random function or my loop in the new_game function seem to be haywire. I'm a beginner in c++ so any help will be much appreciated. I know the code is a bit unorganized and clumsy, not to mention not optimized. New to all that.

#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;
}




Aucun commentaire:

Enregistrer un commentaire