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")
            else if (KeyPressed == "K"||KeyPressed == "k")
            else if (KeyPressed == "J"||KeyPressed == "j")
            else if (KeyPressed == "L"||KeyPressed == "l")
            else if (KeyPressed == "Q"||KeyPressed == "q")
                  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.";
            while (!kbhit()) {
                  int random_integer;
                  random_integer = (rand()%60)+1;
                  int random_integer2;
                  random_integer2 = (rand()%25)+1;
                  gotoxy(random_integer, random_integer2);
                  cout << "*";

      return 0;

Who is Participating?
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;


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

etc. Note the single quotes for *character* 'I'.
What is not working in your program?
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?
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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).
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).

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?
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
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.