Reputation: 938
I have no clue what's going on and I've tried all sorts of things with cin
, including ignore with and without parameters and sync and all sorts of things. All I'm trying to do is getline(cin, str)
. I'm on visual studio express 2013 if that helps anything.
Here's my code:
bool prompt(std::string &str)
{
cout << "> ";
if (cin) cout << endl << "Everything is ok" << endl;
std::getline(std::cin, str);
if (!cin) cout << "cin is failed" << endl;
pause();
return true;
}
Edit: Removed a cin.ignore
and a cin.sync
. Error still happens
The program as of now outputs "everything is ok" and "cin is failed". No pause except for after the "cin is failed" bit.
Any ideas? Thank you very much.
Upvotes: 0
Views: 1333
Reputation: 938
I found out what was going on. Incredibly stupid of me, I completely forgot to realize how I allocated the console in the first place. In my main function:
if (AllocConsole())
{
freopen("CONOUT$", "wt", stdout);
ShowWindow(GetConsoleWindow(), SW_SHOW);
}
I used this snippet floating around the internet forever. However it didn't occur to me that the input stream had yet to be linked to the console. I added this line:
freopen("CONIN$", "rt", stdin);
And that did the trick seemingly.
Upvotes: 0
Reputation: 510
I was able to imitate these results through a few different methods, all of which involve some odd things that redefine cin or the specifically look to set cin to an odd state. The following code will generate this issue:
#include <string>
#include <iostream>
using namespace std;
bool prompt(std::string &str)
{
cout << "> ";
if (cin) cout << endl << "Everything is ok" << endl;
std::getline(std::cin, str);
if (!cin) cout << "cin is failed" << endl;
return true;
}
int main()
{
cin.setstate(ios::eofbit);
string test;
prompt(test);
}
That code generates the output:
>
Everything is ok
cin is failed
This code set the eof flag for cin, which doesn't cause it to be failed so the check for cin returns true, but any attempt to read it immediately fails. The other method involves changing the definition of cin within (or in my case, I copied it and then modified it and included the copy). These are the only ways I was able to figure out to produce the output that your function has in visual studio 2013.
Upvotes: 0
Reputation: 1354
I reproduced this problem on eclipse:
#include <iostream>
#include <string>
using namespace std;
bool prompt(std::string &str)
{
cout << "> ";
if (cin) cout << endl << "Everything is ok" << endl;
std::getline(std::cin, str);
if (!cin) cout << "cin is failed" << endl;
return true;
}
int main()
{
string strIn;
prompt(strIn);
cout << "Input: " << strIn << endl;
}
Now you will get the output:
>
Everything is ok
cin is failed
Input:
whenever you terminate your running program on eclipse. This means that some other process might be terminating your running program.
Upvotes: 0
Reputation: 921
I compiled the following code with Visual Studio Ultimate 2013 Update 3. The pause(); statement is commented out since I do not know what it is.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
bool prompt(std::string &str)
{
cout << "> ";
if (cin) cout << endl << "Everything is ok" << endl;
std::getline(std::cin, str);
if (!cin) cout << "cin is failed" << endl;
// pause();
return true;
}
int main()
{
string strIn;
prompt(strIn);
cout << "Input: " << strIn << endl;
}
When run it, the output is
>
Everything is ok
Input Test.
Input: Input Test.
I do not see the failbit problem. Maybe something is wrong with the Visual Studio Express 2013?
failbit: The input obtained could not be interpreted as a valid textual representation of an object of this type. In this case, distr preserves the parameters and internal data it had before the call. Notice that some eofbit cases will also set failbit.
If enter Ctrl+Z as the input, "cin is failed" does show up:
>
Everything is ok
^Z
cin is failed
Input:
Upvotes: 3
Reputation: 43662
It is true that
if(cin)
std::cout "everything is okay";
would not print the statement if cin
has the fail
or bad
bit set, but not if it has the eof
bit set:
bool prompt(std::string &str)
{
cout << "> ";
if (cin) cout << endl << "Everything is ok" << endl; // Will be printed
std::getline(std::cin, str);
if (!cin) cout << "cin is failed" << endl; // Will be printed
return true;
}
int main()
{
std::string obj;
std::cin.setstate(std::ios::eofbit); // Simulate an eof/Ctrl^Z on Win read
prompt(obj);
}
a previous operation might have left it set, this was also discussed in one of your previous topics.
To ensure cin
is in a valid state you should first unset its state bits with cin.clear();
bool prompt(std::string &str)
{
cin.clear(); // Reset state bits
cout << "> ";
if (cin) cout << endl << "Everything is ok" << endl;
std::getline(std::cin, str);
if (!cin) cout << "cin is failed" << endl;
return true;
}
if your code still happens to fail, make sure you're actually reading a valid textual input and avoid reading invalid characters.
Upvotes: 0