Code:#include<stdio.h> #include<dos.h> #include<conio.h> void main() { int x=1; for(int i=i;i<10;i++) { textcolor(i+1); cprintf("%c \t %d \t \t",x,x); x++; } }
Code:#include<stdio.h> #include<dos.h> #include<conio.h> void main() { int x=1; for(int i=i;i<10;i++) { textcolor(i+1); cprintf("%c \t %d \t \t",x,x); x++; } }
it is showing me this error...
Compiling NONAME00.CPP:
Error NONAME00.CPP 13: Call to undefined function 'textcolor' in function main()
Error NONAME00.CPP 14: Call to undefined function 'cprintf' in function main()
Well, dos.h and conio.h are ancient headers that don't appear to be supported by your compiler anymore, thus you can't use textcolor or cprintf. At least it's a good sign you're NOT using Turbo C. Look at the documentation for current functions that do the same thing: Console Functions.
Also, on line 11, what is the value of i the first time through the loop? That's right, it's random garbage! You need to initialize it to something reasonable, like 0 or 1.
Andruil I thought C does not support local declarations within a for loop? Is there an earlier standard that supported this?
ex: for(int i=0;.....)
No, standard support for that started with C99. It's often disabled or warned about by default though, since many popular compilers only have partial support for C99.
IIRC, the older standard, C89 didn't support mixed declarations at all, meaning all variable declarations had to be at the top of the function.
i am using borland version 4.5 ... when opened the help ... it shows me that type of declaration only ... is their any alternative for it ..
and yeah .. line 11 error .. got it .. started it with 0 .. typing error while posting it here...
You mean the help files show you the declaration of i inside the for loop? If that works, then keep using it. If it doesn't, you can just put int i; at the top of the funciton and take it out of the for loop:
Code:int i; for (i = 0; i < 10; i++)
not the for loop ... i am saying it for dos.h .. and cprintf .. etc..
Ahh, okay. Well, there is no portable way, but for Windows, check out the console functions: Console Functions. I'm not a Windows programmer though, so that's as far as my knowledge goes.
i tried to read that ... but it all goes over my head...
will it run if i try in linux?? ubuntu(11.10) ??
No. Because it's Windows-specifc. That implies it will only work on Windows.
For Linux, drop `dos.h` and `conio.h`, and use standard `printf()` instead of `cprintf()`. Forget about `textcolor()` and see this link about how to get some color into your shell.
Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.
ok .. it didnt worked well on windows....
shows me this errorCode:#define BRIGHT 1 #define RED 31 #define BG_BLACK 40 printf("%c[%d;%d;%dmHello World", 0x1B, BRIGHT,RED,BG_BLACK);
Compiling NONAME00.CPP:
Warning NONAME00.CPP 4: Style of function definition is now obsolete
Error NONAME00.CPP 4: ) expected
You have some typos in your printf unless you need that '[' and the 'm' embedded in your printed string.
That method msh gave you was for Linux.
Well, what you have is not a complete program. There is no main function, and no #include statements for example. So either you need to make that a complete program and try compiling again, or you need to post the real program with the real errors and real line numbers.
As for console colors in general, you have a few options:
- You are writing this for Windows only. Use the link I gave you in post #9. In there is a function called SetConsoleTextAttribute. That's what you want. Google for examples.
- You are writing this for Linux only, and DONT have ncurses. Use the link provided by msh in post #11, though this is highly unportable.
- You are writing for Linux and/or Windows. Use a curses library like ncurses/pdcurses. This is the most portable (there will be some small differences), but requires a bit more work to get things up and running. Google for examples.