hi.. i'm new here
i try to do bingo program in separated file but my class name is not declare.. i search on internet on how to solve it but still gives me error
can some one help me???

#ifndef BINGO_H
#define BINGO_H
#include<iostream>
#include<stdlib.h>
#include <time.h>
#include <string.h>
using namespace std;
class game
{
    public:
    
    void display();
    void rules();
    void gen_rand(int size, int* array);
    void print_map(int size, int array[]);
    void start(int size, int* array, int x);
    void play(int* array1, int* array2, int size, int x);
    void word( int* array1, int* array2,  int size);
    game();
    private:
    int size=25;
    int P1[size];
    int P2[size];
    int x;
    int y;
    int num;
    char bingo1[]="     ";
    char bingo2[]="     ";
    char choice;
};
#endif
#include<iostream>
#include<stdlib.h>
#include <time.h>
#include <string.h>
#include "BINGO.h"

using namespace std;
game::game()
{

}

void game::display()
{
    system("cls");
    cout <<"WELCOME TO BINGO" << endl<< endl;
    cout <<"+-----------------------------------+" << endl;
    cout <<"|Select:                            |" << endl;
    cout <<"|1 => Rules                         |" << endl;
    cout <<"|2 => Play                          |" << endl;
    cout <<"|Q => Quit                          |" << endl;
    cout <<"+-----------------------------------+" << endl;
    cout <<"Number selected:";
}
void game::rules()
{
cout << endl;
cout <<"This game require 2 players.\nEach player is holding 25 numbers (1-25).\nThey will plot these numbers randomly on a 5 by 5 square plane.\nPlayer 1 will guess a number both the players must mark this\nnumber on their square planes.\nNext, player 2 will do the guessing and both the players will mark the\nguessed number on their planes. E.g. " << endl;
cout <<endl;
cout <<"Assume that player 1 has the following plane:"<< endl;
cout <<"1   8   6   18  17" << endl;
cout <<"14  11  16  5   22" << endl;
cout <<"20  2   10  24  13" << endl;
cout <<"4   15  25  19  23" << endl;
cout <<"9   21  3   12  7" << endl;
cout <<endl;
cout <<"and player 2 has the following plane:" << endl;
cout <<"25  13  17  21  22" << endl;
cout <<"1   24  4   16  12" << endl;
cout <<"2   8   11  9   5" << endl;
cout <<"20  19  23  15  18" << endl;
cout <<"7   14  3   10  6" << endl;
cout <<endl;
cout <<"Player 1 guesses number 10 and both player\nwill plat the number in their plane." << endl;
cout <<"Player 1 plane" << endl;
cout <<"1   8   6   18  17" << endl;
cout <<"14  11  16  5   22" << endl;
cout <<"20  2   X   24  13" << endl;
cout <<"4   15  25  19  23" << endl;
cout <<"9   21  3   12  7" << endl;
cout <<"Player 2 plane" << endl;
cout <<"25  13  17  21  22" << endl;
cout <<"1   24  4   16  12" << endl;
cout <<"2   8   11  9   5" << endl;
cout <<"20  19  23  15  18" << endl;
cout <<"7   14  3   X   6" << endl;
cout <<endl;
cout <<"The game goes on.\nAssume that player 1 square plane has the following pattern" << endl;
cout <<"1   8   6   18  X" << endl;
cout <<"14  11  16  X   22" << endl;
cout <<"20  2   X   24  13" << endl;
cout <<"4   X   25  19  23" << endl;
cout <<"X   21  3   12  7" << endl;
cout <<endl;
cout <<"This indicates that, player 1 got B from the B-I-N-G-O letters.\nPlayer 1 will win the game if he manages to guess \nthe correct numbers to obtain all the 5 letters before player 2. E.g." << endl;
cout <<"X   X   X   X   X" << endl;
cout <<"X   11  X   X   22"<< endl;
cout <<"X   2   X   24  13" << endl;
cout <<"X   X   X   X   X" << endl;
cout <<"X   21  X   12  7" << endl;
system("pause");
}
void game::gen_rand(int size, int* array)
{


    srand ( time(NULL) );

    for(int x=0; x<size; x++) {
        array[x] = rand()%25 + 1;                                   //generate random number between 1-25 for array
        for(int y=0; y<x; y++) {
                if(array[y]== array[x]){
                   x=x-1;
                    y=x;}
        }
    }
}

void game::print_map(int size, int array[])
{

    int x;

    cout<<endl;                                                     //   ________________________
    cout<<"\t\t ________________________"<<endl;                    //  |    |    |    |    |    |
    cout<<"\t\t|    |    |    |    |    |\n";                       //  | 0  | 1  | 2  | 3  | 4  |
    for(x=0; x<size; x++) {                                         //  |____|____|____|____|____|
                                                                    //  |    |    |    |    |    |
        if(x%5==0)                                                  //  | 5  | 6  | 7  | 8  | 9  |
            cout<<"\t\t| ";                                         //  |____|____|____|____|____|
        if (array[x]!=0)                                            //  |    |    |    |    |    |
            cout << array[x];                                         //  | 10 | 11 | 12 | 13 | 14 |
        else                                                        //  |____|____|____|____|____|
            cout<<"X";                                              //  |    |    |    |    |    |
        if(array[x]<10)                                             //  | 15 | 16 | 17 | 18 | 19 |
            cout<<" ";                                              //  |____|____|____|____|____|
        cout<<" | ";                                                //  |    |    |    |    |    |
        if((x+1)%5==0 && x!= 24) {                                  //  | 20 | 21 | 22 | 23 | 24 |
            cout<<endl<<"\t\t|____|____|____|____|____|";           //  |____|____|____|____|____|
            cout<<endl<<"\t\t|    |    |    |    |    |"<<endl;}
        if (x==24)
            cout<<endl<<"\t\t|____|____|____|____|____|"<<endl;
    }
}

void game::start(int size, int* array, int p)
{

    char choice;
    bool proceed = false;  //set proceed = 0//
    do
    {
        cout<<"\n\tPlayer "<<p<<" please press any key to view your plane...\n\n";
        system("pause");
        system("cls");

        gen_rand(size, array);
        print_map(size, array);

        bool invalid = false;
        do{
            cout<<"\n\tDo you want to keep this plane and proceed? (y/n) :";
            cin>>choice;

            if(choice=='y' || choice=='Y') {
                cout<<"\n\tThis will be your plane!\n\n";
                invalid = true;
                proceed = true; }
            else if(choice =='n' || choice=='N')
                invalid = true;
            else {
                cout<<"\tInvalid choice..";
                invalid = false;}
        }
        while (!invalid); // terminate the program//
    }
    while (!proceed); // terminate the program//
   (system("pause"));
}

void game::ins_x(int size, int* array, int num)
{

    int x;

    for(x=0; x<size; x++) {
        if (array[x]==num){
        array[x]=0;
        x=size;}
    }
}

void game::check_bin( int* array, char* string, int start, int end, int add)
{

    int x, count=0;

     for(x=start; x<=end; x+=add) {
            if(array[x]==0)
                count++;}

    if(count==5){
        for(x=0; x<5; x++){
            if(string[x]==' ') {
                switch(x) {
                case 0: string[0]='B';
                        break;
                case 1: string[1]='I';
                        break;
                case 2: string[2]='N';
                        break;
                case 3: string[3]='G';
                        break;
                case 4: string[4]='O';
                        break;
                default: break;}
                //x=5;
                }
        }
    }
}

void game::check_end(int* array, char* string)
{

    check_bin(array, string, 0,4,1);
    check_bin(array, string, 5,9,1);
    check_bin(array, string, 10,14,1);
    check_bin(array, string, 15,19,1);
    check_bin(array, string, 20,24,1);
    check_bin(array, string, 0,20,5);
    check_bin(array, string, 1,21,5);
    check_bin(array, string, 2,22,5);
    check_bin(array, string, 3,23,5);
    check_bin(array, string, 4,24,5);
    check_bin(array, string, 0,24,6);
    check_bin(array, string, 4,20,4);
}

void game::play(int size, int* array1, int* array2, char* string1, char* string2, int p1, int p2, bool &done, bool &replay )
{

    int num;

    system("cls");
    cout<<"\n\tPlayer "<<p1<<" ready?\n\n";
    system("pause");

    system("cls");
    print_map(size, array1);
    cout<<"\n\t\t"<<string1<<endl<<endl;
    strncpy(string1, "     ", 5);

    bool valid = true;
    do{
        cout<<endl<<"\tPlayer "<<p1<<" please choose a number(1-25): ";
        cin>>num;

        if(num<1 || num>25){
            cout<<"\tInvalid number!";
            valid = false;}
        else
            valid = true;
    } while(!valid);

    ins_x(size, array1, num);
    ins_x(size, array2, num);
    check_end(array1, string1);
    strncpy(string2, "     ", 5);
    check_end(array2, string2);

    system("cls");
    print_map(size, array1);
    cout<<"\n\t\t"<<string1<<endl<<endl;

    if(strncmp (string1, "BINGO",5) == 0) {
        system("cls");
        cout <<"\n\t\tPlayer "<<p1<<endl;
        print_map(size, array1);
        cout<<"\n\t\t"<<string1<<endl<<endl;

        cout <<"\n\t\tPlayer "<<p2<<endl;
        print_map(size, array2);
        cout<<"\n\t\t"<<string2<<endl<<endl;

        cout <<"\n\t\tCONGRATULATIONS!!!\n\n\tPlayer "<<p1<<" WON!!!!\n";
    system("pause");
}
}
#include<iostream>
#include<stdlib.h>
#include <time.h>
#include <string.h>
#include "BINGO.h"
using namespace std;
int main()
{
    game().display();
    game().rules();
    game().gen_rand(int size, int* array);
    game().print_map(int size, int array[]);
    game().start(int size, int* array, int x);
    game().play(int* array1, int* array2, int size, int x);
    game().word( int* array1, int* array2,  int size);
    return 0;
}

A few observations:
1) You shouldn't use headers like <string.h> or <time.h>. These are deprecated. You should use <cstring> and <ctime> instead (notice "c" in front, and no ".h"), these are versions of the same old libraries but that will compile more easily with C++ code and compiler.
2) In your class "game" declaration, line 21, 27 and 28 are illegal. If you want to give a value to a data member of a class, they need to be both static and constant. As so:

class game {
  //...
  private:
    static const int size = 25;
   //...
};

3) When you are calling the functions in the main function, you need to give parameters to the function that you call. All line 11 to 15 (in the last segment) are illegal. These are not function calls but they are function prototypes (or declarations) (which are not allowed inside a function, for your practical purposes). I cannot change all those function calls for you because I don't know exactly what parameters they should be given, but here is a simple example:

#include <iostream>

void myFunction(int aParameter); //this is called a function declaration.

void myFunction(int aParameter) { //this is the function's implementation.
  std::cout << "The parameter is " << aParameter << std::endl;
};

int main() {
  myFunction(42); //this is a function call (with a literal parameter);
  int a = 24;
  myFunction(a); //this is a function call (with a variable passed as the parameter).
  return 0;
};

4) Your main function makes a very odd use of the class "game". You would normally create an object of class "game" and then call its member functions. Not create a new object for each member function call (that is what your code does). This would be a more normal use:

int main()
{
  game myGame; //this creates an object called "myGame" of class "game".
  myGame.display();
  myGame.rules();
  myGame.gen_rand(int size, int* array);                 // You still
  myGame.print_map(int size, int array[]);               // have to
  myGame.start(int size, int* array, int x);             // fix all
  myGame.play(int* array1, int* array2, int size, int x);// those 
  myGame.word( int* array1, int* array2,  int size);     // function calls!
  return 0;
};

This should help you clear up a good chunk of the errors that you have. Please post the exact error messages if you have further problem compiling it. And welcome to Daniweb!

thanks 4 ur guide...\(^O^)/
i already delete the datatype in my main but it gives me error my parameters are not declare. and also the game class is not declare
is it correct how i link my main with .cpp file n h file?

You need to compile it with this command-line:

g++ main.cpp BINGO.cpp -o BingoGame

Or if you are using Visual C++, you need to make sure that main.cpp and BINGO.cpp are both part of the project "sources" and that the directory in which your BINGO.h file is is part of the "include paths". At first, you should just try to put all the code in one file (content of BINGO.h, then the content of BINGO.cpp and then the content of main.cpp) and make sure that compiles and works without error. After that, you should worry about splitting it up into different files.

hi.. i already try to run in one file.. but i wanted to print out a letter from word bingo if there is 5 x's in one row or column and it wont came out.

#include<iostream>
#include<stdlib.h>
#include <time.h>
#include <string.h>

using namespace std;

class player {

    public:

        player(string sname);
        string get_name();
        void gen_rand();
        void print_map();
        void start();
        void ins_x();
        void check_bin(int start, int end, int add);
        void check_end();
        void play(int pass_num, bool &done);
        int get_num();

   private:

        string name;
        char bingo[];
        int i,j, num;
        static const int size=25;
        int array[size];
        char choice;
};


player::player(string sname) {

        name=sname;
        num = 0;
        for(i=0;i<size;i++){
            array[i]=0;}
}

string player::get_name(){
    return name;
}

void player::gen_rand() {

    srand ( time(NULL) );
    for(i=0; i<size; i++) {
        array[i] = rand()%25 + 1;
        for(j=0; j<i; j++) {
                if(array[j]== array[i]){
                   i=i-1;
                    j=i;}
        }
   }

}

void player::print_map() {

    cout<<endl;                                                     //   ________________________
    cout<<"\t\t ________________________"<<endl;                    //  |    |    |    |    |    |
    cout<<"\t\t|    |    |    |    |    |\n";                       //  | 0  | 1  | 2  | 3  | 4  |
    for(i=0; i<size; i++) {                                         //  |____|____|____|____|____|
                                                                    //  |    |    |    |    |    |
        if(i%5==0)                                                  //  | 5  | 6  | 7  | 8  | 9  |
            cout<<"\t\t| ";                                         //  |____|____|____|____|____|
        if(array[i]!=0)                                            //  |    |    |    |    |    |
            cout<<array[i];                                         //  | 10 | 11 | 12 | 13 | 14 |
        else                                                        //  |____|____|____|____|____|
            cout<<"X";                                              //  |    |    |    |    |    |
        if(array[i]<10)                                             //  | 15 | 16 | 17 | 18 | 19 |
            cout<<" ";                                              //  |____|____|____|____|____|
        cout<<" | ";                                                //  |    |    |    |    |    |
        if((i+1)%5==0 && i!= 24) {                                  //  | 20 | 21 | 22 | 23 | 24 |
            cout<<endl<<"\t\t|____|____|____|____|____|";           //  |____|____|____|____|____|
            cout<<endl<<"\t\t|    |    |    |    |    |"<<endl;}
        if (i==24)
            cout<<endl<<"\t\t|____|____|____|____|____|"<<endl;
    }
}

void player::start() {

    cout<<"\n\t\tLETS START THE GAME!\n\n";

    bool proceed = false;  //set done = 0//

    do
    {
        cout<<"\n\t"<<player::get_name()<<" please press any key to view your plane...\n\n";
        system("pause");
        system("cls");

        player::gen_rand();
        player::print_map();

        bool invalid = false;
        do{
            cout<<"\n\tDo you want to keep this plane and proceed? (y/n) :";
            cin>>choice;

            if(choice== 'y' || choice == 'Y') {
                cout<<"\n\tThis will be your plane!\n\n";
                invalid = true;
                proceed = true; }
            else if(choice =='n' || choice == 'N')
                invalid = true;
            else {
                cout<<"\tInvalid choice..";
                invalid = false;}
        }
        while (!invalid); // terminate the program//
    }
    while (!proceed); // terminate the program//
    system("pause");
    system("cls");
}

void player::ins_x() {

    for(i=0; i<size; i++) {
        if (array[i]==num){
        array[i]=0;
        i=size;}
    }
}

void player::check_bin(int start, int end, int add) {

    int count;

    for(i=start; i<=end; i+=add) {
            if(array[i]==0)
                count++;}

    if(count==5){
        for(i=0; i<5; i++){
            if(bingo[i]=' '){
            switch(i) {
                case 0: bingo[0]='B';
                        break;
                case 1: bingo[1]='I';
                        break;
                case 2: bingo[2]='N';
                        break;
                case 3: bingo[3]='G';
                        break;
                case 4: bingo[4]='O';
                        break;
                default:break;}
            i=5;}
                }
        }
    }


void player::check_end() {

    check_bin(0,4,1);
    check_bin(5,9,1);
    check_bin(10,14,1);
    check_bin(15,19,1);
    check_bin(20,24,1);
    check_bin(0,20,5);
    check_bin(1,21,5);
    check_bin(2,22,5);
    check_bin(3,23,5);
    check_bin(4,24,5);
    check_bin(0,24,6);
    check_bin(4,20,4);
}

void player::play(int pass_num, bool &done) {

    num=pass_num;
    player::ins_x();
    strncpy(bingo, "     ", 5);
    player::check_end();

    system("cls");
    cout<<"\n\t"<<player::get_name()<< "ready?\n\n";
    system("pause");

    system("cls");
    player::print_map();
    cout<<"\n\t\t" << bingo << endl << endl;

    bool valid = false;
    do{
        cout<<endl<<"\t"<<player::get_name()<<" please choose a number(1-25): ";
        cin>>num;

        if(cin.fail()) {
            cin.clear();
            cin.ignore(100,'\n');
            cout<<"\tInvalid number!";
            valid = false;}
        else if(num<1 || num>25){
            cout<<"\tInvalid number!";
            valid = false;}
        else
            valid = true;
    } while(!valid);

    player::ins_x();
    strncpy(bingo, "     ", 5);
    player::check_end();

    system("cls");
    player::print_map();
    cout<<"\n\t\t"<<bingo<<endl<<endl;

    if(strncmp (bingo, "BINGO",5) == 0) {
        cout <<"\n\t\tCONGRATULATIONS!!!\n\n\t"<<player::get_name()<<" WON!!!!\n";
        done = true;
     }
     else
        done = false;

    system("pause");
}

int player::get_num(){
    return num;
}

int main()
{
        string user1, user2;
        int x;

        cout<<"\n\n\t\tPlayer 1 please enter your name: ";
        cin>>user1;
        cout<<"\n\n\t\tPlayer 2 please enter your name: ";
        cin>>user2;
        system("cls");

        player p1(user1);
        player p2(user2);
        p1.gen_rand();
        p2.gen_rand();
        p1.start();
        p2.start();

        cout<<"\n\n\t\tLETS THE GAME BEGIN!!\n";
        cout<<"\n\n\t\t"<<p1.get_name()<<" starts first!\n\n";
        system("pause");

        bool done = false;
        do {
            x = p2.get_num();
            p1.play(x,done);
            if(!done)
            x = p1.get_num();
            p2.play(x,done);

        } while (!done);

    cout<<"\n\n\t\tThank You For Playing...Bye Bye....";
    system("pause");
}

the function to call the letter is in function play line 130

You forgot to give an initial value to "count" on line 132. Initialise it to zero and it should work. However, there are a few additional changes to make, listed below. But overall, I would say great job! A quality of programming rarely seen at your level!

Small changes:
1) At line 26, you should declare the variable bingo as a "string", it will make it a lot better (right now you actually have a memory corruption problem because bingo is a pointer that points to anywhere and you are overwriting that memory and that's not good). You could also use a fixed array (i.e. "char bingo[6]") but that just useless trouble. So, change the line 26 to "string bingo;".
2) As said before, at line 132, add an initial value to count, i.e. "int count = 0;".
3) You have a small typo at line 140, you used a single equal sign where there should be two.
4) Lines 179 and 208 should be changed to reflect the change of the type of bingo. In other words, it should be "bingo = " ";" (five spaces).
5) The string comparison at line 215 should also be changed to "if(bingo == "BINGO") {".
That's it! I compiled it on my computer and it works like a charm! Pretty fun game too!

Edited 5 Years Ago by mike_2000_17: n/a

\(^O^)/ i can compile already the program... thank u so much for ur help..
and now i try to put it in separated file.. h file, cpp and main
but when i compile my main class show and play bingo is not declare..
can we mix a normal function and a class function together?

This article has been dead for over six months. Start a new discussion instead.