Hi!

I'm working on a project and need a way to add a newline to the input stream, by code and not by user's help.

The user should not need to do anything, I need a simple line of code that places a newline in the input stream, but I don't know how.

I meen like cin.get(), but without the user need to press a key.


Can anybody help?

Recommended Answers

All 6 Replies

you mean if the user types "Hello World" <Enter key>, you want to add '\n' to the end of the string?

Not really.

I've used cin.get() in a function, which is called several times during a full program run. After the first run of this function cin.get() is discarded somehow.
But after that, the user will have to press a key, and I don't want that.

Well, if you don't want the user to press a key then delete the cin.get() lines -- that's what that function is used for.

>>cin.get() is discarded somehow.
Probably because a previous cin operation was for an integer, which leaves the '\n' in the keyboard buffer, and the next cin.get() will remove that '\n' from the keyboard buffer and toss it away.

int num;
cout << "Enter a number\n";
cin >> num;
cin.ignore(); // flush the '\n' from keyboard buffer

Thanks!

If you don't mind, I have another question.

I have a function that has a vector<vector<int> > return type.
Later I call this function from another function, which shall declare a new variable and copie the last function's return value.

Ex.

vector<vector<int> > f (...) {
...
}

void g (f() ) {
vector<vector<int> > x = f();
...
}

But this will cause a out of range error, but I can't fix it.

Please help!

make it a reference parameter, not a return value

void f(vector<vector<int>> & ay)
{
   // fill in the vector
}

int main()
{
    vector<vector<int>> ay;
    f( ay );
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.