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

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.
You can't. cin doesn't get any input until the user presses enter.
So this is like a console thing? When the console receives enter signal it automatically moves to a new line?
Yes. Enter writes a '\n'.
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.
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.
@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
@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?
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
i realize that i put way too many periods, lol. Anyways, @Albatross what is the stringstream that you are talking about?
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
does it actually allow you to write stuff out to the console?
Stringstream? Not on its own, no. You'd have to combine it with something or derive it.

-Albatross
oh alright that makes sense.
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;
}
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
Topic archived. No new replies allowed.