Hey, I came across a program in my textbook that allows you to make a doodle program. It's an old book, so I am assuming some things are outdated. So I copied it and got a bunch of errors I can't seem to figure out. I'd really appreciate if someone could help me fix this program into more modern C++. Thank you.
#include <iostream>
#include <conio.h>
#include <lvp\random.h>
usingnamespace std;
int main()
{
randomize();
cout << "Doodler! Press I/J/K/M to move, Q to quit" << endl;
char KeyPressed; // Key Pressed by user
int x = 40; int y = 10; //establish initial position of cursor
do{
//plot a point
gotoxy(x, y);
cout << '*';
gotoxy(x, y); // move blinking cursor under current spot
KeyPressed = getch(); // Get a key, not echoing to screen
if (KeyPressed == 'I' || KeyPressed == 'i')
y--;
elseif (KeyPressed == 'M' || KeyPressed == 'm')
y++;
elseif (KeyPressed == 'J' || KeyPressed == 'j')
x--;
elseif (KeyPressed == 'K' || KeyPressed == 'k')
x++;
elseif (KeyPressed == 'Q' || KeyPressed == 'q')
; //do nothing
else
cout "\a"; //Beep for bad keystroke
}while((KeyPressed != 'Q' %% KeyPressed != 'q'));
gotoxy(1,1); //clear and display new title
clreol();
cout << "Random Stars! Press any key to stop." << endl;
while (!kbhit()){
gotoxy(1+random(60), 2+random(20));
cout << "*"; // A star and a blank to draw AND erase
}
return 0;
}
And here are the errors I get:
1 2 3 4 5 6 7 8
3 H:\C++ Projects\doodle.cpp lvp\random.h: No such file or directory.
H:\C++ Projects\doodle.cpp In function `int main()':
8 H:\C++ Projects\doodle.cpp `randomize' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
15 H:\C++ Projects\doodle.cpp `gotoxy' undeclared (first use this function)
31 H:\C++ Projects\doodle.cpp expected `;' before string constant
38 H:\C++ Projects\doodle.cpp `random' undeclared (first use this function)
Sorry if it's long, but I want to understand what is wrong with this program. Thank you!
@Lumpkin
I think randomize is supposed to be in random.h (the #include on line 3 that fails according to the first error message). And iirc, gotoxy is sometimes defined in conio.h. Also, instead of reiterating the errors, why not try to identify why the errors are happening (hint, the OP isn't actually missing a semicolon on line 31).
@OP
From a quick search of Google (that sounds weird), it appears that lvp/random.h is obsolete (the only information I can find says that people are using it with old Borland compilers). That, and both random.h and conio.h are non-standard (read: best avoided).
Finally, you are missing the << operator one line 31: cout/*<<*/ "\a";