How do I make the srand code run infinitely the stop with getch?

A guy helped me with termios.h and getch on linuxquestions to do this and I was very impressed, now what I want to find out is how do I make the random number code loop infinitey when 1 is pressed and exit the loop and the program when 2 is pressed? At the moment the random number code runs once every time 1 is pressed and that is great but I want it to keep repeating until 2 is pressed, how do I do this? thanks.

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
43
44
45
46
47
48
 #include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <ctime>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>

struct termios termios_saved;

void restore_termios()
{
    tcsetattr(0, TCSAFLUSH, &termios_saved);
}

int main(int argc, char *argv[])
{
    struct termios my_termios_settings;
    int c;

    // save original termios values and setup an atexit routine
    // to ensure they are restored on exit:
    if ( tcgetattr(0, &termios_saved) == 0 )
        atexit(restore_termios);
    else
        exit(EXIT_FAILURE);

    // turn off buffered input and local echo:
    my_termios_settings = termios_saved;
    my_termios_settings.c_lflag &= ~(ICANON|ECHO);
    tcsetattr(0, TCSAFLUSH, &my_termios_settings);


    // get one character at a time until someone hits 'q' or EOF

    puts("Press (1)For a stream of random numbers. Press (2) to quit.");
    while ( (c = getchar()) != 004  &&  c != '2' && c == '1' )
        	{
					srand (static_cast <unsigned> (time(0)));
					float r = static_cast <float> (rand() / static_cast <float> (RAND_MAX));
					printf("This is the random number: %f\n",r);
			}
			
    return 0;
}
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#define _POSIX_C_SOURCE 199309L
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <termios.h>
#include <sys/ioctl.h>

struct termios termios_saved;

void restore_termios()
{
    tcsetattr(0, TCSAFLUSH, &termios_saved);
}

void set_termios()
{
    struct termios my_termios_settings;
    if (tcgetattr(0, &termios_saved) == 0)
        atexit(restore_termios);
    else
        exit(EXIT_FAILURE);
    my_termios_settings = termios_saved;
    my_termios_settings.c_lflag &= ~(ICANON|ECHO);
    tcsetattr(0, TCSAFLUSH, &my_termios_settings);
}

int kbhit()
{
    struct termios term;
    tcgetattr(0, &term);
    struct termios term2 = term;
    term2.c_lflag &= ~ICANON;
    tcsetattr(0, TCSANOW, &term2);
    int byteswaiting;
    ioctl(0, FIONREAD, &byteswaiting);
    tcsetattr(0, TCSANOW, &term);
    return byteswaiting > 0;
}

int main()
{
    set_termios();
    srand(time(0));
    struct timespec ts = {0, 100000000}; // 1/10 second

    int c = 0;
    while (c != '1' && c != '2')
    {
        puts("Press (1) for a stream of random numbers. Press (2) to quit.");
        c = getchar();
    }

    while (c == '1')
    {
        float r = rand() / (float)RAND_MAX;
        printf("%f\n", r);
        nanosleep(&ts, NULL);
        if (kbhit() && getchar() == '2')
            c = '2';
    }
            
    return 0;
}

Can you talk me through this line by line as I cant find a tutorial on termios.h anywhere
Topic archived. No new replies allowed.