This is a small C++ Program I made, It simply uses a batch command to make the background change colours epicly fast, so I called it 'Flash'.
I included the code with it just in case you people are scared o_o.
Yeah it's basic, I know.
I made this a while ago but just decided I'd post it.
SOURCE CODE:
#include <iostream>
using namespace std;
int main()
{
int numbz;
numbz = 0; //Integarz
int lulzcake;
for ( ; ; )
{
static const char *colourlist[10]; //array with the colourz
colourlist[0] = "color 0a";
colourlist[1] = "color 1b";
colourlist[2] = "color 2c";
colourlist[3] = "color 3d";
colourlist[4] = "color 4e";
colourlist[5] = "color 5f";
colourlist[6] = "color 6b";
colourlist[7] = "color 7a";
colourlist[8] = "color 8f";
colourlist[9] = "color 9e";
numbz++; //Infinite loop of changing colours
system(colourlist[numbz]);
if(numbz>9) //Each time it does teh loop numbz goes up and it plays teh specified Array thing
{
numbz=0;//reset it back to 0 once it's past 9 so it keeps changing :biggrin.gif:
}
}
}
I commented some stuff because I was boooored.
Rollback Post to RevisionRollBack
Notch:
Celebrating the full version with the fan is going to be an amazing experience. I am a bit nervous about it, to be honest.
1. Don't define the const array inside the loop. Define it outside of the loop so it doesn't have to get recreated again and again.
2. There's an easier and shorter way to fill your array, and it actually does look a little different when being compiled. Same thing with your numbz variable.
3. Fun fact: You can make for loops work for you, something else I'll show you in a bit.
4. There's an error in your loop logic
Here's how I would write the same thing. Here's hoping the forum doesn't destroy my indentation:
There's many differences, as you might notice, the biggest of which is that I moved the creation of the array outside of the loop and also initialized it. See, when you declare something, you can initialize it on the same line, and you should if you can. There's a small difference between initializing a variable as it's being created and declaring a variable and then putting values into it. The former is slightly faster than the latter, and it does matter if you'll be doing a lot of initialization.
Here, I've shown you the syntax for initializing an array and also for initializing a regular variable (numbz, which is done in the loop).
For loops are fun because you can get them to do things for you rather than placing that code elsewhere in your program. Here, I've got it creating the numbz variable and also incrementing it. The first statement executes once before the loop begins, the second is for the logic check to see if the loop should continue (it executes before each iteration, and the return value is used to determine whether the loop continues or not), the third is any statement you want to execute at the end of each iteration.
I also did the logic for resetting numbz back to 0 once it hits 9 a bit differently. For things like this, I prefer to use the modulo ( % ) operation. What it does is divides the value on the left by the value on the right and returns the remainder (9 % 4 = 1; 5 % 3 = 2). Here, I've used the unary version of that, so it performs the operation using the value of the variable on the left and the value of the entire right side of the equation and then sets the variable on the left to the return value. It's just a little bit more elegant.
Mostly, my code is just cleaner. All programmers should strive for nice, clean code, though.
You also have a one-off error in your loop logic. Your loop will bring numbz up to 10 and execute using that value, yet your array only goes from 0-9. You will not get a compiler error for attempting to access colourlist[10], and the program will happily do it, but you do not know what that data is. This is what's known as a buffer overflow, and it's dangerous behavior in a program.
Now, thankfully you're not actually running into anything and most CPUs and OSs have protection against this sort of thing, but that doesn't mean it still can't cause unintended behavior. Re-evaluate your loop logic and fix that error.
http://www.filefactory.com/file/b295313/n/Flash_zip
This is a small C++ Program I made, It simply uses a batch command to make the background change colours epicly fast, so I called it 'Flash'.
I included the code with it just in case you people are scared o_o.
Yeah it's basic, I know.
I made this a while ago but just decided I'd post it.
SOURCE CODE:
I commented some stuff because I was boooored.
Nah, don't say it sucks. All programmers go through an early state like this, it's just a stepping stone to something greater.
1. Don't define the const array inside the loop. Define it outside of the loop so it doesn't have to get recreated again and again.
2. There's an easier and shorter way to fill your array, and it actually does look a little different when being compiled. Same thing with your numbz variable.
3. Fun fact: You can make for loops work for you, something else I'll show you in a bit.
4. There's an error in your loop logic
Here's how I would write the same thing. Here's hoping the forum doesn't destroy my indentation:
There's many differences, as you might notice, the biggest of which is that I moved the creation of the array outside of the loop and also initialized it. See, when you declare something, you can initialize it on the same line, and you should if you can. There's a small difference between initializing a variable as it's being created and declaring a variable and then putting values into it. The former is slightly faster than the latter, and it does matter if you'll be doing a lot of initialization.
Here, I've shown you the syntax for initializing an array and also for initializing a regular variable (numbz, which is done in the loop).
For loops are fun because you can get them to do things for you rather than placing that code elsewhere in your program. Here, I've got it creating the numbz variable and also incrementing it. The first statement executes once before the loop begins, the second is for the logic check to see if the loop should continue (it executes before each iteration, and the return value is used to determine whether the loop continues or not), the third is any statement you want to execute at the end of each iteration.
I also did the logic for resetting numbz back to 0 once it hits 9 a bit differently. For things like this, I prefer to use the modulo ( % ) operation. What it does is divides the value on the left by the value on the right and returns the remainder (9 % 4 = 1; 5 % 3 = 2). Here, I've used the unary version of that, so it performs the operation using the value of the variable on the left and the value of the entire right side of the equation and then sets the variable on the left to the return value. It's just a little bit more elegant.
Mostly, my code is just cleaner. All programmers should strive for nice, clean code, though.
You also have a one-off error in your loop logic. Your loop will bring numbz up to 10 and execute using that value, yet your array only goes from 0-9. You will not get a compiler error for attempting to access colourlist[10], and the program will happily do it, but you do not know what that data is. This is what's known as a buffer overflow, and it's dangerous behavior in a program.
Change your code to this and see what I mean:
Just hit enter to move the program one step.
Now, thankfully you're not actually running into anything and most CPUs and OSs have protection against this sort of thing, but that doesn't mean it still can't cause unintended behavior. Re-evaluate your loop logic and fix that error.