| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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"); } |
|
#2
|
|||
|
|||
|
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 :) |
|
#3
|
|||
|
|||
|
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
|
|
#4
|
|||
|
|||
|
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 ![]() |
|
#5
|
|||
|
|||
|
your expertise has saved me many hours of cursing Borland C++ Builder, thankyou
|
|
#6
|
|||
|
|||
|
You're welcome
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Infinite loops |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|