Loops

This is a discussion on Loops within the C++ Programming forums, part of the General Programming Boards category; Hi, i got my program to loop round back to the begining if the user presses yes. it will ask ...

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    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

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Post some code. One possibility... there is a trailing newline character in the input stream that isn't being dealt with.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    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;
    
            }
    
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    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?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Do you really need all those spaces? You are making your program twice the length just by adding in loads of empty lines.

  6. #6
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Code:
    cin.ignore();
    must be something like

    Code:
    cin.ignore(100, '\n');  // the number must be at least larger than 1
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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.

  8. #8
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    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?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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().

  10. #10
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    well acutally it dosnt read the initials part not the address sorry my mistake.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    My guess is still the same. Perhaps you can post your current code (a more complete version) and the input you are giving it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 12:18 AM
  3. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM
  4. for loops in C
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-15-2001, 05:09 PM
  5. exiting loops with ease?
    By KingRuss in forum Game Programming
    Replies: 3
    Last Post: 09-24-2001, 08:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21