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

Posted on 2004-10-26
Last Modified: 2006-11-17
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;

Question by:Rhyox
    LVL 2

    Expert Comment

    What is not working in your program?

    Author Comment

    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?
    LVL 16

    Accepted Solution

    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'.
    LVL 2

    Expert Comment

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

    Expert Comment

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

    LVL 2

    Expert Comment

    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?

    Author Comment

    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
    LVL 2

    Expert Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    The Complete Ruby on Rails Developer Course

    Ruby on Rails is one of the most popular web development frameworks, and a useful tool used by both startups and more established companies to build strong graphic user interfaces, and responsive websites and apps.

    Suggested Solutions

    Title # Comments Views Activity
    why to use string 10 47
    Hibernate methods 2 34
    java continue statement 10 33
    sumDigits  challenge 7 26
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    857 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now