Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 30th, 2006, 3:50 PM   #1
fhslacrosse13
Newbie
 
Join Date: Oct 2006
Posts: 25
Rep Power: 0 fhslacrosse13 is on a distinguished road
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;
}
fhslacrosse13 is offline   Reply With Quote
Old Nov 30th, 2006, 4:09 PM   #2
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3 kruptof is on a distinguished road
Quote:
Originally Posted by fhslacrosse13 View Post

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
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Nov 30th, 2006, 6:08 PM   #3
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
There is no call to cin.get inside your inner loop, so it will loop forever.
The Dark is offline   Reply With Quote
Old Nov 30th, 2006, 6:31 PM   #4
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
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.
Game_Ender is offline   Reply With Quote
Old Dec 1st, 2006, 4:24 PM   #5
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4 mackenga is on a distinguished road
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.
__________________
"I'm not a genius. Why do I have to suffer?"
mackenga is offline   Reply With Quote
Old Dec 1st, 2006, 4:45 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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."
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 1st, 2006, 4:55 PM   #7
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by DaWei View Post
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.
The Dark is offline   Reply With Quote
Old Dec 1st, 2006, 5:14 PM   #8
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3 kruptof is on a distinguished road
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).
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Value of index incorrect after loop aznluvsmc C 13 Nov 6th, 2005 9:47 PM
Help in QBASIC (I think it's similar to VB) phoenix987 Visual Basic 3 May 9th, 2005 12:33 PM
Help with a QBASIC program phoenix987 Other Programming Languages 4 May 5th, 2005 12:27 PM
WinSock accept() hangs program in Do loop... layer C++ 5 Apr 29th, 2005 11:28 AM
Timing loop problems badbasser98 C++ 11 Mar 10th, 2005 8:30 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:25 PM.

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