Whats wrong with this code. I think its in the if statement.

I'm making a doodle program where you use I/J/K/L to move and it places an asterisk there. No errors come up when i debug it...heres the code.. just for reference im a senior in highschool that just started this in independent study, so the teacher doesn't even know what im doing....


#include "stdafx.h"
#include <conio.h>
#include <cstdlib>
#include <ctime>        //didnt know which ones i needed, so went with a bunch
#include <iostream>
#include <windows.h>
#using <mscorlib.dll>
using namespace std;
using namespace System;

void gotoxy(short x, short y) { //defines gotoxy
      HANDLE hConsoleOutput;
      COORD Cursor_Pos = {x, y};

      hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
      SetConsoleCursorPosition(hConsoleOutput, Cursor_Pos);
}
int _tmain()
{
      cout <<"Doodler. Press J/K/L/I to move and Q to quit." <<endl;
      char KeyPressed[1]; //key pressed by user
      int count = 0;
      int x = 40; int y = 10; //initial position of cursor.
      do {
            //plot a point
            gotoxy(x, y);
            cout <<"*";
            gotoxy(x, y);
      
            *KeyPressed = getch();//user input
            if (KeyPressed == "I"||KeyPressed == "i")
                  y--;
            else if (KeyPressed == "K"||KeyPressed == "k")
                  y++;
            else if (KeyPressed == "J"||KeyPressed == "j")
                  x--;
            else if (KeyPressed == "L"||KeyPressed == "l")
                  x++;
            else if (KeyPressed == "Q"||KeyPressed == "q")
                  ;
            else
                  cout <<"\a";
            }
            while ((KeyPressed != "Q")||(KeyPressed != "q"));

            gotoxy(1, 1);   //from here down it generates random stars, or suppose to
            void clrscr();
            cout <<"Random stars! press any key to stop." <<endl;
            cout <<"To start press any key.";
            getch();
      
            while (!kbhit()) {
                  //srand((unsigned)time(0));
                  int random_integer;
                  random_integer = (rand()%60)+1;
                  int random_integer2;
                  random_integer2 = (rand()%25)+1;
                  gotoxy(random_integer, random_integer2);
                  cout << "*";
            }

      return 0;

}
RhyoxAsked:
Who is Participating?
 
imladrisConnect With a Mentor Commented:
Looks like the problem is in the treatment of KeyPressed. It is declared as an array. Then at:

          *KeyPressed = getch();//user input

The first element of the array is initialized (KeyPress is an array, so KeyPress is the address of the array, so *KeyPress is the first element of the array), but then:

          if (KeyPressed == "I"||KeyPressed == "i")

you're comparing the address of the array to the address of the string "I". This will not come up with a useful result. I'm not sure why you went for the array complication. The straightforward declaration of KeyPressed as a char will work fine:

char KeyPress;

KeyPress=getch();

if(KeyPress=='I' || KeyPress=='i')

etc. Note the single quotes for *character* 'I'.
0
 
tdisessaCommented:
What is not working in your program?
0
 
RhyoxAuthor Commented:
When i run it, it will read the letter entered but nothing will happen to the asterisk, it will just go to the else in the if statement and make a beep... is something wrong with the if part?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
tdisessaCommented:
What he said ----^

And don't forget to change the comparison in your while statement, also.

(I would have said this earlier, but work got in the way.  Oh well).
0
 
griesshCommented:
and the
while ((KeyPressed != 'Q')||(KeyPressed != 'q'));

has to be changed to use && instead of || or you will never quit (One of these statements is always true).

======
Werner
0
 
EagleEye1975Commented:
Also, having the

else if (KeyPressed == "Q"||KeyPressed == "q")

is redundant.  There's no need to test for those keys being pressed within the "do-while", as that's being evaluated as the condition of the do-while continuing.  Notice you're doing nothing if that key is pressed, so why have it in there?
0
 
RhyoxAuthor Commented:
Thanks for your help, no way i could of found that problem without it. Its especially difficult because im trying to learn from a book thats from 1997 (my school is cheap).. thanks for all your efforts. Im sure ill be needing them again.  >8D
0
 
tdisessaCommented:
One of my favorite C books was published in 1988.  It is the original C Programming Language (2nd Edition)
by Brian W. Kernighan and Dennis Ritchie.  It has examples very similiar to waht you are doing here, but
it doesn't include the c++ stuff you are doing (like cout << ...).

It is ISBN: 0131103628
0
All Courses

From novice to tech pro — start learning today.