Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,480 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,714 people online right now. Registration is fast and FREE... Join Now!




problem with char buffer command in my project

 
Reply to this topicStart new topic

problem with char buffer command in my project

ISAI
5 Mar, 2008 - 11:44 PM
Post #1

New D.I.C Head
*

Joined: 26 Dec, 2007
Posts: 41


My Contributions
I created a code that makes a text i wrote to appear in random color except for black (the background color)
in a few different pages and when i enter one of them and press a key it's as if i pressed a key in all that pages that has the getch(); / kbhit(); command.

EX: i enter the High score page, i press some random key to make the page go back to the menu,
and then i go to the Instructions page and it enters it for a second and then it go back to the menu as if i pressed a key again. (which i didn't).

My teacher and i sat down on my code and tried changing the getch(); to kbhit(); , or using flushall(); command,
and nothing seems to fix this problem.

(the flushall command is still in my code i didn't removed it)

heres the menu page (game.cpp):
CODE
#include <graphics.h>
#include "d:\1vs100\button.h"
#include "d:\1vs100\mouse.h"
#include "d:\1vs100\screens.h"
#include "d:\1vs100\q_music.h"
#include "d:\1vs100\q_sports.h"
#include "d:\1vs100\q_tech.h"
#include "d:\1vs100\q_enter.h"
#include "d:\1vs100\q_game1.h"

void game1(int flag,char *name,int Pflag)
{
  int a,c,d,i,l,x,y,status,places=7,mone=0,degel=1,left=185,top=125;
  int index,mone_admin=0,admin[]={'a','d','m','i','n'};
  char ch,temp[2];
  button *array;

     array=(button*)malloc(places*sizeof(button)); // array of button
     if (array==NULL) // if memory=not enough room > NULL
    {
       printf("Not enough memory to allocate buffer!\n");
       getch();
       exit(0);
    }
     array[0]=InitButton(190,380,290,410,0,13,"Music");
     array[1]=InitButton(190,420,290,450,0,13,"Sports");
     array[2]=InitButton(300,420,400,450,0,13,"Technology");
     array[3]=InitButton(300,380,400,410,0,13,"Entertainment");
     array[4]=InitButton(20,45,100,65,0,13,"Quit");
     array[5]=InitButton(20,20,100,40,0,13,"Main Menu");
     array[6]=InitButton(243,125,343,155,0,13,"1VS100");

     setfillstyle(8,9); // s,c
     bar(15,480,0,0); // left frame
     bar(0,15,640,0); // up frame
     bar(623,480,640,0); // right frame
     bar(0,480,640,465); // down frame
     setcolor(5);
     settextstyle(0,0,0); // f,d,s

if(flag)
{
  x=left+20;
  y=top+60;
  setfillstyle(1,13);
  bar(left,top,left+217,top+90);
  setcolor(5);
  outtextxy(left+52,top+10,"Player's Name:");
  line(x,y+14,x+180,y+14);
  while(degel)
    {
      l=-1;
      ch=getch();
      if(ch>=65&&ch<=90 || ch>=97&&ch<=122 && mone<15)
    {
       l++;
       temp[l]=ch;
       temp[++l]='\0';
       strcat(name,temp);
       outtextxy(x,y,temp);
       mone++;
       x+=8;
       for(index=0;index<5;index++)
        {
        if(temp[l]==admin[index])
           {
            mone_admin++;
           }
        if(mone_admin>4)
           {
            setcolor(0);
            setfillstyle(0,0);
            bar(left,top,left+217,top+90);
            outtextxy(left+52,top+10,"Player's Name:");
            line(x,y+14,x+180,y+14);
            setcolor(4);
            outtextxy(240,20,"Welcome, Oren");
            degel=0;
           }
        }
    }
      else if(ch==8 && mone>0)
         {
         x-=8;
         bar(x,y,x+10,y+13);
         mone--;
         name[mone]='\0';
         }
       else if(ch==13 && mone>0)
           {
            setcolor(0);
            setfillstyle(0,0);
            bar(left,top,left+217,top+90);
            outtextxy(left+52,top+10,"Player's Name:");
            line(x,y+14,x+180,y+14);
            setcolor(5);
            outtextxy(240,20,"Welcome,");
            outtextxy(313,20,name);
            degel=0;
           }
    } // while
} // if
else
     {
       outtextxy(240,20,"Welcome,");
       outtextxy(313,20,name);
     }

     setcolor(9); // light blue
     settextstyle(4,0,4); // f,d,s
     outtextxy(205,70,"THE GAME");
     setcolor(9);
     circle(293,199,10); // stick head
     line(293,209,293,232); // stick body
     line(314,210,294,220); // stick right arm
     line(271,210,292,220); // stick left arm
     line(293,233,300,252); // stick right leg
     line(293,233,286,252); // stick left leg
     /* N-heb */
     line(308-2,276-17,308-2,286-17);
     line(303-2,286-17,307-2,286-17);
     /* G-heb */
     line(298-2,276-17,298-2,282-17);
     line(292-2,282-17,298-2,282-17);
     line(292-2,282-17,292-2,286-17);
     line(292-2,286-17,298-2,286-17);
     /* D-heb */
     line(286-2,277-17,286-2,286-17);
     line(279-2,277-17,288-2,277-17);
     line(260,270,260,304); // (1) digget one
     circle(284,290,13); // (0) first digget zero
     circle(317,290,13); // (0) second digget zero
     setcolor(1);
     line(248,255,339,255); // seperate line
     rectangle(247,185,339,320); // box
     setcolor(5);
     settextstyle(4,0,4); // f,d,s
     outtextxy(246,325,"Practice");

     for(i=0;i<places;i++) DrawButton(array[i]);
     mouse_cursor_on();
     while(1)
    {
       for (i=0;i<places;i++)
          {
         read_mouse(&c,&d,&status);
         if ((c>=array[i].x1)&&(c<=array[i].x2)&&(d>=array[i].y1)&&(d<=array[i].y2)&&(status&0x01))
            {
               sound(250);
               delay(50);
               nosound();
               if (BPos(array[i])==0) // if button is pressed
              {
                 array[i].pos=1;
                 DrawButton(array[i]);
                 delay(200); // illusion of button being pressed
                 array[i].pos=0;
                 DrawButton(array[i]);
                 if (i==0) // Music
                {
                   mouse_cursor_off();
                   clearviewport();
                   free(array);
                   music(name,0);
                }

                 if (i==1) // Sports
                {
                   mouse_cursor_off();
                   clearviewport();
                   free(array);
                   sports(name,0);
                }

                 if (i==2) // Technology
                {
                   mouse_cursor_off();
                   clearviewport();
                   free(array);
                   tech(name,0);
                }

                 if (i==3) // Entertainment
                {
                   mouse_cursor_off();
                   clearviewport();
                   free(array);
                   enter(name,0);
                }

                 if (i==4) // Quit Game
                {
                   closegraph();
                   free(array);
                   sound(250);
                   delay(4);
                   nosound();
                   exit(1);
                }

                 if (i==5) // Main Menu
                {
                   mouse_cursor_off();
                   clearviewport();
                   free(array);
                   menu();
                }

                 if (i==6) // 1vs100
                {
                   if(Pflag)
                   {
                     mouse_cursor_off();
                     clearviewport();
                     free(array);
                     game_mat(name);
                   }
                   else
                   {
                     setcolor(4);
                     settextstyle(0,0,0);
                     outtextxy(235,164,"-ACCESS DENIED-");
                     delay(1000);
                     outtextxy(172,175,"Must pass 1 category of practice");
                     delay(2850);
                     setcolor(0);
                     outtextxy(235,164,"-ACCESS DENIED-");
                     outtextxy(172,175,"Must pass 1 category of practice");
                   }
                }
              } // if

               if (BPos(array[i])==1)
              {
                 array[i].pos=0;
                 DrawButton(array[i]);
                 if (i==2)
                {
                   closegraph();
                   free(array);
                   sound(250);
                   delay(4);
                   nosound();
                   exit(1);
                } // if
                break;
              } // if
            } // if
          } // for
    } // while
} // game func




game.h
CODE
typedef struct
{
char question[50];
char ans1[20];
char ans2[20];
char ans3[20];
int right_ans;
}question;

void game1(int flag,char * name,int Pflag);




high score page(high.cpp), has the getch(); code:
CODE
#include <graphics.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <dos.h>
#include "d:\1vs100\high.h"
#include "d:\1vs100\menu.h"
#include "d:\1vs100\screens.h"

list *head=NULL;

void high()
{
  int color;

     setcolor(9); // light blue
     settextstyle(4,0,4); // f,d,s
     outtextxy(224,15,"High Scores");
     setfillstyle(8,9); // s,c
     bar(15,480,0,0); // left frame
     bar(0,15,640,0); // up frame
     bar(623,480,640,0); // right frame
     bar(0,480,640,465); // down frame
     settextstyle(0,0,0); // f,d,s
     h_main();
     randomize();
     color = ( color + rand() ) %15 +1;
     setcolor(color);
     delay(800);
     outtextxy(215,450,"Press any key to return");
     delay(800);
     setcolor(0);
     outtextxy(215,450,"Press any key to return");
     flushall();

    while (!kbhit())
    {
     color = ( color + rand() ) %15 +1;
     setcolor(color);
     delay(800);
     outtextxy(215,450,"Press any key to return");
     delay(800);
     setcolor(0);
     outtextxy(215,450,"Press any key to return");
    }
       flushall();
       clearviewport();
       menu();



} // high func

void addItem(list **headPtr,rec data)
{
  list *ptr;
  ptr=(list *)malloc(sizeof(list)); // 1*sizeof
  if(ptr)
    {
    if (*headPtr==NULL)
    {
        ptr->record=data;
        ptr->next=NULL;
        *headPtr=ptr;
    }
    else
    {
        list *temp=*headPtr;
        while(temp->next)
            temp=temp->next;
        ptr->record=data;
        ptr->next=NULL;
        temp->next=ptr;
    }
    } // if ptr
} // addItem func

void printList(list *headPtr)
{
  rec data;
  list *temp=headPtr;

  while(temp)
    {
    data=temp->record;
    setcolor(11);
    outtextxy(205,110,data.name);
    outtextxy(340,110,data.nikud);
    temp=temp->next;
    } // while
  } // printList func

void addInsertSort(list **ptr,rec data)
{
  list *temp=(list *)malloc(sizeof(list));
  if(temp)
    {
    temp->record=data;
    temp->next=*ptr;
    *ptr=temp;
    }
} // addInsertSort func

void insertSort(list **headPtr,rec data)
{
  list *temp,*ptr;
  temp=*headPtr;
  while((temp)&&(temp->record.nikud>data.nikud))
    {
    ptr=temp;
    temp=temp->next;
    }
  if(ptr==*headPtr)
    addInsertSort(headPtr,data);
  else
    addInsertSort(&(ptr->next),data);
} // InsertSort func

void h_main()
{
  rec data;
  strcpy(data.name,"OrEn");
  strcpy(data.nikud,"1,000,000");
  addItem(&head,data);
  printList(head);
} // h_main func




high.h
CODE
typedef struct
{
  char name[25];
  char nikud[10];
}rec;

typedef struct list
{
  rec record;
  struct list *next;
}list;


void high();
void addItem();
void printList();
void addInsertSort();
void insertSort();
void h_main();





instructions page(ins.cpp), has the getch(); code as well:
CODE
#include <graphics.h>
#include <string.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include "d:\1vs100\screens.h"
#include "d:\1vs100\menu.h"

void ins()
{
  int i,color,x=100,y=59;
  char s[81],ch;

     flushall();
     setcolor(9); // light blue
     settextstyle(4,0,4); // f,d,s
     outtextxy(240,15,"Instructions");
     setfillstyle(8,9); // s,c
     bar(15,480,0,0); // left frame
     bar(0,15,640,0); // up frame
     bar(623,480,640,0); // right frame
     bar(0,480,640,465); // down frame
     setcolor(9);
     settextstyle(3,0,1); // f,d,s

     FILE*fp;
     fp=fopen("d:\\1vs100\\ins.txt","rt");
     if(fp)
     {
    while(fgets(s,80,fp))
    {
       i=0;
       while(s[i]=='\t')
        i++;
       strcpy(s,s+i);
       s[strlen(s)-1]='\0';
       outtextxy(x,y,s);
       y+=23;
    }
     }
     fclose(fp);

     settextstyle(0,0,0); // f,d,s
     randomize();
     while(1)
     {
     color = ( color + rand() ) %15 +1;
     setcolor(color);
     delay(800);
     outtextxy(215,450,"Press any key to return");
     delay(800);
     setcolor(0);
     outtextxy(215,450,"Press any key to return");

     if(kbhit())
       {
        flushall();
        clearviewport();
        menu();
       }

     }
} // ins func




ins.h
CODE
void ins();



Thanks a lot in advance.

This post has been edited by ISAI: 5 Mar, 2008 - 11:47 PM
User is offlineProfile CardPM
+Quote Post

ISAI
RE: Problem With Char Buffer Command In My Project
12 Mar, 2008 - 01:48 AM
Post #2

New D.I.C Head
*

Joined: 26 Dec, 2007
Posts: 41


My Contributions
someone?
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Problem With Char Buffer Command In My Project
12 Mar, 2008 - 01:51 AM
Post #3

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,465



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
It's 5:30 am EST here in the US, a lot of the members are currently sleeping... please be patient & someone will help you as best as they can.
User is online!Profile CardPM
+Quote Post

ISAI
RE: Problem With Char Buffer Command In My Project
12 Mar, 2008 - 06:20 AM
Post #4

New D.I.C Head
*

Joined: 26 Dec, 2007
Posts: 41


My Contributions
QUOTE(no2pencil @ 12 Mar, 2008 - 02:51 AM) *

It's 5:30 am EST here in the US, a lot of the members are currently sleeping... please be patient & someone will help you as best as they can.


It's been 6 days since i posted my topic, and just now I spoke up.
I'm far from not being patient.
User is offlineProfile CardPM
+Quote Post

ISAI
RE: Problem With Char Buffer Command In My Project
14 Mar, 2008 - 05:25 AM
Post #5

New D.I.C Head
*

Joined: 26 Dec, 2007
Posts: 41


My Contributions
please help =/
User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Problem With Char Buffer Command In My Project
14 Mar, 2008 - 06:45 AM
Post #6

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
Did you try using getchar();
User is offlineProfile CardPM
+Quote Post

ISAI
RE: Problem With Char Buffer Command In My Project
16 Mar, 2008 - 04:09 AM
Post #7

New D.I.C Head
*

Joined: 26 Dec, 2007
Posts: 41


My Contributions
QUOTE(letthecolorsrumble @ 14 Mar, 2008 - 07:45 AM) *

Did you try using getchar();


What's the difference between getchar(); and getch(); ?
And where do I use it? instead of the entire getch(); code I have?

Ty.
User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Problem With Char Buffer Command In My Project
16 Mar, 2008 - 04:47 AM
Post #8

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
Maybe I have not understood your entire program. Your program has many self-defined header files, that I do not have access to, so to solve your problem, I have to have a look at those. If you could attach them, I will give it a try and check what is going wrong. Also, a better detailed explanation of your task would help a lot. :)
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 06:51PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month