The >> operator always returns a value, and thus the condition of the while loop will always be met. Do you want to stop the loop when the user enters '*'? Then you could add
You have a problem in your second loop. Because you are using b.size() to test your variable "i", it stops when i == b.size()!. Try saving b.size into an int before the loop and use it for your test:
1 2 3 4 5 6 7 8 9 10 11
int thisMany = b.size();
for( i = 0; i < thisMany; i++ )
{
cout << b.front() << endl;
b.pop();
} /* for( i < thisMany ) */
Please compile this snippet with integers and then a letter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Example program
#include <iostream>
int main()
{
int test;
while(std::cout << "Please enter an integer: " && std::cin >> test)
{
std::cout << test << " is an integer\n";
}
std::cout << "a non-integer value has been entered." << std::endl;
return 0;
}
Though in the case of it being a character you would have to use ctrl-z to break out of it.
@OP If you are trying to move all characters from a string to a queue ignoring the asterisks you could do something like
1 2 3 4 5 6 7 8 9 10
for(charconst &ch : str)
{
if(ch != '*') b.push(ch);
}
while(!b.empty()) //not empty
{
cout << b.front() << ' ';
b.pop();
} //if only c++ returned on pop like every other language for queue :P
@gibit I know that; however the code to see if cin is not good is much more clumsy if you put it on one line. If cin >> a has an error the first time, it still returns true -- try it and see!