backspacing so that after cin>>, you don't go to new line

Dec 9, 2010 at 8:47pm
for example, say i write
1
2
3
4
5
6
7
8
int main()
{
  string name;
  cout<<"Enter your name: "; cin>>name;
  cout<<"..........Name entered\n";

  return 0;
}


how can i get line 5 to be printed on the same line as line 4. Because after you input from istream it automatically goes to a new line.
Dec 9, 2010 at 8:50pm
You can't. cin doesn't get any input until the user presses enter.
Dec 9, 2010 at 8:53pm
So this is like a console thing? When the console receives enter signal it automatically moves to a new line?
Dec 9, 2010 at 9:20pm
Yes. Enter writes a '\n'.
Dec 9, 2010 at 11:50pm
and instead of \n i've heard that it's better to use endl because it's not specific to platform, it can be used on various ones since endl is part of the standard library. Correct me if i'm wrong.
Dec 10, 2010 at 12:21am
This really isn't very good and I'm sure there's a better way, but you could try adding this:

1
2
3
  system("cls");
  cout<<"Enter your name: "; cout << name;
  cout<<"..........Name entered\n";


This way is really only an "I don't know what to do so I'll just throw down something that works" kind of thing. I recommend looking in the reference section for something better.
Dec 10, 2010 at 12:29am
@Browni3141

in these lines:
1
2
cout << "Enter your name: "; cout << name;
cout << "..................Name entered\n"


you can rewrite it as this:
cout << "Enter your name: " << name << "................. Name entered" << endl;

just makes it cleaner you can have as many of the << as you want. as long as it's a cout because you are using the cout from the beginning so it's going to cout everything that you do on that line of code. or you can make it like this (I believe, i've seen other people do something like that, i use the other one though):
1
2
3
4
cout << "Enter your name: " 
     << name 
     << "................. Name entered" 
     << endl;


that's all just your preference.
Last edited on Dec 10, 2010 at 12:32am
Dec 10, 2010 at 12:35am
@ERanz21:
Yeah, I don't know why I wrote it like that, I guess I wasn't really thinking.

Does anyone know of a better way to answer the OP's question?
Dec 10, 2010 at 12:45am
The standard library as far as I know doesn't offer any means of offering the equivalent of a backspace. You would have to either use a different library (curses, but I mean the library called curses; I'm not cursing) or clear the screen and print out everything again with the desired modifications as if nothing happened. If you choose to go with the second option, stringstreams might be of use. :)

EDIT: Have fun! :)

-Albatross
Last edited on Dec 10, 2010 at 12:51am
Dec 10, 2010 at 12:59am
i realize that i put way too many periods, lol. Anyways, @Albatross what is the stringstream that you are talking about?
Dec 10, 2010 at 1:05am
I suggested the stringstream so you can easily store that which has already been printed out to the console, with all the desired exceptions. Combine with an std::string for easy string manipulation!

Here's a tutorial: http://www.dreamincode.net/forums/topic/95826-stringstream-tutorial/

EDIT: Or you could use use a normal std::string.

-Albatross
Last edited on Dec 10, 2010 at 1:06am
Dec 10, 2010 at 2:35am
does it actually allow you to write stuff out to the console?
Dec 10, 2010 at 2:42am
Stringstream? Not on its own, no. You'd have to combine it with something or derive it.

-Albatross
Dec 10, 2010 at 3:42am
oh alright that makes sense.
Dec 10, 2010 at 7:02pm
You can always use some OS API calls to get job done. Example on windows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <windows.h>
#include <iostream>
#include <string>
using namespace std;

void SetCursor(int x, int y)
{
    HANDLE console=GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;

    pos.X=x;
    pos.Y=y;

    SetConsoleCursorPosition(console,pos);
}

void GetCursor(int & x, int & y)
{
    HANDLE console=GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO csbi;

    GetConsoleScreenBufferInfo(console,&csbi);

    x=csbi.dwCursorPosition.X;
    y=csbi.dwCursorPosition.Y;
}

int main()
{
    string name;
    int x,y;

    cout<<"Enter your name: ";
    getline(cin,name);

    GetCursor(x,y);

    y--;
    x=18+name.size();

    SetCursor(x,y);

    cout<<"Name entered\n";

    cin.get();
    return 0;
}
Dec 10, 2010 at 9:39pm
windows.h is heavy and is (for obvious reasons) unportable.
You could download an original Borland copy of conio.h.
Rather lighter, I hear.

Edit:
Hey, a stupid suggestion: How about cout-ing an ASCII backspace character?
What would that do?
Last edited on Dec 10, 2010 at 9:40pm
Topic archived. No new replies allowed.