A Digital Age Deserves A Digital Leader

C++ Bug in program

C++ Bug in program

Postby coreyw2000 » Wed Apr 13, 2005 3:33 pm

This program is not working... it doesnt show anything on the screen, then it goes in an endless loop. This is a class assignment, and the tyeacher is stumped. Any ideas?

Code: Select all
#include <iostream.h>
#include <conio.h>
#include <lvp\random.h>
#include <windows.h>

//-----------------------------------------------

void gotoxy(int xpos, int ypos)
/* requires the use of the library windows.h */
{
  COORD scrn;   

  HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);

  scrn.X = xpos; scrn.Y = ypos;

  SetConsoleCursorPosition(hOuput,scrn);
}

//-----------------------------------------------

int main()

{
   // Randomize();
   cout << "Doodler!Press I/J/K/M to move, Q to quit\n";
   char KeyPressed;
   int x = 40; int y = 10;
   do
   {
      gotoxy(x, y);
      cout << '*';
      gotoxy(x, y);

      KeyPressed = getch();
      
      if (KeyPressed == 'I' || KeyPressed == 'i')
         y--;
      else if (KeyPressed == 'M' || KeyPressed == 'm')
         y++;
      else if (KeyPressed == 'J' || KeyPressed == 'j')
         x--;
      else if (KeyPressed == 'K' || KeyPressed == 'k')
         x++;
      else if (KeyPressed == 'Q' || KeyPressed == 'q')
         ;
      else
         cout << "\a";
   } while ((KeyPressed != 'Q') && (KeyPressed != 'q'));


   gotoxy(1, 1);
   system("cls");
   cout << "Random stars! Press any key to stop.\n";
   while (!kbhit())
   {
      gotoxy(1 + random (6), 2 + random(20));
      cout << "*";
   }

   return(0);
PROfessional Member
Posts: 4644
Joined: Tue Oct 05, 2004 5:30 am
Location: Saskatchewan, Canada

Postby glexp » Wed Apr 13, 2005 9:04 pm

cout << '*';
If cout is expecting a string, then this may be your problem. Single quotes generates a single character with no null-termination. Double quotes generates a null teminated array. Again, if cout is expecting a string (i.e. null-terminated array) then using single quotes will not guarantee a null-termination and the program will continue to output characters to cout until it finds a null.
So, instead of cout << '*';, try cout << "*";

Also, instead of using a bunch of if..else statements, it may be more readable to use a switch statement in the do..while loop.:
switch( getch() )
{
case 'I':
case 'i':
y--;
break;

case 'J':
case 'j':
x--;
break;
|
|
|
default:
cout << "\a";
}

-GP
What, me worry?

- Luck is the residue of proper planning...
PROfessional Member
User avatar
Posts: 364
Joined: Sun Jun 30, 2002 6:33 am
Location: California

Postby Wound » Thu Apr 14, 2005 7:21 pm

well, using my magic visual C++, and after commenting out your <lvp\random.h> and looping down the bottom it worked fine. Post the contents of your random.h and I'll have another look
PRO New Member
User avatar
Posts: 2
Joined: Wed Apr 13, 2005 9:08 am
Location: Bristol, UK

Return to HTML, CSS, and Scripts

Who is online

Users browsing this forum: No registered users and 2 guests