Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   my loop exits early (http://www.programmingforums.org/showthread.php?t=12064)

fhslacrosse13 Nov 30th, 2006 4:50 PM

my loop exits early
 
I'm working on a program that reads in a line of text from the user and then runs a free function(palidrome) to see if the text entered in a palidrome. The loop should prompt user for line of text then run palidrome which returns a bool, tell the user if it is a palidrome, then askes the user for another line of text until <ctrl-z> is entered. for some reason it askes for the palidrome and then exits. Can't figure it out.

:

int main(){
        myQueue<char> Q;
        myStack<char> S;
        char ch;
        cout<<"Enter lines of text - enter <ctrl-Z> to quit: "<<endl;
        cin.get(ch);
        while(ch!='\n'){
        while(cin){
                if(!isspace(ch)){
                        Q.push(ch);
                        S.push(ch);
                }
                if(palindrome (S,Q))
                        cout<<"Palindrome!"<<endl;
                else
                        cout<<"Not Palindrome"<<endl;
        }
                cout<<"Enter lines of text - enter <ctrl-Z> to quit: "<<endl;
                cin.get(ch);
        }
        return 0;
}


kruptof Nov 30th, 2006 5:09 PM

Quote:

Originally Posted by fhslacrosse13 (Post 120161)

:

int mainmyQueue<char> Q;
        myStack<char> S;
        char ch;
        cout<<"Enter lines of text - enter <ctrl-Z> to quit: "<<endl;
        cin.get(ch);
        while(ch!='\n')
    {
            while(cin)
        {
                        if(!isspace(ch))
            {
                      Q.push(ch);
                  S.push(ch);
                        }

                        if(palindrome (S,Q))
                        {
                                cout<<"Palindrome!"<<endl;
                        }
                        else
                        {
                                cout<<"Not Palindrome"<<endl;
                        }

                        cout<<"Enter lines of text - enter <ctrl-Z> to quit: "<<endl;
                        cin.get(ch);
                }
        }
        return 0;
}



i put the curly braces in...........very badly i know

The Dark Nov 30th, 2006 7:08 PM

There is no call to cin.get inside your inner loop, so it will loop forever.

Game_Ender Nov 30th, 2006 7:31 PM

Umm... if we are being anal about it this:
:

if(palindrome (S,Q))
        cout<<"Palindrome!"<<endl;
else
        cout<<"Not Palindrome"<<endl;

If perfectly good style in quite a few circles, but only if both statements are only one line each.

mackenga Dec 1st, 2006 5:24 PM

Yup, I like the cleaner braceless look too for short conditions; I actually think it's clearer and easier to read. A lot of people disagree with us about that though.

DaWei Dec 1st, 2006 5:45 PM

I agree that it's cleaner. If the block is short, I may even put it on the same line. At the same time, I recommend that novices go for the braces, even for one liners. This insures that if they add a debug statement, the suddenly-longer block will still be a block.

On the other hand, I would certainly never unilaterally add unnecessary braces to another's post, in the name of "correcting a mistake."

The Dark Dec 1st, 2006 5:55 PM

Quote:

Originally Posted by DaWei (Post 120295)
On the other hand, I would certainly never unilaterally add unnecessary braces to another's post, in the name of "correcting a mistake."

Especially when adding tabs so the braces don't line up in the browser, making the result harder to read than the original.

kruptof Dec 1st, 2006 6:14 PM

hey i already said, if i knew how to delete that post i properly would, and i already said it was really bad. curly braces make it easier for others to look over your work (okay don't refer to my example....i cannot over emphasises how wrong it is).


All times are GMT -5. The time now is 1:39 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC