C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 1st, 2006, 09:43 AM
nicehat nicehat is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 3 nicehat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 28 sec
Reputation Power: 0
Infinite loops

when executed the below code should validate a postal code, currently set to accept in the format: 'AAN NAA' where N = digit, A = letter, and the space must be included. unfortunately incorrect entries spark an infinite loop..
i am inexperienced in C++ so if anyone has any ideas on the source of this error or how to procede correcting it.. i have exhausted all ideas. thanks in advance if anyone can help.

#include <iostream>
#include <cmath>
#include <cctype>
#include <string>

using namespace std;

char postcode[10];
int postflag = 0;
bool validpostcode = false;

bool isValidPostcode(char postcode[])
{
if ( isalpha(postcode[0]) & isalpha(postcode[1])
& isalpha(postcode[5]) & isalpha(postcode[6]) )
{
if (isdigit(postcode[2]) & isdigit(postcode[4]) )
{
if ( isspace(postcode[3]) )
{
postflag = 1;
return true;
}
else
{return false;}
}
else
{return false;}
}
else
{return false;}
}


void main()
{



while(validpostcode == false)
{
cout << endl << "enter postcode :" << endl;
cin.get (postcode,10);
validpostcode = isValidPostcode(postcode);
cout << validpostcode << endl;
cout << postflag << endl;
}


system ("pause");
}

Reply With Quote
  #2  
Old April 1st, 2006, 10:55 AM
ossinator ossinator is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 40 ossinator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 11 m 12 sec
Reputation Power: 4
Send a message via ICQ to ossinator
You're using the wrong and operator
& (single ampersand) is used for bitwise and.
the logical and, which you want to use is && (two ampersands)

furthermore, i'd modify your code a little, in order to be more readable, like so:

bool IsValidPostcode(char Postcode[])
{
if (isalpha(Postcode[0]) && isalpha(Postcode[1]) && isalpha(Postcode[5]) && isalpha(Postcode[6]) && isdigit(Postcode[2]) && isdigit(postcode[4]) && isspace(Postcode[3]))
{
postflag = 1;
return true;
}
else
{
return false;
}
}
It is much more intuitive to have all conditions regarding the same thing to be in one if construction. Besides, the paths that you could hit if some of the if's isn't true are always the same, which really means you need only 1 if construction. If you wanted some error reporting, then you could use separate if constructions. You could go without the else {} block, and put the return false outside it, but it's much more readable this way.

Also, if you want to do error identification, i'd put it in a loop, and use switch case to examine the position and see whether it should be alpha, num or whitespace, and then error report it, because that way you can know at which position was the error.

Hope that helps, good luck :)

Reply With Quote
  #3  
Old April 2nd, 2006, 07:00 AM
nicehat nicehat is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 3 nicehat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 28 sec
Reputation Power: 0
its still broken but thanks for that, the '&' mistake would've caused much grief further down the line. as far as i can see the code executes correctly, it just doesnt stop to wait for user input again after an incorrect postcode entry. i tried alternatives to cin.get with no success

Reply With Quote
  #4  
Old April 2nd, 2006, 07:55 AM
ossinator ossinator is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 40 ossinator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 11 m 12 sec
Reputation Power: 4
Send a message via ICQ to ossinator
Hm..
I tried compiling it, and can't figure out why it happens so, i think it has something to do with the cin buffer.
Anyhow, as a workaround, do it like this:

while(validpostcode == false)
{
cout << endl << "enter postcode :" << endl;
cin.getline(postcode,10);
validpostcode = isValidPostcode(postcode);
cout << validpostcode << endl;
cout << postflag << endl;
}
system ("pause");

I'll try to understand why it doesent work the other way and post here again.


Edit Later:

I figured it out.
using cin.get without a delimiter doesen't flush the input stream. So we read empty lines forever, unless we say that '\0' is a delimiter and we want cin.get to stop reading there.

The solution:

If you want to use cin.get, use it like this:

cin.get (postcode,8,'\0');


We use 8 as the num characters, because get(), when used with a delimiter character, reads up to num-1 characters or the delim symbol, and if we say we need 10, we'll get asked for the other 2, but we want only 7.

Hope that shines a little light on the matter :>

ps. I noticed you included a lot of header files - cmath,cctype,string - Do you really need them? If you have other code besides this that utilizes them, it's ok, but if not, there's no use for them, you need only iostream for that program
Comments on this post
B-Con agrees!

Reply With Quote
  #5  
Old April 3rd, 2006, 02:47 PM
nicehat nicehat is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 3 nicehat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 28 sec
Reputation Power: 0
your expertise has saved me many hours of cursing Borland C++ Builder, thankyou

Reply With Quote
  #6  
Old April 3rd, 2006, 03:06 PM
ossinator ossinator is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 40 ossinator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 11 m 12 sec
Reputation Power: 4
Send a message via ICQ to ossinator
You're welcome

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Infinite loops


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT