• Popular Now

• 13
• 15
• 19
• 27
• 9

Archived

This topic is now archived and is closed to further replies.

C++ Command Prompt Question

This topic is 5879 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

Share on other sites
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.

Share on other sites
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

Share on other sites
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!

Share on other sites
don''t forget FOREGROUND_INTENSITY and BACKGROUND_INTENSITY

Share on other sites
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

Share on other sites
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.

Share on other sites
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?

Share on other sites
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...

Share on other sites
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