Advertisement

C++ Command Prompt Question

Started by February 06, 2002 07:23 PM
11 comments, last by blooper 22 years, 9 months ago
I''m relatively new to C++ and I had a question. In the command prompt is there a way to make it so everything remains static/in place instead of scrolling? Also I was wondering about coloring letters (I don''t know if it makes a difference but I have XP''s command prompt not reg DOS). I''m really more concerned with the scrolling issue though, thanks for your time. -- Some people seem to have freedom of the masses confused with personal freedom.
C++ as such doesnt give any direct control over the console - you need to use native libraries for that. If you''re on Windows, you will want to look into the functions listed here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/conchar_3vg3.asp
For *nix, you probably want to use the ncurses library(google is your friend)



Once there was a time when all people believed in God and the church ruled. This time is called the Dark Ages.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Advertisement
I replied to your ast message I dont think you saw it so here is what I posted before:

Well I can help with coloring the text, but the scrolling is an issue I would have to do myself. Her goes with the coloring.

You need this line

#include < windows.h >

in that header file there is 2 things you need to use this:

HANDLE H_OUTPUT = GetStdHandle(STD_OUTPUT_HANDLE);

You need that to do colors. Then you make need to do this:

SetConsoleTextAttribute(H_OUTPUT, FOREGROUND_RED);
cout << "Hello World";

Now there is six color attributes they are:

FOREGROUND_RED
FOREGROUND_BLUE
FOREGROUND_GREEN
BACKGROUND_RED
BACKGROUND_BLUE
BACKGROUND_GREEN

The FOREGROUND colors affect the symbol, while the BACKGROUND colors affect behind the symbol.

Now you can do this:

SetConsoleTextAttribute(H_OUTPUT, FOREGROUND_RED | FOREGROUND_GREEN | BACKGROUND BLUE);

That will make orange lettering on blue.

The ''|''s, I wont go into details, but if you want orange you have to mix up FOREGROUND_RED and FOREGROUND_GREEN. The ''|'' tells it to use that.

Try this:

      #include < windows.h >#include < iostream >using namespace std;int main(){   HANDLE H_OUTPUT = GetStdHandle(STD_OUTPUT_HANDLE);   SetConsoleTextAttribute(H_OUTPUT, FOREGROUND_RED | FOREGROUND_GREEN | BACKGROUND_BLUE);   cout << "Hello!";}      


I hope I helped

Jeff D
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
Actually I did manage to read your other answer though I regret I couldn''t respond as I was in a Physics class at the time. Thanks alot that helped quite a bit!

Oh, Arild Fines, thanks alot for the site...though do you know of any tut progs? I have found example to be much more easily grasped (though I am currently delving into the Dark Recesses of the Black Library that is MSDN now...) Thanks in any event!
don''t forget FOREGROUND_INTENSITY and BACKGROUND_INTENSITY
Why Not?
You can't make everything stay in place, but you can redraw the window over every time you make a change
Just make a function to draw on the screen, and, at the top of the function, clear the screen so that everything gets wiped out, and the cursor returns to 0,0 (the upper left)
There are a few ways to clear the screen, but probably the easiest is
    System("cls");    

System() is (as I recently discovered) a function to use DOS commands (such as DIR, PROMPT, and CLS).
To use it, you need to include stdlib.h (umm...maybe its stdio.h....I can't remember...and theres another one...starts with a P...can't remember its name...too...sleepy....)

"I've learned something today: It doesn't matter if you're white, or if you're black...the only color that REALLY matters is green"
-Peter Griffin

EDIT: You need either process.h or stdlib.h to use the System() function

Edited by - matrix2113 on February 10, 2002 1:21:58 AM
"I've learned something today: It doesn't matter if you're white, or if you're black...the only color that really matters is green"-Peter Griffin
Advertisement
quote: Original post by matrix2113
System() is (as I recently discovered) a function to use DOS commands (such as DIR, PROMPT, and CLS). To use it, you need to include stdlib.h (umm ... maybe its stdio.h .... I can''t remember...and theres another one ... starts with a P ... can''t remember its name ... too ... sleepy ....)

First of all, it''s "system" (with no capital ''S''). Secondly, it isn''t for DOS commands, it''s for invoking the system command interpreter in general (it doesn''t have to be DOS). It''s in stdlib.h, you''re correct. It''s a standard function, not an I/O function, so it wouldn''t be in stdio.h.

In general, you should stay away from using the "system" function. There''s almost always a better way of doing what you want.

I''m really looking for anything (no really, anything) that I can do pretty quickly and basicly allows me to "draw" on the command prompt screen without the need for scrolling. If there is a way other than calling system() then I am more than open to suggestion, but thanks for this!

Oh...where can I find other "system()" commands/calls?
I wrote this program and it didn''t seem to work, I get the busy hourglass for a second and then it just ends without clearing the screen. (I''m running XP by the way...)

#include <iostream>
#include <stdlib.h>

//introduces namespace std
using namespace std;

int main( void )
{
cout<<"Clear screen? ";
char resp=''n'';
cin>>resp;
if(resp==''y''){ system("cls"); }
}

Is it my prog? Thanks ahead of time.
Oh, and I fiddled with it to just print out a line then call system("cls") and still no luck...

I''m running XP and it worked just fine for me (if you don''t want it to exit, make sure to add getch() at the end)
Other system commands....lets see...lets see...
I think you could use cd (change directory) and md (make directory), which are standard for DOS. I know you can use cls to clear the screen. ummm...more Dos commands, more DOS commands...
.....
Prompt (changes prompt - $P = path, $G = >, $L = <, $T = Time, $D = Date)...Thats MIGHT work, I haven''t tried it
I know you can use pause (which does the same thing as getch()) I''ve never used it, but I''ve seen other people do it.


"I''ve learned something today: It doesn''t matter if you''re white, or if you''re black...the only color that REALLY matters is green"
-Peter Griffin
"I've learned something today: It doesn't matter if you're white, or if you're black...the only color that really matters is green"-Peter Griffin

This topic is closed to new replies.

Advertisement