Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

Output formatting

Hi,

I'm in my first year of a Computer Science class, and I'm working in a console program. I was wondering if there is anyway to have a carriage return without a line feed. By this I mean that it will move the cursor down one line from the current x-position. If there is anyway of doing this, it would be great appreciated if you would let me know!
0
Cyngus_X-1
Asked:
Cyngus_X-1
  • 3
  • 2
  • 2
  • +2
2 Solutions
 
igor_skCommented:
Try printf("\v");
0
 
igor_skCommented:
Or in C++
cout << "\v";
0
 
DarthNemesisCommented:
The conio.h function cprintf() will treat a newline ('\n') character as only a line feed and not as a carriage return. You can use it just like the c function printf, i.e.:

cprintf("First Line\nSecond Line");

or

int i = 3;
cprintf("x = %d",i);
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
DarthNemesisCommented:
Yeah, i tried \v already, it doesn't work for all compilers (for instance, Borland).
0
 
bkrahmerCommented:
Use the real value, not a string literal.

printf("%c", 0x13);  //10 or 13, i forget

brian
0
 
DarthNemesisCommented:
Yep, same result as using \n or char(10). Simply prints a character (!! for 0x13, solid > for 0x10). The cprintf function is the only one that does it for me, but maybe the compiler Cygnus uses won't be as finicky.
0
 
bkrahmerCommented:
Yeah, I ran a test program, and got the same result.  It appears that the stream flushing is affecting the behavior.  If you build a string like "line1\rline2", and print that, you will see that \r probably works just fine.  DOS just isn't that friendly...

brian
0
 
SalteCommented:
First off, if you want to move down and stay in current position, you want a 'line feed' or LF. The CR code or 'carriage return' will move the cursor to the beginning of current line.

The \r or CR is often used in a loop:

cout << '\r' << "Bytes left: " << setw(10) << nbytes << flush;

This will display the string "Bytes left: ...some number here..."

and will next time move the cursor back to beginning of line and display the same string but possibly with a new number, the result is that user see the number changing.

So your problem is that you would like to just output a '\n'. Your problem is that C treat a '\n' as a combination of '\r' followed by '\n' since C treat '\n' as the 'line break' code.

This leaves the option for \v and similar solutions. One obvious solution to your problem is to output a '\n' but make sure the system doesn't translate it to a CR LF combination first.

This is definitely a very system specific and you are therefore asking the question in the wrong forum. At the very least you should tell us which system you're on when you ask a system specific question.

I am very sure you don't want to output CR which is in a sense the opposite of what you want.

Alf
0
 
tinchosCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split points between igor_sk & Salte

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now