Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-26
8
Medium Priority
?
298 Views
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")
                  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;

}
0
Comment
Question by:Rhyox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 2

Expert Comment

by:tdisessa
ID: 12410940
What is not working in your program?
0
 

Author Comment

by:Rhyox
ID: 12410998
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
 
LVL 16

Accepted Solution

by:
imladris earned 200 total points
ID: 12411502
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:tdisessa
ID: 12411673
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
 
LVL 11

Expert Comment

by:griessh
ID: 12412195
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
 
LVL 2

Expert Comment

by:EagleEye1975
ID: 12414867
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
 

Author Comment

by:Rhyox
ID: 12421722
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
 
LVL 2

Expert Comment

by:tdisessa
ID: 12422148
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
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 …
Introduction to Processes

618 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