### Matrix Effect Main Loop Help

 ``123456789101112131415161718192021222324`` ``````int _tmain(int argc, _TCHAR* argv[]) { int j = 7; while (true) { int i = 0; // Output a random row of characters while (i < 80) { std::cout << GetChar(j + i*i, 33, 30); ++i; } j = (j + 31); // Delay i = 0; while (i < 300000) { GetChar(1, 1, 1); ++i; } } std::cin.get(); return 0; }``````

It's from this tutorial here but Xoax doesn't explain it too well. I've understood his previous tutorials absolutely fine but it's rushed in too quickly to new concepts without building me up to fully understand it.

But yeah to be honest I don't get any of the main block. If you want to see all of the code it's here:
http://xoax.net/cpp/crs/console/lessons/Lesson13/

If someone could explain it to me it would be fantastic.
Last edited on
When you add numbers to a char, it increments the char by the ASCII table, a char might be 70 in the ASCII table. If you add 4 to it, now it'll display char ASCII 74. Getchar function does that. The function modulus is not needed because the operator '%' is here. We get randomness using the getChar function by incrementing the variables. Use a forever for loop. Use sleep() to delay. Use this header to use sleep and color. Use using namespace std; and int main(int nNumberofArgs,char* pszArgs[])
{

}

Revised code:

main.cpp
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include #include #include #include "concor.h" using namespace std; char GetChar(int iGenerator, char cBase, int iRange) { return (cBase + (iGenerator % iRange)); } int main(int nNumberofArgs,char* pszArgs[]) { char caRow[80]; int j = 7; int k = 2; int l = 5; int m = 1; for(;;) { int i = 0; // Output a random row of characters while (i < 80) { if (caRow[i] != ' ') { caRow[i] = GetChar(j + i*i, 33, 30); } cout << caRow[i]; ++i; } j = (j + 31); k = (k + 17); l = (l + 47); m = (m + 67); caRow[j % 80] = '-'; caRow[k % 80] = ' '; caRow[l % 80] = '-'; caRow[m % 80] = ' '; // Delay i = 0; Sleep(10); } return 0; } ``````

concor.h //color and sleep()

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879`` ``````#ifndef _EKU_CONCOL #define _EKU_CONCOL #ifndef _INC_WINDOWS #include #endif /*_INC_WINDOWS*/ bool textcolorprotect=true; /*doesn't let textcolor be the same as backgroung color if true*/ enum concol { black=0, dark_blue=1, dark_green=2, dark_aqua,dark_cyan=3, dark_red=4, dark_purple=5,dark_pink=5,dark_magenta=5, dark_yellow=6, dark_white=7, gray=8, blue=9, green=10, aqua=11,cyan=11, red=12, purple=13,pink=13,magenta=13, yellow=14, white=15 }; inline void setcolor(concol textcolor,concol backcolor); inline void setcolor(int textcolor,int backcolor); int textcolor();/*returns current text color*/ int backcolor();/*returns current background color*/ #define std_con_out GetStdHandle(STD_OUTPUT_HANDLE) //----------------------------------------------------------------------------- int textcolor() { CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(std_con_out,&csbi); int a=csbi.wAttributes; return a%16; } int backcolor() { CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(std_con_out,&csbi); int a=csbi.wAttributes; return (a/16)%16; } inline void setcolor(concol textcol,concol backcol) {setcolor(int(textcol),int(backcol));} inline void setcolor(int textcol,int backcol) { if(textcolorprotect) {if((textcol%16)==(backcol%16))textcol++;} textcol%=16;backcol%=16; unsigned short wAttributes= ((unsigned)backcol<<4)|(unsigned)textcol; SetConsoleTextAttribute(std_con_out, wAttributes); } #if defined(_INC_OSTREAM)||defined(_IOSTREAM_) ostream& operator<<(ostream& os,concol c) {os.flush();setcolor(c,backcolor());return os;} #endif #if defined(_INC_ISTREAM)||defined(_IOSTREAM_) istream& operator>>(istream& is,concol c) {cout.flush();setcolor(c,backcolor());return is;} #endif #endif /*_EKU_CONCOL*/ ``````
Thanks. How does `caRow[i] = GetChar(j + i*i, 33, 30);` work with these declarations?:
 ``12345678`` `````` j = (j + 31); k = (k + 17); l = (l + 47); m = (m + 67); caRow[j % 80] = '-'; caRow[k % 80] = ' '; caRow[l % 80] = '-'; caRow[m % 80] = ' ';``````

That's the thing that I get stuck on. How does the actual randomness work and how do the random ASCII figures get outputted? When do the spaces occur and when do the - occur? Why is the ++i present (does it print out the values onto the screen one by one in a row format)?
Last edited on
 ``123`` ``````j = (j + 31); // Making new value for j caRow[j % 80] = '-'; //pretend j = 80, '%' is dividing j by 80 and only outputting the remainder. caRow[0] (because remainder of 80 / 80 = 0) will now have the char '-' ``````

 `` `` ``It should be i++ for the usual incrementing. caRow[0] will be different than caRow[1], or caRow[n] (n being anything). pretend the 'n' is an address for every space in the array. When you first declare an array such as array[256], the array 'array' will have 256 spaces to put your inputs. So, caRow[0] is holding a different char then caRow[1]. When you do caRow[i], and if 'i' was 3, it will translate to caRow[3] and therefore 'cout'ing caRow[3]. i++ is to make the 'i' go up by one.``
^
|
|

*facepalm*

epic explanation fail....
Hi DELB,

I really think you need to go back to basics with C++
You've jumped in way too deep before you're understanding the basics; these examples you're trying to understand seem to be too complex.

Having said that, it's great that you're keen to learn and we'll continue to give you all the help we can. I just feel you'll get much more from it if you revisit the basics.

I've just scanned through the 1st page of this tutorial ...

http://www.cprogramming.com/tutorial/lesson1.html

It seems to be covering all the important stuff.

Jim
Topic archived. No new replies allowed.