GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Apr-2007, 00:49
volpix volpix is offline
New Member
 
Join Date: Apr 2007
Posts: 4
volpix is on a distinguished road
Unhappy

hi eveyone! (& helpppp!!!!)


hi eveyone there... im a new guy here... so hi!

can any1 help me? i hav a mojor problem here... is my assignment and is due next week.
v hav 2 write a game and mine has unknown problem... lots of ppl try 2 help me but they dont know wats wrong or y izit wrong...
is not complete yet but i juz need 2 make the bunny move... i canot clear the screen and canot use a getch()...
here's my source cade for it... plz plz plz help.... thx

CPP / C++ / C Code:
#include <iostream>
#include <conio>

const int ROWS=20;       //declaration of constant for size of array
const int COLS=20;
char background[ROWS][COLS];  //declaration of array

//function prototypes
void drawbackground();    //to draw background
//to draw bunny as player
void drawbunny(int&,int&,int&,int&,int&,int&,int&,int&);

void erasebunny();
//to display the background
void displaybackground();
//to ask player to input control
void putcontrol();


int main()   //start of main function
{

 drawbackground(); //call for drawbackground function

 putcontrol();

 return 0;
}

void drawbackground()//start of function to draw background
{
 int i,j,k;

 for(i=0; i<=ROWS; i++)       //nested for loops to initialize the array
 {
  if ((i==0) || (i==ROWS))
  {
   for (k=0; k<=ROWS; k++)
   background[i][k]='#';      //symbol to construct the background
  }

  else if ((i>=1) || (i<ROWS))
  {
   background[i][0]='#';
   background[i][COLS]='#';

   for(j=1; j<=(ROWS-1); j++)
   background[i][j]=' ';
  }
 }

 return;
}//end of drawbackground function

//start of function to draw bunny
void drawbunny(int&row2_a,int&row3_a,int&row4_a,int&col2_a,int&col3_a,int&col4_a,int&col5_a,int&col6_a)
{
 background[row2_a][col3_a]='/';        //initialize the array
 background[row2_a][col4_a]=')';
 background[row2_a][col5_a]='/';
 background[row2_a][col6_a]=')';
 background[row3_a][col2_a]='(';
 background[row3_a][col3_a]='^';
 background[row3_a][col4_a]='.';
 background[row3_a][col5_a]='^';
 background[row3_a][col6_a]=')';
 background[row4_a][col4_a]='v';

 return;
} //end of drawbunny function

void erasebunny(int&row2_b,int&row3_b,int&row4_b,int&col2_b,int&col3_b,int&col4_b,int&col5_b,int&col6_b)
{
 background[row2_b][col3_b]=' ';
 background[row2_b][col4_b]=' ';
 background[row2_b][col5_b]=' ';
 background[row2_b][col6_b]=' ';
 background[row3_b][col2_b]=' ';
 background[row3_b][col3_b]=' ';
 background[row3_b][col4_b]=' ';
 background[row3_b][col5_b]=' ';
 background[row3_b][col6_b]=' ';
 background[row4_b][col4_b]=' ';

 return;
}

void displaybackground() //start of function to display
{                                                   //background
 int l,m;

 clrscr();                      //THE PROBLEM IS HERE!

 for(l=0;l<=ROWS;l++)           //nested for loops to display the background
 {
  for(m=0;m<=COLS;m++)
    cout<<background[l][m];
  cout<<endl;
 }
 return;
}       //end of function to display background

//start of function to receive player input control to move the bunny
void putcontrol()
{
 char quit;
 int row2=2,row3=3,row4=4,col2=2,col3=3,col4=4,col5=5,col6=6;
 drawbunny(row2,row3,row4,col2,col3,col4,col5,col6);   //call for drawbunny function

 do
 {
  char move;
  move=getch();   //THE SECOND PROBLEM IS HERE!!
  switch(move)
  {
    case 'j':        //if the player press j or J
    {
      erasebunny(row2,row3,row4,col2,col3,col4,col5,col6);
      col2=col2-1;
      col3=col3-1;
      col4=col4-1;
      col5=col5-1;
      col6=col6-1;

      drawbunny(row2,row3,row4,col2,col3,col4,col5,col6);

      displaybackground();
      break;
    }

    case 'l':
    {
      erasebunny(row2,row3,row4,col2,col3,col4,col5,col6);
      col2=col2+1;
      col3=col3+1;
      col4=col4+1;
      col5=col5+1;
      col6=col6+1;

      drawbunny(row2,row3,row4,col2,col3,col4,col5,col6);

      displaybackground();
      break;
    }

    case 'i':
    {
      erasebunny(row2,row3,row4,col2,col3,col4,col5,col6);
      row2=row2-1;
      row3=row3-1;
      row4=row4-1;

      drawbunny(row2,row3,row4,col2,col3,col4,col5,col6);

      displaybackground();
      break;
    }

    case 'k':
    {
      erasebunny(row2,row3,row4,col2,col3,col4,col5,col6);
      row2=row2+1;
      row3=row3+1;
      row4=row4+1;

      drawbunny(row2,row3,row4,col2,col3,col4,col5,col6);

      displaybackground();
      break;
    }

    default:
    {
     cout<<"\a";

     drawbunny(row2,row3,row4,col2,col3,col4,col5,col6);

     displaybackground();
     break;
    }

  }
  cout<<"to quit, press y.";
  cin>>quit;
 }while(quit!='y');
 return;
}
Last edited by admin : 11-Apr-2007 at 07:09. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 11-Apr-2007, 17:20
Silent Silent is offline
Junior Member
 
Join Date: Mar 2007
Posts: 44
Silent will become famous soon enough

Re: hi eveyone! (& helpppp!!!!)


Hi Volpix,

I'll start by being completely honest... I don't know the answer to your problem... I can't work out how to paste this into CodeBlocks (kinda frustrating! )

I'm a recent addition to the world of C++ you see, so I'll be banking on some of your advice in the near future too.

Just a suggestion to help you get some help... at the top of this forum is a notice (sticky) explaining the do's and don'ts for asking for help, and one thing we are requested to avoid is L337 5P3AK... if you catch my drift?

I'm not admin here or anyone important, I'd just like to see this bunny jumping too as I know I'd learn something from the finished product.
  #3  
Old 11-Apr-2007, 22:59
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,017
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: hi eveyone! (& helpppp!!!!)


For the 2nd problem, change the 'move' variable to be a two-byte array:
CPP / C++ / C Code:
  char move[2];
then call cin's getline() function to get the input into move:
CPP / C++ / C Code:
  cin.getline(move, 2);  // stores 1 character, plus a terminator.
then change the switch to only look at move's first character:
CPP / C++ / C Code:
  switch( move[0] ) // add index.

For the 1st problem, what system are you using?
[I fear a flame-spray coming from dave, davis, and/or waltp]
if windows, NOTE THAT THIS IS NOT PORTABLE... a.k.a M$-specific!
include stdlib.h, and calling system("cls") might be possible;

if you have another system [UNIX, LINUX], I've seen other posts that claim system("clear") for those, but I'm not 100% sure.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #4  
Old 12-Apr-2007, 06:57
volpix volpix is offline
New Member
 
Join Date: Apr 2007
Posts: 4
volpix is on a distinguished road
Wink

Re: hi eveyone! (& helpppp!!!!)


oh... I am so happy!!... and so touched there are people out there who are willing to help me.
to TurboPT, thank you very very much for your help. I am able to clear screen now (which make me jumping joyously nonstop). feel motivated to continue writing the game now.... because I was really frustrated due to the problems. I am using Borland C++ and can I know why the clrscr() cannot work? and what if I want to input the move without pressing enter? anyway, thanks alot again.
to Silent, thanks for your advice too. I did not look at the do's and don'ts before I ask for help. so, thanks for reminding me. I hope I did not make anyone angry.... I know what not to do now, appreciate your help.
and to 57 other members who had viewed my post, thanks. I so like to be a part of this community.
  #5  
Old 12-Apr-2007, 11:41
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,017
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: hi eveyone! (& helpppp!!!!)


How old is your Borland compiler?

Here are some posts with more info. about clrscr(), and the header file conio.h: (concerning their non-standardness)
(by waltp) http://www.gidforums.com/showpost.ph...77&postcount=4
(by davekw7x) http://www.gidforums.com/showpost.ph...82&postcount=5

A couple of other things that I didn't mention, in the previous post, that you might want to consider:
1. Since you have headers without the .h extension, I didn't see this statement (typically found between the header files and main):
CPP / C++ / C Code:
  using namespace std;
Depending on the age of your compiler, this might error. However, there should have been other compile errors (again, depending on the compiler), especially with cout/cin without this statement.

2. You are working outside the bounds of the array with some of your loops.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #6  
Old 13-Apr-2007, 02:03
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,281
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: hi eveyone! (& helpppp!!!!)


Quote:
Originally Posted by TurboPT
A couple of other things that I didn't mention, in the previous post, that you might want to consider:
1. Since you have headers without the .h extension, I didn't see this statement (typically found between the header files and main):
CPP / C++ / C Code:
  using namespace std;
Depending on the age of your compiler, this might error. However, there should have been other compile errors (again, depending on the compiler), especially with cout/cin without this statement.
The Borland compiler in use is most likely from before namespaces were introduced to C++. Adding the statement will probably cause errors.

And volpix, since it's obvious you haven't done this yet, please read the Guidelines. You must have missed them on the main C++ forum page.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #7  
Old 13-Apr-2007, 11:02
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 165
dabigmooish will become famous soon enough

Re: hi eveyone! (& helpppp!!!!)


Quote:
Originally Posted by volpix
I am using Borland C++ and can I know why the clrscr() cannot work?
I would just forget about using this function. It's nonstandard and it isn't working for you anyway. I think WaltP's method (as referenced earlier) works best.

Quote:
Originally Posted by volpix
and what if I want to input the move without pressing enter?

I don't think you can do this. You have to let C++ know when you are done entering text. Otherwise how would the program know when to stop gathering data?
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
  #8  
Old 13-Apr-2007, 22:27
volpix volpix is offline
New Member
 
Join Date: Apr 2007
Posts: 4
volpix is on a distinguished road
Question

Re: hi eveyone! (& helpppp!!!!)


hi again, I am kind of busy doing my assignment and thaknks for those tips. however, i can use the clrscr() and getch() in other function(like interface and menu) so I am wondering why is it only malfunction inside the putcontrol function?

and when I face a few problems when I wanted to use setcolor function because in some function(displaybackground) I do not think I can use textcolor and cprintf. so when I try with the setcolor with the header <windows>,

CPP / C++ / C Code:
#include <windows>

void setcolor(unsigned short color)    //setcolor function
{
   HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleTextAttribute(hcon,color);
}

I got 5 errors such as (20,15): too many types in declaration, (20,16): { expected, (47,12): expression syntax, (54,15): too many types in declaration, and (54,16): { expected. all this do not appear until I put in the header file #include <windows>.

here is the game so far, still working on the time limit function as I do not know how to countdown. and I cut some irrelevant part like what the guideline say....

CPP / C++ / C Code:
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>

const int ROWS=50;       //declaration of constant for size of array
const int COLS=50;
char background[ROWS][COLS];  //declaration of array
int score;

//function prototypes
//to display introduction
void interface();
//to display menu
void menu();
//to draw hearts and diamonds
void drawhearts();
//to draw background
void drawbackground();
//to draw bunny as player
void drawbunny(int&,int&,int&,int&,int&,int&,int&,int&);
//to erase the bunny from array
void erasebunny();
//to display the background
void displaybackground();
//to ask player to input control
void putcontrol();
//to count score
//int countscore(int&,int&,int&,int&,int&,int&,int&,int&);
//to count down the time limit
//int timelimit();
//function for exit option
void endgame();


int main()   //start of main function
{
 _setcursortype(_NOCURSOR);
 
 interface();

 menu();

 return 0;
}

void interface()
{
   textcolor(13);
   cprintf("  ###     ###  #########  ###       #########  #########  ###    ### ########");
   cprintf("     ###     ###  #########  ###       #########  #########  ###    ### ########");
   cprintf("     ###  #  ###  ###        ###       ###        ###   ###  ####  #### ###     ");
   cprintf("     ### ### ###  #########  ###       ###        ###   ###  ########## ########");
   cprintf("     ###########  ###        ###       ###        ###   ###  ### ## ### ###     ");
   cprintf("     ### ## ####  #########  ########  #########  #########  ###  # ### ########");
   cprintf("     ###  #  ###  #########  ########  #########  #########  ###    ### ########");

 getch();         //I can use getch() here
 return;
}

void menu()
{
 char option;
 clrscr();        //I can use clrscr() here

 cout<<"                                  MOVE UP"<<endl;
 cout<<"                                  ......."<<endl;
 cout<<"                                  :  I  :"<<endl;
 cout<<"                                  :     :"<<endl;
 cout<<"                            ..................."<<endl;
 cout<<"                            :     :     :     :"<<endl;
 cout<<"                  MOVE LEFT :  J  :  K  :  L  : MOVE RIGHT"<<endl;
 cout<<"                            ..................:"<<endl;
 cout<<"                                  MOVE DOWN"<<endl<<endl;
 cout<<"                        Help Bunny in her journey."<<endl;
 cout<<"            Bunny must get as many hearts and diamonds as possible"<<endl;
 cout<<"   to fill his heart with love before the time runs out AND to earn more marks."<<endl<<endl;
 cout<<" TO PROCEED TO GAME PRESS 'P'                    TO EXIT PRESS 'E'"<<endl;

  cin>>option;
  if(option=='P'||option=='p')
  {
    putcontrol();
  }
  else if(option=='E'||option=='e')
  {
    endgame();
  }
  else
  {
    while(option!='P'||option!='p'||option!='E'||option!='e')
    {
 	   cout<<"INVALID INPUT"<<'\1'<<"PLEASE ENTER AGAIN!!THANK YOU!!"<<endl;
      cin>>option;

      if(option=='P'||option=='p')
      {
        putcontrol();
      }
      else if(option=='E'||option=='e')
      {
        endgame();
      }
      else
      continue;
    }
  }

 return;
}

void drawhearts()
{
 background[2][26]='\4';
 background[25][25]='\3';
 background[3][45]='\4';
 background[4][35]='\3';
 background[38][40]='\4';
 background[45][16]='\4';
 background[6][45]='\3';
 background[7][23]='\4';
 background[42][13]='\3';
 background[44][30]='\4';
 background[10][26]='\4';
 background[11][5]='\3';
 background[12][45]='\4';
 background[13][2]='\3';
 background[14][18]='\4';
 background[23][25]='\4';
 background[24][32]='\3';
 background[34][6]='\4';
 background[48][35]='\3';
 background[40][17]='\4';
 background[11][5]='\4';
 background[15][24]='\3';
 background[30][6]='\3';
 background[38][22]='\4';
 background[27][45]='\3';
 background[28][2]='\4';
 background[5][19]='\3';
 background[45][49]='\3';
 background[46][3]='\4';
 background[29][7]='\3';

 return;
}

void drawbackground()//start of function to draw background
{
 int i,j,k,score;

 for(i=0; i<=ROWS; i++)       //nested for loops to initialize the array
 {
  if ((i==0) || (i==ROWS))
  {
   for (k=0; k<=ROWS; k++)
     background[i][k]='#';      //symbol to construct the background
  }

  else if ((i>=1) || (i<ROWS))
  {
   background[i][0]='#';
   background[i][COLS]='#';

   for(j=1; j<=(ROWS-1); j++)
   background[i][j]=' ';
  }
 }

 return;
}//end of drawbackground function

//start of function to draw bunny
void drawbunny(int&row2_a,int&row3_a,int&row4_a,int&col2_a,int&col3_a,int&col4_a,int&col5_a,int&col6_a)
{
 background[row2_a][col3_a]='/';        //initialize the array
 background[row2_a][col4_a]=')';
 background[row2_a][col5_a]='/';
 background[row2_a][col6_a]=')';
 background[row3_a][col2_a]='(';
 background[row3_a][col3_a]='^';
 background[row3_a][col4_a]='.';
 background[row3_a][col5_a]='^';
 background[row3_a][col6_a]=')';
 background[row4_a][col4_a]='v';

 return;
} //end of drawbunny function

//start of function to erase buuny
void erasebunny(int&row2_b,int&row3_b,int&row4_b,int&col2_b,int&col3_b,int&col4_b,int&col5_b,int&col6_b)
{
 background[row2_b][col3_b]=' ';     //assign blanks to array to erase the bunny
 background[row2_b][col4_b]=' ';
 background[row2_b][col5_b]=' ';
 background[row2_b][col6_b]=' ';
 background[row3_b][col2_b]=' ';
 background[row3_b][col3_b]=' ';
 background[row3_b][col4_b]=' ';
 background[row3_b][col5_b]=' ';
 background[row3_b][col6_b]=' ';
 background[row4_b][col4_b]=' ';

 return;
} //end of erasebunny function

void displaybackground() //start of function to display background
{
 int l,m;

 system("cls");                 //to clear screen

 for(l=0;l<=ROWS;l++)           //nested for loops to display the background
 {
  for(m=0;m<=COLS;m++)
  {
     cout<<background[l][m];          //I do not think that textcolor and cprintf will work here
  }                                   //so is there any other way?
  cout<<endl;
 }

 cout<<"to exit, press e."<<endl;
 return;
}       //end of function to display background

//start of function to receive player input control to move the bunny
void putcontrol()
{
 drawbackground(); //call for drawbackground function
 drawhearts();     //call for drawhearts function

 char quit;      //declare the quit variable
 //declare and initialize variable for array index

 int row2=2,row3=3,row4=4,col2=2,col3=3,col4=4,col5=5,col6=6;

 //call for drawbunny function
 drawbunny(row2,row3,row4,col2,col3,col4,col5,col6);
 //to display the background
 displaybackground();

 while(1)     //while loop to keep receiving input
 {
  char move[2];           //declare move as single array
  cin.getline(move,2);

  //if(                     //if time runs out
  switch(move[0])         //switch statement for variable move
  {
    case 'j':        //if the player press j
    {
      //call fuction to erase the buuny of from the last position
      erasebunny(row2,row3,row4,col2,col3,col4,col5,col6);
      col2=col2-1;     //everything move left 1 column
      col3=col3-1;
      col4=col4-1;
      col5=col5-1;
      col6=col6-1;


      //call fuction to draw the new bunny
      drawbunny(row2,row3,row4,col2,col3,col4,col5,col6);

      //call function to display the new background
      displaybackground();
      break;
    }

    case 'l':         //if the player press l
    {
      erasebunny(row2,row3,row4,col2,col3,col4,col5,col6);
      col2=col2+1;    //everything move left 1 column
      col3=col3+1;
      col4=col4+1;
      col5=col5+1;
      col6=col6+1;

      drawbunny(row2,row3,row4,col2,col3,col4,col5,col6);

      displaybackground();
      break;
    }

    case 'i':        //if the player press i
    {
      erasebunny(row2,row3,row4,col2,col3,col4,col5,col6);
      row2=row2-1;   //everything move up 1 row
      row3=row3-1;
      row4=row4-1;

      drawbunny(row2,row3,row4,col2,col3,col4,col5,col6);

      displaybackground();
      break;
    }

    case 'k':        //if the player press k
    {
      erasebunny(row2,row3,row4,col2,col3,col4,col5,col6);
      row2=row2+1;   //everything move down 1 row
      row3=row3+1;
      row4=row4+1;

      drawbunny(row2,row3,row4,col2,col3,col4,col5,col6);

      displaybackground();
      break;
    }

    case 'e':        //if the player e to choose to quit
    {
       endgame();    //call for endgame function
    }

    default:        //if the player press other character
    {
     drawbunny(row2,row3,row4,col2,col3,col4,col5,col6);

     displaybackground();
     break;
    }
  }

 }
 return;
}


//start of timelimit function      //I do not know what I am writing here
//int timelimit()
//{
  //clock_t countdown;
  //countdown= 10;
  //while(clock()< countdown)
  //{}
//}

//start of function of endgame
void endgame()
{
  system("cls");

  char quit;
  cout<<"Do you really want to quit?\n(yes-y or no-n)\n";
  cin>>quit;

  if(quit=='y')      //if player choose y
     {
       exit(1);         //exit the game
     }
     else if(quit!='y')    //if the player not choose y
     {
      putcontrol();
     }
  return;
}  //end of endgame fuunction

is it all right to post such long codes here? if not, please tell me....
  #9  
Old 14-Apr-2007, 21:33
volpix volpix is offline
New Member
 
Join Date: Apr 2007
Posts: 4
volpix is on a distinguished road
Thumbs down

Re: hi eveyone! (& helpppp!!!!)


Quote:
Originally Posted by volpix
and when I face a few problems when I wanted to use setcolor function because in some function(displaybackground) I do not think I can use textcolor and cprintf. so when I try with the setcolor with the header <windows>,

CPP / C++ / C Code:
#include <windows>

void setcolor(unsigned short color)    //setcolor function
{
   HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleTextAttribute(hcon,color);
}

I got 5 errors such as (20,15): too many types in declaration, (20,16): { expected, (47,12): expression syntax, (54,15): too many types in declaration, and (54,16): { expected. all this do not appear until I put in the header file #include <windows>.

last night I go through the code and I hate myself for not realizing source of the problem earlier. I can use the <windows.h> header file now as the real cause is the name of the function: interface. it is actually a keyword,set aside for special purpose and cannot be use. Problem solve!
sigh... the assignment is suppose to be pairwork and it is my partner's reponsible to do the intro (since she has no idea how to write a more complicated game) and a few problems I face is from her part of code. like the hearts thingy, she wants to have two kinds(hearts and diamond) which cause the marks to go haywire...

anyway, I solve the problem and I am sorry if I trouble anyone. The assignment will be summit on Monday.
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Which text has the most words, the least words, etc. GQgirl C Programming Language 5 27-Nov-2006 01:07
reading from csv file, plzzzzzzzz helpppp tomyy C Programming Language 3 10-Mar-2006 10:44
Hello to eveyone Dailyhosting New Member Introductions 2 26-Nov-2004 01:27
hello eveyone hangmansjoke Open Discussion Forum 7 25-Sep-2004 03:57

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:19.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.