Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Very newb question about std::cin

Posted on 2003-11-19
5
Medium Priority
?
460 Views
Last Modified: 2010-04-02
I used:

std::cin >> someStdStringVar;

and then I follow it up with another prompt and

std::cin.getline(someCharArray,maxCharsToExtract);

If you've used std::cin frequently you'll know that this won't work. There are still characters left in the buffer (2 in my case) and my bet is that they are the \r\n pair not stored into someStdStringVar previously.

The only way I managed to get them out of there was to add another getline() call before the one I'm interested in.

I tried std::cin.seekg(0,std::ios_base::end); and also std::cin.ignore(INFINITE); before the call to getline() but both caused the following call to getline() to fail to block while waiting for input and filled the buffer with nothing.

I don't mind calling getline() and extra time, but it seems crude. Why is seekg() not working as I expect it to?

thanks,
-Sandra

0
Comment
Question by:Sandra-24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 13

Accepted Solution

by:
SteH earned 1200 total points
ID: 9785392
I would guess that
std::cin.ignore (INFINITE, '\n');
should work. From cin I wouldn't expect any EOF since it is a stream but not a file.
0
 
LVL 3

Author Comment

by:Sandra-24
ID: 9785466
Nope does the same as the rest :(

Using cin is not as simple as it looks.
0
 
LVL 1

Expert Comment

by:Hoegje
ID: 9785481
maybe this will help :

std::cin.flush(); // flush the complete stream

Not sure though !
0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 800 total points
ID: 9786071
This works for me, Sandra:
--------8<--------
#include <iostream>
#include <limits>

int main()
{
        std::string str1,str2;
        using std::cin;
        const int INFINITE = std::numeric_limits<int>::max();

        cin >> str1;
        cin.ignore(INFINITE,'\n');
        getline(cin,str2);
        std::cout << "str1 [" << str1 << "]\nstr2 [" << str2 << "]\n";
}
--------8<--------

Can you post a simple example of your problem?
0
 
LVL 3

Author Comment

by:Sandra-24
ID: 9789633
You guys are right. The trouble was that I didn't define INFINITE in my program. Ordinarily that would prevent it from compiling, but it worked because a define of INFINITE does exist 0xFFFFFFFF which proabably wrapped to a negative number (or so I'm guessing). At any rate it's working now, thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

618 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