473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to recover from failing cin.get(str,n,'\n')?

How to recover from failing cin.get(str,n,'\n')?
The user pressed "Enter" on cin.get(...).I would like to let him try
once more but the system doesn't stop on the next cin.get. Code:

cin.get(str,n,'\n');
cout<<"haha\n";
char c;
/*
Now get '\n'. Doesn't work if the first time nothing else was input.
Have this line or leave it -it doesn't change the outcome in this case.
*/
cin.get(c);
cin.get(str,n,'\n'); //here doesn't wait if the first time Enter was
pressed.

--
Best regards,
Alex.

PS. To email me, remove "loeschedies" from the email address given.
Jul 22 '05 #1
5 2234

"Alexander Malkis" <al*****************@stone.cs.uni-sb.de> wrote in message
news:c4************@hades.rz.uni-saarland.de...
How to recover from failing cin.get(str,n,'\n')?
The user pressed "Enter" on cin.get(...).I would like to let him try
once more but the system doesn't stop on the next cin.get. Code:

cin.get(str,n,'\n');
cout<<"haha\n";
char c;
/*
Now get '\n'. Doesn't work if the first time nothing else was input.
Have this line or leave it -it doesn't change the outcome in this case.
*/
cin.get(c);
cin.get(str,n,'\n'); //here doesn't wait if the first time Enter was
pressed.


if std::istream::get(T*, std::streamsize, T) does
not extract any elements, it sets std::istream::failbit.
(This function does not extract the delimiter T).
No further i/o will be performed until the stream state
is reset.

#include <ios>
#include <iostream>
#include <limits>
int main()
{
char str[100] = {0};
std::streamsize n(sizeof str);

std::cin.get(str, n, '\n');
std::cout << "one\n";

std::cin.clear();

std::cin.ignore
(std::numeric_limits<std::streamsize>::max(), '\n');

std::cin.get(str, n, '\n');
std::cout << "two\n";
return 0;
}

Rather than a simple 'std::cin.get()' to remove the newline,
I've used 'std::istream::ignore' instead, which will remove
any pending characters, the number of which might have exceeded
the value of the type 'streamsize' argument.

You could avoid all these machinations by using the
'std::getline()' nonmember function declared by <string>,
which stores the input in a 'std::string' object, and
does extract (but does not store) the newline character.

#include <iostream>
#include <string>

int main()
{
std::string line;
std::getline(std::cin, line);
std::getline(std::cin, line);
return 0;
}

-Mike
Jul 22 '05 #2

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:Ud*****************@newsread2.news.pas.earthl ink.net...
You could avoid all these machinations by using the
'std::getline()' nonmember function declared by <string>,
which stores the input in a 'std::string' object, and does extract (but does not store) the newline character.


Should be:

does extract (but does not store) the delimiter
(a newline by default) character.

-Mike
Jul 22 '05 #3
Mike Wahler wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:Ud*****************@newsread2.news.pas.earthl ink.net...
You could avoid all these machinations by using the
'std::getline()' nonmember function declared by <string>,
which stores the input in a 'std::string' object, and


does extract (but does not store) the newline character.

Should be:

does extract (but does not store) the delimiter
(a newline by default) character.

-Mike

Thanks!

--
Best regards,
Alex.

PS. To email me, remove "loeschedies" from the email address given.
Jul 22 '05 #4
Actually, I used
cin.getline(char_type* __s, streamsize __n, char_type __delim);
somewhere from std_istream.h

--
Best regards,
Alex.

PS. To email me, remove "loeschedies" from the email address given.
Jul 22 '05 #5
Actually, I used
cin.getline(char_type* __s, streamsize __n, char_type __delim);
somewhere from std_istream.h

--
Best regards,
Alex.

PS. To email me, remove "loeschedies" from the email address given.
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Chris Curvey | last post by:
I've looked at this so long that I must be looking right past it. This code: class Page: ########################################################## def write(self, value, row=None, col=None,...
3
by: Tuxtrax | last post by:
Hi all. I have need of assistance on something that should be very simple, and is driving me absolutely batty. This is a code snippet from a short program that downloads news headers via...
2
by: Steve S | last post by:
It always returns false...*pvParam never gets populated. Any ideas? int temp; char *pvParam = ""; try
5
by: Alexander Malkis | last post by:
How to recover from failing cin.get(str,n,'\n')? The user pressed "Enter" on cin.get(...).I would like to let him try once more but the system doesn't stop on the next cin.get. Code: ...
4
by: Paul Thompson | last post by:
I am getting an odd, inscrutable error in Mozilla Firefox. When I use an array to shift focus to an element, I get the error Error: " nsresult: "0x8057001e (NS_ERROR_XPC_JS_THREW_STRING)" ...
17
by: jake1138 | last post by:
Here is a function I have to get a number at the end of a string. I'm posting this in case it proves helpful to someone. Comments are welcome. int getnum(char *str) { char buffer; char *buf...
3
by: lans redmond | last post by:
I have the following which works in a jsp page var obj = bew ActiveXObject(Tool.parsedatService); var str = obj.test(); alert(str); this prints out a string that i return from my activeX dll...
3
by: Tony Girgenti | last post by:
Hello. I'm developing a web comsuming client using VS.NET 2003(VB), .NET Framework 1.1.4322, ASP.NET 1.1.4322, WSE2.0 on a WinXP Pro Sp2 computer. Using this code that i copied from a book: ...
0
by: Paavo Helde | last post by:
I get strange error in VS2008: it seems localeconv() is plain failing after a _configthreadlocale() call. Am I missing something? Test code follows: #include <iostream> #include <locale.h>...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.