Rhyox
asked on
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_HA NDLE);
SetConsoleCursorPosition(h ConsoleOut put, 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;
}
#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_HA
SetConsoleCursorPosition(h
}
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;
}
What is not working in your program?
ASKER
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 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).
======
Werner
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
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?
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?
ASKER
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
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