The number part executes nicely but whenever I put strings into the input....the while starts an infinite loop.
btw_ this is for parsing the input for a calculator program...
Any idea what is going wrong...or a better way to put the different parts of the input line to be analysed separately later?
then the stream would start right at the beginning again and would never meet the .eof() condition of the loop.Is there a way to take it back through one operation ...which I could use in the beginning of the else block?... seems that the >> is used twice every time a string is encountered.
I was wrong about that....recently learnt that it would only clear the last read....in this case ,the error of trying to read a string into a double...
st >>x,x first reads x from the stream and then turns up a true condition if it is double...and a false if it is a string..
When an expression has a , within it, the left part is evaluated but doesn't return anything...the condition of the if block comes from the nature of x.
operator >> returns a reference to istream object which is convertible to a boolean. When an error occurs, This bool = false. Your code gets input and then checks if that input is 0.
Try compiling this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <limits>
int main(){
double i;
while(true){
if(std::cin >> i, i){
std::cout << "y" << i << "\n";
}else{
std::cout << "n" << i << "\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
return 0;