Problem of using cin twice
Good writeup that explains some of the reasons why you are running into this issue, primarily due to the behavior of the input types and that you are mixing them
How to make it read cin twice?
I bet you did not read the i character from the input stream
Using cin.get(); Twice
cin.get();
retrieves a single character from the input. So if you have 5\n
(\n
being equivalent to pressing ENTER) on the input, cin.get();
will return 5
, and another cin.get();
will return \n
. If you are reading multiple numbers one after another, say in a while
loop, if you forget about the \n
character, you are likely going to run into issues.
Using cin.ignore(256, '\n');
can also correct this issue once you are done reading the characters you want or care about.
how can i use (!(cina)) twice times?
It seems you mean the following
#include <limits>
//...
if ( not ( std::cin >> arr[i] ) )
{
//...
std::cin.clear();
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
}
Problem while taking input using cin after detaching that thread in C++
In your case you should use atomic shared variables across threads.
The std::cin
is blocking operation. In your code two seconds later doing detach for thread1. But std::cin
still waiting for input...
The console window can std::cin
or std::cout
but not both at the same time so you need to change algorithm to avoid this problem.
cin double and string without space results in error
It's a bug in libc++ (reported back in 2013 I might add).
If a number is followed by a letter in an input stream without an intervening space, then reading this number may or may not fail, depending on the letter. The "bad" letters are A through F, I, N, P, and X (and their lower case counterparts).
An astute reader will notice that those are exactly the letters that can make up hexadecimal constants (yes P is one of them) and the words NAN and INF.
Among other characters that behave the same way are the plus sign, the minus sign, and the decimal point (the reason is obvious).
Cin making me enter twice
It's because you're asking them twice!
cin >> distanceTraveled;
while ((!(cin >> distanceTraveled)) ...
Try:
// assuming that distanceTraveled == 0 is invalid input
while ((cin >> distanceTraveled) && (distanceTraveled == 0))
And if you're trying to clear out any other input they entered on a line, you can use getline
instead.
std::string junk;
std::getline(cin, junk);
std::cout << "ERROR: invalid Entry. Try again.\n\nHow far did ya go? ";
So the code would now look like this:
std::string junk;
std::cout << "How far did you go? ";
while ((cin >> distanceTraveled) && (distanceTraveled == 0))
{
std::getline(cin, junk);
std::cout << "ERROR: invalid Entry. Try again.\n\nHow far did ya go? ";
}
Related Topics
How to Embed Webkit into My C/C++/Win32 Application
What Is the Most Efficient Way to Display Decoded Video Frames in Qt
General Rules of Passing/Returning Reference of Array (Not Pointer) To/From a Function
What Does the Void() in Decltype(Void()) Mean Exactly
Why, Really, Deleting an Incomplete Type Is Undefined Behaviour
Difference Between 'Strcpy' and 'Strcpy_S'
C++ Pure Virtual Function Have Body
Efficiently Getting All Divisors of a Given Number
How to Break When a Specific Exception Type Is Thrown in Gdb
What's the Difference Between Opening a File with iOS::Binary or iOS::Out or Both
How to Implement Timeout for Function in C++
How to Build Google's Protobuf in Windows Using Mingw
Wrapping C++ Class API for C Consumption
How to Calculate the Angle from Rotation Matrix
Differencebetween "Include Directories" and "Additional Include Directories"