-
Loops
Hi,
i got my program to loop round back to the begining if the user presses yes.
it will ask the user for their name, and address, postcode, etc.
when i type in the address it goes into an infinate loop around the postcode, this only happens when i loop round the program for the second time.
does anyone know the reason for this and what i can do to stop it?
Thanks
-
Post some code. One possibility... there is a trailing newline character in the input stream that isn't being dealt with.
-
here is the code, sorry it a bit long but i thought i sohuld but alot of it in just incase it was summat else.
Code:
do
{
cout << "Please enter your initials and surname: ";
cin.getline(szname, 20);
cin.ignore();
cout << "Enter your address:";
cin.getline(szaddress1, 150);
cin.ignore();
cout << endl;
/////////////////// validation loop for post code////////////////////////////////
do
{
cout << "Please enter your postcode" <<endl;
cin.getline (postcode, 8);
} while(!isValidPostcode(postcode));
bool isValidPostcode(char postcode[])
{
int errorcounter = 0;
if(isalpha(postcode[0]))
{
}
else
{
errorcounter++;
}
if(isalpha(postcode[1]))
{
}
else
{
errorcounter++;
}
if(isdigit(postcode[2]))
{
}
else
{
errorcounter++;
}
if (postcode[3] == ' ')
{
}
else
{
errorcounter++;
}
// no space so check remainding 3 chars
if (isdigit(postcode[4]))
{
}
else
{
errorcounter++;
}
if (isalpha(postcode[5]))
{
}
else
{
errorcounter++;
}
if (isalpha(postcode[6]))
{
}
else
{
errorcounter++;
}
if (errorcounter ==0)
{
return true;
}
else
{
cout <<"the postcode is INVALID please enter in again";
cout <<endl;
return false;
}
}
-
Where is the rest of that outermost do/while loop? What are you entering as input (starting with the input when you first go into the loop)? What happens when you enter that input (step-by-step)? What do you expect to see as far as output versus what you actually see?
-
Do you really need all those spaces? You are making your program twice the length just by adding in loads of empty lines.
-
must be something like
Code:
cin.ignore(100, '\n'); // the number must be at least larger than 1
-
The cin.ignore(); calls after the calls to getline are unnecessary. You only need them after calls to cin >> because cin >> doesn't throw away the newline at the end of the user input like getline does. In this case, the ignore will cause the first character of the next input to be ignored. This could be causing the postcode to be invalid.
-
hi thanks, i got that sorted but now i have another problem and that is it moves the name part into the address and dosnt show the address for some reason
anyone knw why this is?
-
If you have a call to cin >> before the getline that gets the name, then you do have to have a cin.ignore() after that call to cin>>.
Remember, you add cin.ignore() after calls to cin >>, but not after calls to cin.getline().
-
well acutally it dosnt read the initials part not the address sorry my mistake.
-
My guess is still the same. Perhaps you can post your current code (a more complete version) and the input you are giving it.