While

Hello,
My while is endless and i can't understand why.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include<iostream>
#include<queue>
using namespace std;

int main(){
 int i;
 char a;
 queue <char> b;

 while(cin>>a){
    if(a!='*')
        b.push(a);
 }

 for(i=0;i<b.size();i++){
    cout<<b.front()<<endl;
    b.pop();
 }

}
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

else break;

After your if statement.
i have to insert some characters and if the character it's not an '*' push it into the queue and at the end print the queue.
BasV is right: Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

    while( 1 )
        {
        cin >> a;

        if( cin.good() )
            {
            // do your character-checking logic

            }    /*    if( cin.good() )    */
        else
            {
             break;

            }    /* if( ! cin.good() )    */

        }    /* while( 1 )    */




still wrong. it only prints the first character.
i changed a little bit my code and it's closer to what i want but still wrong.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<queue>
#include<string>
using namespace std;

int main(){
 int i;
 string n;
 queue <char> b;

cin>>n;

for(i=0;i<n.size();i++){
    if(n[i]!='*')
        b.push(n[i]);
}

for(i=0;i<b.size();i++){
    cout<<b.front()<<endl;
    b.pop();

 }
 }
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 )    */


Note that there are many other ways to do this.
Last edited on
@Koothkeeper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while( 1 )
        {
        cin >> a;

        if( cin.good() )
            {
            // do your character-checking logic

            }    /*    if( cin.good() )    */
        else
            {
             break;

            }    /* if( ! cin.good() )    */

        }    /* while( 1 )    */


is the same as

1
2
3
4
while(cin >> a)
{
    // do your character-checking logic
}


http://www.cplusplus.com/reference/ios/ios/operator_bool/


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(char const &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 
Last edited on
@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!
Last edited on
Topic archived. No new replies allowed.