Friday, August 19, 2005

Can this be done with cout?

#include <"iostream">
#include <"algorithm">
#include <"string">
#include <"fstream">

using namespace std;

/*
i would like to keep the numbers of the output : Power , step etc,
on the same place in the console. But this isnt going to work with
this code... So the idea is that the numbers behind the '=' signs
change without going down in the console. how do i do this ?
*/

int main()
{
int dummie;
cout << "Power = 45\n"; cout << "Step = 1\n";
cout << "Magnitude = 34\n"; cout << "press a digit";
scanf("%d",&dummie); // press key..
cout << "\r\r\r\rPower = 48\n";
// going back to first line and overwrite it.
cout << "Step = 2\n";
// overwrite second line with new data.
cout << "Magnitude = 29\n";
// overwrite last line with new data.
cout << "press a digit"; scanf("%d",&dummie);
// press key.. return(1);

}
// well this doesnt work but how do i change the code to make it work

15 Comments:

At 7:38 AM, August 19, 2005, Anonymous Anonymous said...

There is no way in the Standard C++. Anything available would be
platform-specific. See 'curses' library or 'ANSI terminal strings'
or anything else of that nature.

 
At 7:38 AM, August 19, 2005, Anonymous Anonymous said...

So it is in my case dependend on the options of my DEV-C++ terminal
if it is possible and how to make it so?

 
At 7:39 AM, August 19, 2005, Anonymous Anonymous said...

I don't understand the question. What's "DEV-C++ terminal"?

 
At 7:41 AM, August 19, 2005, Anonymous Anonymous said...

#include <"iostream">
#include <"cstdlib">

using namespace std;

int main()
{
char dummie;

// unix/linux clear the screen.
// use system("cls"); on MS Windows
system("clear");

// display text starting at top line of screen
cout << "Power = 45\n";
cout << "Step = 1\n";
cout << "Magnitude = 34\n";

cout << "press a key then Enter ";
// won't work as expected if more than one key pending
cin >> dummie; // read keypress

// unix/linux clear the screen.
// use system("cls"); on MS Windows
system("clear");

// display text starting at top line of screen
cout << "Power = 48\n";
cout << "Step = 2\n";
cout << "Magnitude = 29\n";

cout << "press a key then Enter ";
// won't work as expected if more than one key pending
cin >> dummie; // read keypress

return(1);
}

 
At 7:41 AM, August 19, 2005, Anonymous Anonymous said...

I would assume that he is using Dev-C++ as his IDE - writing terminal
(non-windows) applications. It's what I use - Am I correct?

 
At 7:42 AM, August 19, 2005, Anonymous Anonymous said...

Ehm yes i use Bloodshed Dev-C++ as my IDE.(iam a total idiot, keep this
in mind)
i just compile my code with it and press 'run' to run it

 
At 7:43 AM, August 19, 2005, Anonymous Anonymous said...

tnx! Larry, system("cls") does the trick.
such an simple solution for my problem

 
At 7:44 AM, August 19, 2005, Anonymous Anonymous said...

Bear in mind that this is not portable. On one of the systems I use,
system("cls") executes the spreadsheet program and then deletes all my
emails. Happened to me twice. Dang.

Jonathan

 
At 7:44 AM, August 19, 2005, Anonymous Anonymous said...

Sounds like a virus infected machine to me.

On Windows, "cls" clears the screen, any other action means something is terribly wrong.

Larry

 
At 7:45 AM, August 19, 2005, Anonymous Anonymous said...

It was a joke, except for the portability part.

Jonathan

 
At 7:46 AM, August 19, 2005, Anonymous Anonymous said...

Even if he only wnats to use this program on a win32 pc, system()
commands can be very dangerous and should be avoided in certain cases.
Why do you return(1) has this a reason?
return(0) more common when your porogram finishes without an error.

 
At 7:47 AM, August 19, 2005, Anonymous Anonymous said...

> Sounds like a virus infected machine to me.

Does it? I don't think so.

> On Windows, "cls" clears the screen, any other action
> means something is terribly wrong.

Strangely enough, I rarely work on Windows machines and cls may
mean rather different things (ranging from nothing at all to really weird stuff). The point is that 'system("cls")' is platform specific and may work as intended on some systems but doing weird stuff on others.

 
At 7:48 AM, August 19, 2005, Anonymous Anonymous said...

> Even if he only wnats to use this program on a win32 pc, system()
> commands can be very dangerous and should be avoided in certain cases.
> Why do you return(1) has this a reason?
> return(0) more common when your porogram finishes without an error.

the return(1) is a mistake
i get the impression that a system-command can be somewhat tricky if used under the wrong platvorm. Can i do a check if the program is running under windows?

something like :
if (system.platvorm == win32)
system("cls");

 
At 7:49 AM, August 19, 2005, Anonymous Anonymous said...

You mean in the executable?
No. And you don't need it. Typically a program compiled on platform A won't run on platform B at all for various resons.

But you may check your compiler documentation. Usually there are special macros defined which can be used to diagnose a wrong compiling platform.

 
At 7:50 AM, August 19, 2005, Anonymous Anonymous said...

>> Even if he only wnats to use this program on a win32 pc, system()
>> commands can be very dangerous and should be avoided in certain cases.
>> Why do you return(1) has this a reason?
>> return(0) more common when your porogram finishes without an error.
> i get the impression that a system-command can be somewhat tricky
> if used under the wrong platvorm.

Yes, that the essence of non-portable calls.

> Can i do a check if the program is
> running under windows?

> something like :
> if (system.platvorm == win32)
> system("cls");

and else... what? You see the problem? You cannot possibly list all
systems with all commands. You can either: 1) stick to standard
features; 2) use portable, 3rd party librairies (such as ncurses); 3)
have a compile-time switch (like you're proposing) listing a few major systems; 4) stick to one system with non-portable calls. I'd go for number 2.

If you want to do number 3, check for each compiler, they usually
#define a name, such as WIN32.

Jonathan

 

Post a Comment

<< Home