Need help w/ random library.

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.

Original code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream> 
#include <conio.h>
#include <lvp\random.h>

using namespace 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--;
        else if (KeyPressed == 'M' || KeyPressed == 'm')
           y++;
        else if (KeyPressed == 'J' || KeyPressed == 'j')
           x--;
        else if (KeyPressed == 'K' || KeyPressed == 'k')
           x++;
        else if (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!
The function randomize(), the variable random, gotoxy() aren't declared, and you're missing a semicolon.

You have to create and define these functions yourself. Don't just copy and paste and expect it to work.
@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";
Oh, duh, I didn't know a function like that even existed. My bad.
@Danny Toledo

Thanks, I'll see if I can clean it up. Sound like I need to replace the non-standard libraries with something else. Sounds okay. Thank you for help!
Topic archived. No new replies allowed.