Cout's don't appear after adding a loop

Hello,
this is my first post on cplusplus.com
I'm making my first ever game, and I have a problem with one of my functions.
This is the function:

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
void skull(int score, int top_score)
{
    string tab[7] = {"Y   ", "O   ", "U   ", "   D", "   I", "   E", "   D"};
    for (int i = 0; i < 10; i++)
    {
        system("CLS");
        cout << "\n                      .ed'''' '''$$$$be." << endl;
        cout << "                    -'           ^''**$$$e." << endl;
        cout << "                  .'                   '$$$c" << endl;
        cout << "                 /                      '4$$b" << endl;
        cout << "                d  3                      $$$$" << endl;
        cout << "                $  *                   .$$$$$$" << endl;
        cout << "               .$  ^c           $$$$$e$$$$$$$$." << endl;
        cout << "               d$L  4.         4$$$$$$$$$$$$$$b" << endl;
        cout << "               $$$$b ^ceeeee.  4$$ECL.F*$$$$$$$" << endl;
        cout << "   e$''=.      $$$$P d$$$$F $ $$$$$$$$$- $$$$$$" << endl;
        cout << "  z$$b. ^c     3$$$F '$$$$b   $'$$$$$$$  $$$$*'      .=''$c" << endl;
        cout << " 4$$$$L        $$P'  '$$b   .$ $$$$$...e$$        .=  e$$$." << endl;
        cout << " ^*$$$$$c  %..   *c    ..    $$ 3$$$$$$$$$$eF     zP  d$$$$$" << endl;
        cout << "   '**$$$ec   ''  %ce''    $$$  $$$$$$$$$$*    .r' =$$$$P''" << endl;
        cout << "         '*$b.  'c  *$e.    *** d$$$$$'L$$    .d'  e$$***'" << endl;
        cout << "           ^*$$c ^$c $$$      4J$$$$$% $$$ .e*'.eeP'" << endl;
        cout << "              '$$$$$$'$=e....$*$$**$cz$' '..d$*'" << endl;
        cout << "                '*$$$  *=%4.$ L L$ P3$$$F $$$P'" << endl;
        cout << "                   '$   '%*ebJLzb$e$$$$$b $P'" << endl;
        cout << "                     %..      4$$$$$$$$$$ '" << endl;
        cout << "                      $$$e   z$$$$$$$$$$%" << endl;
        cout << "           " << flush; for (int j = 0; j < i; j++) if (j < 3) cout << tab[j] << flush; for (int j = 3; j - i > 0; j--) cout << "    " << flush; cout << "'*$c  '$$$$$$$P'" << flush; for (int j = 3; j < i; j++) if (j < 8) cout << tab[j] << flush; cout << endl;
        cout << "                        .'''*$$$$$$$$bc" << endl;
        cout << "                     .-'    .$***$$$'''*e." << endl;
        cout << "                  .-'    .e$'     '*$c  ^*b." << endl;
        cout << "           .=*''''    .e$*'          '*bc  '*$e.." << endl;
        cout << "         .$'        .z*'               ^*$e.   '*****e." << endl;
        cout << "         $$ee$c   .d'       " << flush; if (i > 7) {cout << "Score: " << score << "     " << flush; for (int j = 0; pow(10, j) < score; j++) cout << ' ' << flush;} else cout << "              " << flush; cout << "'*$.        3." << endl;
        cout << "         ^*$E')$..$'                         *   .ee==d%" << endl;
        cout << "            $.d$$$*                           *  J$$$e*" << endl;
        cout << "             '''''                              '$$$' " << flush;
        Sleep(500);
    }
}


The function is supposed to draw a skull, words "You died" are supposed to slowly appear, and in the end, score and top score is supposed to pop up. (I still havent programed the top score tho).

The problem I am facing, is that the function works properly, but when I add a loop "for (int j = 0; pow(10, j) < score; j++) cout << ' ';" in the line 34, or any loop really, the few last lines after the words "You died" have generated, don't show up at all.
Last edited on
https://www.cplusplus.com/reference/ostream/ostream/flush/
Use cout.flush() after any cout that doesn't end with a \n (or endl), if you want that to appear sooner rather than later.
Thanks for the good advise!
I never heard about the flush function, and I updated my entire program with it :D.
I'll also update the function I shared.
Sadly tho, the skull problem still remains. All the lines below 28th aren't visible after the 7th iteration of the main loop.
Note that I tried both cout.flush(); and << flush; and neither helped :/.
I still don't understand, why the problem occures only after adding a while/for loop in line 34.
Last edited on
Your score decoration loops are messy one-liners, hiding a bug.
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
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

void skull(int score, int top_score)
{
  string tab[7] = { "Y   ", "O   ", "U   ", "   D", "   I", "   E", "   D" };
  for (int i = 0; i < 10; i++) {
    cout << "\n                      .ed'''' '''$$$$be." << endl;
    cout << "                    -'           ^''**$$$e." << endl;
    cout << "                  .'                   '$$$c" << endl;
    cout << "                 /                      '4$$b" << endl;
    cout << "                d  3                      $$$$" << endl;
    cout << "                $  *                   .$$$$$$" << endl;
    cout << "               .$  ^c           $$$$$e$$$$$$$$." << endl;
    cout << "               d$L  4.         4$$$$$$$$$$$$$$b" << endl;
    cout << "               $$$$b ^ceeeee.  4$$ECL.F*$$$$$$$" << endl;
    cout << "   e$''=.      $$$$P d$$$$F $ $$$$$$$$$- $$$$$$" << endl;
    cout << "  z$$b. ^c     3$$$F '$$$$b   $'$$$$$$$  $$$$*'      .=''$c" << endl;
    cout << " 4$$$$L        $$P'  '$$b   .$ $$$$$...e$$        .=  e$$$." << endl;
    cout << " ^*$$$$$c  %..   *c    ..    $$ 3$$$$$$$$$$eF     zP  d$$$$$" << endl;
    cout << "   '**$$$ec   ''  %ce''    $$$  $$$$$$$$$$*    .r' =$$$$P''" << endl;
    cout << "         '*$b.  'c  *$e.    *** d$$$$$'L$$    .d'  e$$***'" << endl;
    cout << "           ^*$$c ^$c $$$      4J$$$$$% $$$ .e*'.eeP'" << endl;
    cout << "              '$$$$$$'$=e....$*$$**$cz$' '..d$*'" << endl;
    cout << "                '*$$$  *=%4.$ L L$ P3$$$F $$$P'" << endl;
    cout << "                   '$   '%*ebJLzb$e$$$$$b $P'" << endl;
    cout << "                     %..      4$$$$$$$$$$ '" << endl;
    cout << "                      $$$e   z$$$$$$$$$$%" << endl;
    cout << "           " << flush;
    for (int j = 0; j < i; j++)
      if (j < 3)
        cout << tab[j] << flush;
    for (int j = 3; j - i > 0; j--)
      cout << "    " << flush;
    cout << "'*$c  '$$$$$$$P'" << flush;
    for (int j = 3; j < i; j++)
      if (j < 7)  //!! array overrun here!!
        cout << tab[j] << flush;
    cout << endl;
    cout << "                        .'''*$$$$$$$$bc" << endl;
    cout << "                     .-'    .$***$$$'''*e." << endl;
    cout << "                  .-'    .e$'     '*$c  ^*b." << endl;
    cout << "           .=*''''    .e$*'          '*bc  '*$e.." << endl;
    cout << "         .$'        .z*'               ^*$e.   '*****e." << endl;
    cout << "         $$ee$c   .d'       " << flush;
    if (i > 7) {
      cout << "Score: " << score << "     " << flush;
      for (int j = 0; pow(10, j) < score; j++)
        cout << ' ' << flush;
    } else
      cout << "              " << flush;
    cout << "'*$.        3." << endl;
    cout << "         ^*$E')$..$'                         *   .ee==d%" << endl;
    cout << "            $.d$$$*                           *  J$$$e*" << endl;
    cout << "             '''''                              '$$$' " << flush;
  }
}

int main()
{
  skull(10, 20);
  return 0;
}

Topic archived. No new replies allowed.