473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reverse Newline character?

Hi!

I am writing a C++ program for a school project. It's a project and I
could do it without this particualr feature, it's just it would be
better if I could find out one thing:

I'm making a simple game to simulate the rabbit vs. tortoise race. I
need to print a race track on the screen like so:

###############R######
#######T##############

It would be nice if, instead of having to reprint the next tick of the
clock in the race it under the previous line:
#################R####
####T#################

###############R###### SLIPPED!
#######T##############

, which would mean that it would be constantly scrolling, if there was
a way to simply rewrite that exact line on the screen.

So this would be printed:

#################R####
####T#################

The die would roll and moves would be calculated, then the cursor would
be moved up and this would be printed "over" the old racetrack image

###############R###### SLIPPED!
#######T##############

without all of the constant scrolling down the console for each tick of
the clock.

Is there a way to print a reverse newline character so that I could
move the cursor *backwards* up the screen? A newline character moves
it down one line, so is there a way to move the cursor up a line? I've
researched it on the web for a while When I learned PERL (my first
langauge) I tried to find out how to do it then, too, but never could
find anything online or in books about it. So I'm wondering if anyone
could help?

JII

Sep 8 '05 #1
6 6713
<3s******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi!
<SNIP> Is there a way to print a reverse newline character so that I could
move the cursor *backwards* up the screen? A newline character moves
it down one line, so is there a way to move the cursor up a line? I've
researched it on the web for a while When I learned PERL (my first
langauge) I tried to find out how to do it then, too, but never could
find anything online or in books about it. So I'm wondering if anyone
could help?

JII


Platform/OS dependant. With DOS windows you might still be able to use ANSI
commands (not sure) or write directly to the "video memory" (which wouldn't
really be video memory in this case).

You'll need to search the web for your OS (probably windows), "console" and
things like "position".

Maybe: Windows console position c++
or something.

good luck
Sep 8 '05 #2
If you find how to do it, plz give the answer here ~!! I think its quite
interesting to know
<3s******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi!

I am writing a C++ program for a school project. It's a project and I
could do it without this particualr feature, it's just it would be
better if I could find out one thing:

I'm making a simple game to simulate the rabbit vs. tortoise race. I
need to print a race track on the screen like so:

###############R######
#######T##############

It would be nice if, instead of having to reprint the next tick of the
clock in the race it under the previous line:
#################R####
####T#################

###############R###### SLIPPED!
#######T##############

, which would mean that it would be constantly scrolling, if there was
a way to simply rewrite that exact line on the screen.

So this would be printed:

#################R####
####T#################

The die would roll and moves would be calculated, then the cursor would
be moved up and this would be printed "over" the old racetrack image

###############R###### SLIPPED!
#######T##############

without all of the constant scrolling down the console for each tick of
the clock.

Is there a way to print a reverse newline character so that I could
move the cursor *backwards* up the screen? A newline character moves
it down one line, so is there a way to move the cursor up a line? I've
researched it on the web for a while When I learned PERL (my first
langauge) I tried to find out how to do it then, too, but never could
find anything online or in books about it. So I'm wondering if anyone
could help?

JII

Sep 8 '05 #3

<3s******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I am writing a C++ program for a school project. It's a project and I
could do it without this particualr feature, it's just it would be
better if I could find out one thing:

I'm making a simple game to simulate the rabbit vs. tortoise race. I
need to print a race track on the screen like so:

###############R######
#######T##############

It would be nice if, instead of having to reprint the next tick of the
clock in the race it under the previous line:
#################R####
####T#################

###############R###### SLIPPED!
#######T##############

, which would mean that it would be constantly scrolling, if there was
a way to simply rewrite that exact line on the screen.

So this would be printed:

#################R####
####T#################


You can use a library that gives you the ability to reposition the cursor on
the screen. (e.g. ncurses).

Or, if you can display the track and everything else on a single line, you
can take advantage of the '\r' (carriage return) character.

Here is an example; just use a different "slowing down function" instead of
sleep(), if you are not running on a unix system:

#include <iostream>
#include <unistd.h>

using namespace std;

int const track_lenght = 20;

void display_track(int tortoise_position, int rabbit_position)
{
cout << '\r';

for (int i = 0; i != track_lenght; ++i)
{
char const tortoise_char = 'T';
char const rabbit_char = 'R';
char const both_char = 'X';
char const track_char = '#';

char display_char = track_char;

if (i == tortoise_position)
{
display_char = ((i == rabbit_position)
? both_char
: tortoise_char);
}
else if (i == rabbit_position)
{
display_char = rabbit_char;
}

cout << display_char;
}

cout << flush;
}

void wait_for_a_while()
{
sleep(1);
}

int main()
{
for (int i = 0; i != track_lenght; ++i)
{
display_track(i / 2, i);
wait_for_a_while();
}

cout << '\n';
}

Ali

Sep 8 '05 #4
3s******@gmail.com wrote:
Hi!

I am writing a C++ program for a school project. It's a project and I
could do it without this particualr feature, it's just it would be
better if I could find out one thing:

I'm making a simple game to simulate the rabbit vs. tortoise race. I
need to print a race track on the screen like so:

###############R######
#######T##############

It would be nice if, instead of having to reprint the next tick of the
clock in the race it under the previous line:
#################R####
####T#################

###############R###### SLIPPED!
#######T##############

, which would mean that it would be constantly scrolling, if there was
a way to simply rewrite that exact line on the screen.

So this would be printed:

#################R####
####T#################

The die would roll and moves would be calculated, then the cursor would
be moved up and this would be printed "over" the old racetrack image

###############R###### SLIPPED!
#######T##############

without all of the constant scrolling down the console for each tick of
the clock.

Is there a way to print a reverse newline character so that I could
move the cursor *backwards* up the screen? A newline character moves
it down one line, so is there a way to move the cursor up a line? I've
researched it on the web for a while When I learned PERL (my first
langauge) I tried to find out how to do it then, too, but never could
find anything online or in books about it. So I'm wondering if anyone
could help?

JII


'\r' and '\b' are the characters that would help you... (not sure if
it's covered in C++ FAQ.. it's there for sure in C FAQ)

try something like:

....
while(...) {
...
string rLine = constructRabbitLine(...);
string tLine = constructTortoiseLine(...);
cout << rLine << '\n' << tLine << '\r';
cout.flush();
...
}
Sep 9 '05 #5
Jim Langston wrote:
<3s******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi!


<SNIP>
Is there a way to print a reverse newline character so that I could
move the cursor *backwards* up the screen? A newline character moves
it down one line, so is there a way to move the cursor up a line? I've
researched it on the web for a while When I learned PERL (my first
langauge) I tried to find out how to do it then, too, but never could
find anything online or in books about it. So I'm wondering if anyone
could help?


If this is just a 'one-off' and you don't mind not being portable, it is
the case that most terminal software under X recognises ANSI control
sequences (example, xterm).

--
Ron House ho***@usq.edu.au
http://www.sci.usq.edu.au/staff/house
Sep 14 '05 #6
Great!

Thanks for all of your help. I can't put all of the information
required in my project on a single line, but I will see if I can figure
out how to use ncurses (I use GNU/linux normally, and Win2000
sometimes).

I'll see if I can find the C FAQ and figure out what \b does and if
it's what I'm looking for. If not, the ncurses library may be
necessary. I hope it works in Windows!

Thanks guys!

JII

Sep 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: utab | last post by:
Hi there I am trying to read from a file, I am trying to read certain fields,there are 6 fields in this file like --------/--------/--------/--------/--------/--------/ All fields are 8...
13
by: comp.lang.php | last post by:
Other: <input name="school_type_other" size="30" maxlength="75" value="<?php if ($_POST) echo $_POST; else echo str_replace('"', '&quot;', str_replace('\\', '', $result->school_type_other)); ...
0
by: Adam Right | last post by:
Hi, I am using .net framework functions to send a mail over exchange server but i want to use newline character in the body. I cannot insert this character in the body. .NET constructs the...
5
by: Adam Right | last post by:
Hi, Is there a way to construct the mail body including newline characters by using .net framework mailing functions when sending an email? I cannot insert newline character into the body of the...
6
by: Daniel Mark | last post by:
Hello all: I have the following snippet: In : fileName = 'Perfect Setup.txt\n' In : fileName = fileName # remove the '\n' character In : fileName Out: 'Perfect Setup.txt'
16
by: junky_fellow | last post by:
Is there any efficcient way of removing the newline character from the buffer read by fgets() ? Is there any library function that is similar to fgets() but also tells how many bytes it read...
3
by: GoalieDude | last post by:
My problem is that I'll receive a large excel file from a customer that has hard return in one of the cells (i.e. pressing alt + enter to make a new line in a cell) and when I export it to a text...
0
by: Gary Herron | last post by:
Support Desk wrote: The problem has nothing to do with lists. The readlines() function returns each line *with* its newline. To strip it off, use line.strip() Gary Herron
2
by: alex21 | last post by:
The string is coming from this url, http://www.elixirwd.com.au/test.php The Username and Password for this url is, Username:user Password: password Now simply downloading the data using the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.