Can this happen simultaneously?

Hello

I put this code together for getting characters to flash on a grid:

#include <iostream>
#include <windows.h>

using namespace std;

void printarray(char);
void gotoxy (int x, int y);

int main()
{
int num = 10;
int q,w,e,r;
cout<<"This exercise contains a 10 by 10 grid and four fixation targets.\n";
cout<<"Please enter the target flashing frequencies(range from 1-70Hz).\n";
cout<<"Target 1: "; cin>>q; cout<<"Target 2: "; cin>>w;
cout<<"Target 3: "; cin>>e; cout<<"Target 4: "; cin>>r;
cout<<"When you are ready, \n";
system("pause");

printarray(num);

while (true)
{
gotoxy(2,8); // move to where we want to output
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
cout << "x"; // overwrite the current output
Sleep((1000/q)/2);
gotoxy(2,8); // move back to the start of output
cout << " "; // this will reset the output to blank
Sleep((1000/q)/2);

gotoxy(56,8); // move to where we want to output
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
cout << "x"; // overwrite the current output
Sleep((1000/w)/2);
gotoxy(56,8); // move back to the start of output
cout << " "; // this will reset the output to blank
Sleep((1000/w)/2);

gotoxy(2,35); // move to where we want to output
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
cout << "x"; // overwrite the current output
Sleep((1000/e)/2);
gotoxy(2,35); // move back to the start of output
cout << " "; // this will reset the output to blank
Sleep((1000/e)/2);

gotoxy(56,35); // move to where we want to output
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
cout << "x"; // overwrite the current output
Sleep((1000/r)/2);
gotoxy(56,35); // move back to the start of output
cout << " "; // this will reset the output to blank
Sleep((1000/r)/2);

}cin.get();

system("pause");
return 0;
}

void printarray(char n)
{
char array[n][n];

for(int x=0, i=1; x<n; x++, i++)
{
for(int y=0; y<n; y++)
{
array[x][y]=' '; //assigning space ' ' to all elements of matrix
}
//array[0][0] ='x';
//array[0][n-1] ='x';
//array[n-1][0] ='x';
//array[n-1][n-1] ='x';
}
for(int y=0; y<n; y++)
{
for(int x=0; x<n; x++)
{
cout<<"|_"<<array[x][y]<<"_| ";
}
cout<<endl<<endl<<endl;
}
}

void gotoxy (int x, int y)
{
COORD coord; // coordinates
coord.X = x; coord.Y = y; // X and Y coordinates
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); // moves to the coordinates
}

I see that since I have these coordinates placed within the while loop, the process will happen in that order. I there something I can do to make these characters flash simultaneously if I put in the same number?

I don't know how to discuss what you want without going into advanced topics, Like Multi-threading. Basically it would be one thread to Display the data continuously making it flash. The other thread handle user input.

In Gui programming the flashing could be handled timer messages set to an interval, update the display area and continue. The User messages would handle user input which could be entered at any time.

The flashing of text was a trait of older displays and has been phased out on many new systems. New implementation structures would have to be found, considering on the old systems it was a flag to set in ANSI character codes.
You should only sleep once, at the end of your loop, and use variables to keep track of the time each letter is (in)visible. Also, note that your program could be greatly simplified using arrays. As Azagaros said, updating the screen and getting user input are usually done by different threads, but for something this simple there are ways you can do both inside a single loop (using kbhit/getch, GetAsyncKeyState etc... for input).

HINT: [code]your code[/code] -> your 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <windows.h>

using namespace std;

void printarray(char);
void gotoxy (int x, int y);

int main()
{
    int num = 10;
    int q,w,e,r;
    cout<<"This exercise contains a 10 by 10 grid and four fixation targets.\n";
    cout<<"Please enter the target flashing periods(in ms, preferably, multiples of 25).\n";
    cout<<"Target 1: "; cin>>q; cout<<"Target 2: "; cin>>w;
    cout<<"Target 3: "; cin>>e; cout<<"Target 4: "; cin>>r;
    cout<<"When you are ready, \n";
    system("pause");

    printarray(num);

    int qt=0, wt=0, et=0, rt=0;

    while (true)
    {
        if (qt<q)
        {
            gotoxy(2,8); // move to where we want to output
            cout << "q"; // overwrite the current output
        }
        else
        {
            gotoxy(2,8); // move back to the start of output
            cout << " "; // this will reset the output to blank
        }

        if (wt<w)
        {
            gotoxy(56,8); // move to where we want to output
            cout << "w"; // overwrite the current output
        }
        else
        {
            gotoxy(56,8); // move back to the start of output
            cout << " "; // this will reset the output to blank
        }

        if (et<e)
        {
            gotoxy(2,35); // move to where we want to output
            cout << "e"; // overwrite the current output
        }
        else
        {
            gotoxy(2,35); // move back to the start of output
            cout << " "; // this will reset the output to blank
        }

        if (rt<r)
        {
            gotoxy(56,35); // move to where we want to output
            cout << "r"; // overwrite the current output
        }
        else
        {
            gotoxy(56,35); // move back to the start of output
            cout << " "; // this will reset the output to blank
        }

        qt+=25; if (qt>2*q) qt=0;
        wt+=25; if (wt>2*w) wt=0;
        et+=25; if (et>2*e) et=0;
        rt+=25; if (rt>2*r) rt=0;

        Sleep(25);

    }cin.get();

    system("pause");
    return 0;
}

void printarray(char n)
{
    char array[n][n];

    for(int x=0, i=1; x<n; x++, i++)
    {
        for(int y=0; y<n; y++)
        {
            array[x][y]=' '; //assigning space ' ' to all elements of matrix
        }
    }
    for(int y=0; y<n; y++)
    {
        for(int x=0; x<n; x++)
        {
            cout<<"|_"<<array[x][y]<<"_| ";
        }
        cout<<endl<<endl<<endl;
    }
}

void gotoxy (int x, int y)
{
    COORD coord; // coordinates
    coord.X = x; coord.Y = y; // X and Y coordinates
    SetConsoleCursorPosition(
        GetStdHandle(STD_OUTPUT_HANDLE), coord); // moves to the coordinates
}

Last edited on
Azagaros and M4ster r0shi, I thank you greatly for your help. Like Azagaros said multi threading is the direction I need to take my code and M4ster r0shi has supplied me with what I need to keep moving forward with my work. I will keep working to gain more experience with C++
Topic archived. No new replies allowed.