• Create Account

# C++ Command Prompt Question

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

12 replies to this topic

### #1blooper  Members   -  Reputation: 122

Like
Likes
Like

Posted 06 February 2002 - 01:23 PM

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.

### #2Arild Fines  Members   -  Reputation: 968

Like
Likes
Like

Posted 06 February 2002 - 10:50 PM

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.

### #3Jeff D  Members   -  Reputation: 122

Like
Likes
Like

Posted 06 February 2002 - 11:30 PM

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

### #4blooper  Members   -  Reputation: 122

Like
Likes
Like

Posted 07 February 2002 - 01:41 PM

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!

### #5ZenGeneral  Members   -  Reputation: 122

Like
Likes
Like

Posted 07 February 2002 - 01:55 PM

don''t forget FOREGROUND_INTENSITY and BACKGROUND_INTENSITY

### #6matrix2113  Members   -  Reputation: 122

Like
Likes
Like

Posted 09 February 2002 - 06:19 PM

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

### #7Null and Void  Moderators   -  Reputation: 1087

Like
Likes
Like

Posted 09 February 2002 - 06:26 PM

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.

### #8blooper  Members   -  Reputation: 122

Like
Likes
Like

Posted 10 February 2002 - 01:40 PM

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?

### #9blooper  Members   -  Reputation: 122

Like
Likes
Like

Posted 10 February 2002 - 01:51 PM

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...

### #10matrix2113  Members   -  Reputation: 122

Like
Likes
Like

Posted 10 February 2002 - 02:58 PM

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

### #11blooper  Members   -  Reputation: 122

Like
Likes
Like

Posted 10 February 2002 - 03:07 PM

I''ve played with this for some time and I can not get this command to work...any ideas out there? Thanks.

### #12blooper  Members   -  Reputation: 122

Like
Likes
Like

Posted 11 February 2002 - 11:15 AM

A couple more questions concerns that I have....

I wrote this program just to experiment with a few things:

/************************************/
//libraries
#include <iostream>
#include <stdlib.h>

//introduces namespace std
using namespace std;

void wait(float f_secs);
void print_out(string printer);
void clean();

int main( void )
{
print_out("Randomness...");
wait(1.1);
system("clear"); //come on....
clean();
print_out("Work now...");
cin.get();
}

void wait(float f_secs)
{
float secs = f_secs;
clock_t delay = secs * CLOCKS_PER_SEC;
clock_t start = clock();
while(clock() - start < delay)
{}
}

void print_out(string printer)
{
cout<
}

void clean()
{
cout<<"\n";
system("clean"); //i give up
}
/************************************/

My primary concerns (if anyone cares out there):
1)As you can see I can not get system("cls") to work nor "clean" or "clear" as others have suggested (all do nothing). I know it is not XP as I have tried this also on my 98se machine at work. Could it be my compiler (Metrowerks Codewarrior 5, no flames please), though I see no reason why....

2)I found this wait function example in one of my files from a ways back when I was getting into c++ but I can not remember much about it other than the obvious result and a few obscurities floating about in my mind...can anyone give me a quick rundown of the lines? (sad I know) I don''t want to use it again without being absolutely comfortable with its workings.

3)This really should be in q 2 but...I noticed that if I tried to cout "Randomness..." in a line without putting it in a function it would come after the wait not before...why is that?

More than many thanks ahead of time!

### #13matrix2113  Members   -  Reputation: 122

Like
Likes
Like

Posted 11 February 2002 - 04:03 PM

1. I suppose so, seeing how I''ve never heard of that compiler...

2.I don''t get it either, I suggest that if you want to wait, use sleep() (I think thats what its called...it might be comething else though. You need windows.h to use it; it sleeps by the milliseconds aka sending 1000 as the argument would sleep for 1 second)
If you REALLY want to write your own function to sleep (for self-gratification), I recommend something easier (include time.h, then call time(); it returns a number in seconds, if that helps)

3.AHA! Many newbies often get confused by this. This has to do with the way COUT works. See, when you input something, it gets sent to the....thingy (buffer?), where it waits to be sent to the outputscreen. Everything from cout is stored into that...thingy (buffer?) until something FLUSHES it. To FLUSH means to...umm...flush out of from the...thingy...and into the console (where its displayed as whatever you sent it, usually text). The thingy is then empty, and the screen has its output.
I know of several ways to flush the buffer. First of all, calling using endl with an output stream (this thing <<), or using cin with the input stream (this thing >>) flushes the buffer (so you can just add "<< endl" to the end of you cout statement, and it should work). Another way is to flush with thet output (<<) stream (so you could just add "<< flush" to the end of your cout statement, and it would flush without going down a line). The last way I know is to call cout.flush() (which does the same thing as "<< flush", but it looks cooler)

I see you never finished your printout() function, so I''ll do it for you (to show you what it should look like)
  void print_out(string printThis)//Doesn''t Printer call the cout to the printer? Or was there another iostream object for that? I can''t remember, I only read breifly about it in a book once.//...Just to be safe, I''m gonna change the name of the string printer to printThis.{cout << printThis << flush;}

"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

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS