Can i do something in a loop only once?

Okay this is my code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

void z() {Sleep(500);}

char x[10] = {1,2,1,2,1,2,1,2,1,2};
char y[10] = {2,1,2,1,2,1,2,1,2,1};
int main(){
    for (int i=0;i<10;i++)
    {
        cout << x[i];
        cout << '\b'; 
        cout << endl; // I only want this done once.
        cout << y[i];
        cout << '\b';
        z();
    }
getch();
return 0;
}

The problem is that at line 16. It does the endl every single time it loops. I only want it done once. How would I fix this?
Last edited on
closed account (Dy7SLyTq)
put this in the loop:
1
2
if(i == 0)
     cout<< endl
Thanks for the help DTSCode but I've run into a new problem. My goal is to make the ☺ and ☻s blink together. But for some reason they have to take turns. Copy this code and run it to see what I mean. Help would be appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

void z() {Sleep(500);}

char x[10] = {1,2,1,2,1,2,1,2,1,2};
char y[10] = {2,1,2,1,2,1,2,1,2,1};
int main(){
    for (int i=0;i<10;i++)
    {
        cout << x[i];
        cout << '\b';
        cout << y[i];
        cout << '\b';
        if (i == 5)
        cout << endl;
        z();
    }
getch();
return 0;
}
closed account (Dy7SLyTq)
cout<< x[i] << y[i];
cout<< '\b' << '\b';
The top and bottom still have to take turns. I want them to be blinking at the same time like this:
1
2
blink 1: ☺ blink 2: ☻ blink 3:☺
         ☻          ☺         ☻

and so on.
Last edited on
Impossible to do with standard C++, but since you're already using Windows specific 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
#include <iostream>
#include <windows.h>

using namespace std;

void z() 
{ 
    Sleep(500); 
}

void homeCursor(HANDLE h)
{
    COORD home = { 0, 0 };
    SetConsoleCursorPosition(h, home);
}

int main()
{
    HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);

    const char a [] = { '\1', '\2', '\0' };
    const char b [] = { '\2', '\1', '\0' };
    const char* row [] = { a, b };

    for (int i = 0; i < 10; i++)
    {
        homeCursor(console);

        std::cout << row[i % 2] << '\n' << row[1 - (i % 2)] << flush;

        z();
    }
}
It works perfectly! But I want to know how to add more rows of ☺s if necessary. Can you rewrite the code with one more line of ☺s so I can see how you changed it to add more? Thanks for the help.
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
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <windows.h>

using namespace std;

void z()
{
    Sleep(500);
}

void homeCursor(HANDLE h)
{
    COORD home = { 0, 0 };
    SetConsoleCursorPosition(h, home);
}

int main()
{
    HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
    std::srand((unsigned) std::time(0));

    const char a [] = { '\1', '\2', '\0' };
    const char b [] = { '\2', '\1', '\0' };
    const char* row [] = { a, b };

    const unsigned rows = std::rand() % 10 + 2;


    for (unsigned i = 0; i < 10; i++)
    {
        homeCursor(console);

        if (i % 2)
        {
            for (unsigned j = 0; j < rows; ++j)
                std::cout << row[j % 2] << '\n';
        }
        else
        {
            for (unsigned j = 0; j < rows; ++j)
                std::cout << row[1 - (j % 2)] << '\n';
        }
            
        std::cout << flush;

        z();
    }
}
How did you do that?
How did you do that?

Do what? The code is right there.
closed account (Dy7SLyTq)
actually... cires magic
No, I run the code several times without changing it...and there's different outputs! Sometimes it will make 2 lines sometimes it will make 10 lines. Its the same code so it should do the same thing every time...right?
Last edited on
closed account (Dy7SLyTq)
Its the same code so it should do the same thing every time...right?

no that would be boring. thats why at line 28 he wrote
const unsigned rows = std::rand() % 10 + 2;
which set the upper bound for the loop
I fixed the problem and made it boring! Thanks for the help!
Topic archived. No new replies allowed.