Permalänk
Medlem

Färgade cout syntaxer

Har med lite hjälp av lita läsande hittat att man med SetConsoleTextAttribute() ska kunna sätta lite färg på sina program.

Hittade även denna kod.

#include <iostream> #include <string> #include <stdlib> #include <windows.h> using namespace std; void DrawColorString(string text, WORD color) { HANDLE Output; Output = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(Output, color); cout << text << endl; } int main() { DrawColorString("This text is RED", FOREGROUND_RED); return 0; }

Problemet är att även om koden kompileras finfint av g++ så blir det ingen färg.
Kan det vara så att SetConsoleTextAttribute() inte hör till standard biblioteket så det fungerar under linux? Finns det i sådana fall något annat smidigt sätt att göra samma sak på?

Visa signatur

Light travels faster than sound. Is that why some people appear to be smart before they speak?
It's a big rock. I can't wait to tell my friends. They don't have a rock this big.

Permalänk
Medlem

Det är bara i windows det funkar (windows.h). Kanske ncurses kan hjälpa? Har för mig man kunde ändra sånna saker där.

Permalänk
Medlem

Kompilerade det verkligen i linux?

Visa signatur

:€

Permalänk
Medlem

Tja, cygwin för att vara exakt.

Visa signatur

Light travels faster than sound. Is that why some people appear to be smart before they speak?
It's a big rock. I can't wait to tell my friends. They don't have a rock this big.

Permalänk
Medlem

$ g++ blash.cc blash.cc:3:18: stdlib: No such file or directory blash.cc:4:21: windows.h: No such file or directory blash.cc:8: error: type specifier omitted for parameter `WORD' blash.cc:8: error: parse error before `)' token blash.cc: In function `void DrawColorString(...)': blash.cc:10: error: `HANDLE' undeclared (first use this function) blash.cc:10: error: (Each undeclared identifier is reported only once for each function it appears in.) blash.cc:10: error: parse error before `;' token blash.cc:11: error: `Output' undeclared (first use this function) blash.cc:11: error: `STD_OUTPUT_HANDLE' undeclared (first use this function) blash.cc:11: error: `GetStdHandle' undeclared (first use this function) blash.cc:12: error: `color' undeclared (first use this function) blash.cc:12: error: `SetConsoleTextAttribute' undeclared (first use this function) blash.cc:13: error: `text' undeclared (first use this function) blash.cc: In function `int main()': blash.cc:18: error: `FOREGROUND_RED' undeclared (first use this function) $

Visa signatur

4 datorer: 9 cpuer (plats för 4 till), 10scsi+1satadisk, 7.75gb ram, bara Linux
http://isitfika.net http://code.kryo.se

Permalänk
Medlem

Jag fick inga errors alls.

Visa signatur

Light travels faster than sound. Is that why some people appear to be smart before they speak?
It's a big rock. I can't wait to tell my friends. They don't have a rock this big.

Permalänk
Medlem

jag kompilerade det du klistrat in på linux med g++ version 3.3.4, med resultatet ovan. jag tolkade dina inlägg som att du undrade om det fungerade på linux...

Visa signatur

4 datorer: 9 cpuer (plats för 4 till), 10scsi+1satadisk, 7.75gb ram, bara Linux
http://isitfika.net http://code.kryo.se

Permalänk
Medlem

Jag använder cygwin och då gick det att kompilera, men cygwin kanske inte emulerar en ren linuxmiljö.

Visa signatur

Light travels faster than sound. Is that why some people appear to be smart before they speak?
It's a big rock. I can't wait to tell my friends. They don't have a rock this big.

Permalänk
Medlem

yarrick: Ditt problem var ju att du inte hade window.h-headern. Inte konstigt att det inte kompilerar då. Men jag tror inte att det går att kompilera den i linux ändå... coolt annars, dock.

Visa signatur

:€