![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2006
Posts: 25
Rep Power: 0
![]() |
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;
} |
|
|
|
|
|
#2 | ||
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3
![]() |
Quote:
i put the curly braces in...........very badly i know
__________________
Quote:
|
||
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
There is no call to cin.get inside your inner loop, so it will loop forever.
|
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
Umm... if we are being anal about it this:
if(palindrome (S,Q)) cout<<"Palindrome!"<<endl; else cout<<"Not Palindrome"<<endl; |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4
![]() |
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?" |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#7 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
|
|
|
|
|
|
#8 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3
![]() |
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:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |