Trouble with a counting if statement
Jan 27, 2017 at 1:48am UTC
I put together a little code to test colors and I'm not really sure whats wrong. I had the system output the value for the a variable as well but I'm still not having any success, the variable continues to stay at 2, and output the color i associated with 2. Any ideas? Thanks!
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
#include <iostream>
#include <chrono>
#include <thread>
#include <Windows.h>
using namespace std;
using namespace std::this_thread;
using namespace std::chrono;
int a = 1;
int main()
{
while (true )
{
if (a != 7)
{
a += a;
}
else
{
a = a - 6;
}
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
switch (a)
{
case 1:
SetConsoleTextAttribute(h, FOREGROUND_RED);
break ;
case 2:
SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN);
break ;
case 3:
SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_GREEN);
break ;
case 4:
SetConsoleTextAttribute(h, FOREGROUND_GREEN);
break ;
case 5:
SetConsoleTextAttribute(h, FOREGROUND_BLUE);
break ;
case 6:
SetConsoleTextAttribute(h, FOREGROUND_BLUE | FOREGROUND_BLUE | FOREGROUND_RED);
break ;
case 7:
SetConsoleTextAttribute(h, FOREGROUND_BLUE | FOREGROUND_RED);
break ;
}
while (true )
{
cout << "rainbow" << endl;
cout << a << endl;
}
}
return (0);
}
Jan 27, 2017 at 3:04am UTC
Try something on these lines:
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 <Windows.h>
#include <chrono>
#include <thread>
int main()
{
using namespace std::chrono_literals;
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN);
std::cout << "Rainbow \n" ;
auto start = std::chrono::high_resolution_clock::now();
std::this_thread::sleep_for(2s);
auto end = std::chrono::high_resolution_clock::now();
SetConsoleTextAttribute(h, FOREGROUND_BLUE | FOREGROUND_RED);
std::cout << "Rainbow \n" ;
auto start1 = std::chrono::high_resolution_clock::now();
std::this_thread::sleep_for(2s);
auto end1 = std::chrono::high_resolution_clock::now();
}
ps: sorry I could have cleaned this up a bit more but gotta run!
Topic archived. No new replies allowed.