Solved

How to move cursor to newline in console window.....

Posted on 2000-05-18
12
348 Views
Last Modified: 2010-04-15
I want to move the cursor in a console window to a new line without pushing enter key, is that possible?

void main(void)
{
char line[100];
cout << " Type a line terminated by 't'" << endl;
cin.getline( line, 100, 't' );
cout << line;

}
0
Comment
Question by:eing9603
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 3

Expert Comment

by:sburck
ID: 2820987
I think

cout << "\n\r";

 should work.
0
 

Author Comment

by:eing9603
ID: 2821167
The problem is that cin.getline(); must be confirmed with enter..

I want to type a string and confirm with 't', then the string should be stored in line... and the cursor should move to the next line..
0
 
LVL 5

Accepted Solution

by:
laeuchli earned 250 total points
ID: 2821301
try using __getchar();
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 22

Expert Comment

by:nietod
ID: 2827292
Standard C/C++ does not provide any way to do this.  In standard C/C++ input is always "line buffered" this means that the input typed by the user does not become available to your program until the user presses enter/return.   This is to give the user an opportunity to correct mistakes and also has to do with historical hardware related reasons.

Now this does not mean it is not possible, it just means that standard C/C++ does not support it.   Many operating systems will have features that would allow you to do this.  but we can't comment on that without knowing the operating system.     For example the solution laeuchi proposed works on some compilers in the Windows OS.
0
 
LVL 1

Expert Comment

by:karthicraja
ID: 2827765
this is enough
char line[100];
cout << " Type a line terminated by 't'\n" << endl;
cin.getline( line, 100, 't' );
cout << line;
0
 
LVL 22

Expert Comment

by:nietod
ID: 2828076
karthicraja, try it.  It doesn't work.  read what I said.
0
 

Author Comment

by:eing9603
ID: 2832474
Adjusted points from 200 to 250
0
 

Author Comment

by:eing9603
ID: 2832475
nietod, you are right.. It doesn't work.. and my operating system is windows NT...
0
 
LVL 3

Expert Comment

by:sburck
ID: 2832535
I think you ignored laeuchli's answer, which should work.  You weren't clear  in the original question that you wanted the CRLF to happen during getline, but to do this, you must get character by character, and, when you have determined the end-of-input, output the CRLF yourself.  If you want to use cin/cout, use get/put - Something like:

char line[100];
char index = 0;
char ch;

cout << " Type a line terminated by 't'" << endl;
while (index < 100)
{
    cin.get(ch);
    if (ch== 't')
   {
        cout << "/n/r"
        break;
    } else
        cout << ch;
    line[index++] = ch;
// you may want other things, like backspace handling:
//  if (ch == '\x08) --index;, and possibly other things.
}        
0
 

Author Comment

by:eing9603
ID: 2832822
sburck,  It doesn't work.. I think nietod is right, read nietod's comment..  My compiler is Microsoft VC++ 5.0 (Visual studio) and i use an Aplication console project..
0
 
LVL 3

Expert Comment

by:sburck
ID: 2832912
Eing9603 -

I'll use standard C functions here, not C++.  I don't have a reference manual around, and the on-line ones weren't much use, but as far as my feeble memory serves me, although what nietod says is true for getchar, it isn't true for getc or fgetc - in fact, since these can also work on binary files, line buffering would be very problematic for them.  If you read the keyboard with these, I'm not sure the problem would remain.

I also seem to recall a non-standard getchar() replacement which shows up in a lot of places, called getche(), which solved the problem you're talking about.  If you have this function, it should work.










0
 

Author Comment

by:eing9603
ID: 2833020
Thanks for the tip, it works with getch()
and
_putch( '\r' );    /* Carriage return */
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dll Dynamic way c/c++ 9 145
How to organize data in excel ? 2 114
Test against App 49 137
Need to explain what is Ambiguous Permutations and why the solution works . c++ 5 140
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question