0

I'm programming a simple program to randomly generate letters and output them to the screen on repeat till you close the window. The program seems to somewhat work but other errors aside I attempted to ad in the delay() function with the dos.h library at the beginning of the loop so the program would not work at such high speed. Despite having included the dos.h library I always get Error code E0020: Identifier "delay" is undefined.

//Include neccessary libraries 
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <dos.h>

//Only neccessary variable
int     alphanum;

//Main program
int main () {
    //Begin loop
    while (1 > 0) {
        delay(100); //Line where delay error ocuurs
        //Generate random number between 1-26
        srand(time(NULL));
        alphanum = (rand() % 26) + 1;
        //Depending on what number is generated, the corresponding letter will be output
        if (alphanum == 1) {
            std::cout << "A";
        }
        if (alphanum == 2) {
            std::cout << "B";
        }
        if (alphanum == 3) {
            std::cout << "C";
        }
        if (alphanum == 4) {
            std::cout << "D";
        }
        if (alphanum == 5) {
            std::cout << "E";
        }
        if (alphanum == 6) {
            std::cout << "F";
        }
        if (alphanum == 7) {
            std::cout << "G";
        }
        if (alphanum == 8) {
            std::cout << "H";
        }
        if (alphanum == 9) {
            std::cout << "I";
        }
        if (alphanum == 10) {
            std::cout << "J";
        }
        if (alphanum == 11) {
            std::cout << "K";
        }
        if (alphanum == 12) {
            std::cout << "L";
        }
        if (alphanum == 13) {
            std::cout << "M";
        }
        if (alphanum == 14) {
            std::cout << "N";
        }
        if (alphanum == 15) {
            std::cout << "O";
        }
        if (alphanum == 16) {
            std::cout << "P";
        }
        if (alphanum == 17) {
            std::cout << "Q";
        }
        if (alphanum == 19) {
            std::cout << "R";
        }
        if (alphanum == 20) {
            std::cout << "S";
        }
        if (alphanum == 21) {
            std::cout << "T";
        }
        if (alphanum == 22) {
            std::cout << "U";
        }
        if (alphanum == 23) {
            std::cout << "V";
        }
        if (alphanum == 24) {
            std::cout << "X";
        }
        if (alphanum == 25) {
            std::cout << "Y";
        }
        if (alphanum == 26) {
            std::cout << "Z";
        }
    }
}

SOLVED: I found that delay() cannot work in code blocks and switched to Sleep() from the Windows.h library.

  • `srand(time(NULL));` should only be done _once_ during the program's execution. You now do it every iteration. Move it out of the loop and do it as the first thing in `main`. It would be even better to use [``](https://en.cppreference.com/w/cpp/header/random) – Ted Lyngmo Apr 14 '20 at 18:49
  • 1
    use this instead: https://stackoverflow.com/questions/4184468/sleep-for-milliseconds – 463035818_is_not_an_ai Apr 14 '20 at 18:55
  • *Despite having included the dos.h library* -- Even if you had the `dos.h` header and the file compiled, you have another hurdle -- linking. The runtime library of your compiler has to know what `delay` is. – PaulMcKenzie Apr 14 '20 at 18:56
  • `#include ` - that's some *seriously antiquated* system you're working with there. Damn. Maybe upgrade to the current millennium? – Jesper Juhl Apr 14 '20 at 18:56
  • 1
    You dont have to / should not edit your question to add "SOLVED", thats not for questions. Answers go to answers and if your question turned out to be a non-question you can delete it – 463035818_is_not_an_ai Apr 14 '20 at 18:57
  • this has been flagged as duplicate, so you don't have to do anything. Maybe reload the page to see that the system already marked the question as being a duplicate – 463035818_is_not_an_ai Apr 14 '20 at 18:58
  • 2
    `Sleep()` is yet another non-portable way to sleep. Use `std::this_thread::sleep_for()` – Ted Lyngmo Apr 14 '20 at 18:58
  • Also, `const char *alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; ...if (alphanum >= 1 && alphanum <= 26) { std::cout << alpha[alphanum-1];` You don't need that endless set of `if` statements. – PaulMcKenzie Apr 14 '20 at 18:59
  • 2
    DOS is an operating system from the 1980s. If you see DOS stuff referenced in an example, at the very least you should stop and wonder how ing old the example is. – user4581301 Apr 14 '20 at 19:00
  • 1
    Then you get a lot of websites with viruses popping up, knowing that C++ newbies that are using old `C` examples will go to the infected site to download `dos.h` (since no recent compiler has this header). It isn't just an old header, it can be dangerous. – PaulMcKenzie Apr 14 '20 at 19:03
  • Danger... is my middle name. user danger 4581301 at your service. – user4581301 Apr 14 '20 at 19:06
  • If codeblocks supports c++14 you could look at [this](https://godbolt.org/z/pWWM_s) for a portable and better randomized version. – Ted Lyngmo Apr 14 '20 at 19:22

0 Answers0