Thread: Strange loop

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    14

    Unhappy Strange loop

    Hi i strated with c++ yesterday. know im trying to make a script that enter your date of bearth and check it of its correct (more script will come later). Now i create this script:

    Code:
    #include <iostream>
    using namespace std;
    
     
    
    int main(){
    int jaar;
    for(int x=0; x<1;){
    if(jaar <1 && jaar > 2004){
    x=1;
    }
    else{
     cout<<"Jaar:";
     cin>> jaar; 
     cin.ignore(); 
    }
    }
    cout<<jaar;
      cin.get();
    }
    It works good but when your press a letter ore something its keeps couting "jaar:" without anything. Im probaly making a mistake somewhere please help me.

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    14
    Does nobody know what im doing wrong ?

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    14
    Oke i gonne ask my queston on a other way:

    why is that script keep repeating this step: cout<<"Jaar:";
    When i enter as "jaar" a letter ?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    wow....it's 6 in the morning and you wonder why no one has helped yet.....

    just for that, we'll tell you how to fix it tommorow
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    14
    Oh sorry, here its 13.26 i dident know youre sleeping

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int jaar;
    for(int x=0; x<1;)
    {
        if(jaar <1 && jaar > 2004){
            x=1;
        }
        else{
            cout<<"Jaar:";
            cin>> jaar; 
            cin.ignore(); 
        }
    }
    You should initialize jaar to something before entering the loop, otherwise it has junk in it when you get to the if test. Also, the if test itself does not make sense... how can a value ever be less-than 1 and also greater-than 2004? This test can NEVER result in a true outcome and therefore x will never get set to 1 and therefore you will never exit the loop, perhaps you meant to use || instead of &&. Also, a different loop would be more approriate than a for loop.

    I would suggest something like this:

    Code:
    int jaar;
    do
    {
        cout<<"Jaar:";
        cin>> jaar; 
    } while( jaar < 1 || jaar > 2004 );
    Last edited by hk_mp5kpdw; 12-17-2004 at 09:19 AM.
    "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

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Chill D@rk_force, your question has been answered and you have been flamed, but who says its not too late to do both again Why are you looping once? What I mean is why did this seem like a good way to do this?

    Also, spamming the boards won't make us answer any faster. Typically when there is no responce it means that either it was 02:46 when you first asked, or no one knows. In this case it was just a wee bit early.

    > just for that, we'll tell you how to fix it tommorow
    I agree, which is why I more helped him with another error that wasn't erroneous as much as poor design in lieu of giving solid help.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    14
    Quote Originally Posted by master5001
    Chill D@rk_force, your question has been answered and you have been flamed, but who says its not too late to do both again Why are you looping once? What I mean is why did this seem like a good way to do this?

    Also, spamming the boards won't make us answer any faster. Typically when there is no responce it means that either it was 02:46 when you first asked, or no one knows. In this case it was just a wee bit early.

    > just for that, we'll tell you how to fix it tommorow
    I agree, which is why I more helped him with another error that wasn't erroneous as much as poor design in lieu of giving solid help.
    But i dident know that you where sleeping sorry ...

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Even then, posting a "what's up" 7 minutes after the initial post is useless. Yes, this is a rather high-traffic forum, but on other forums I know you have to wait hours for an answer, because the member base is rather small there.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    14
    Quote Originally Posted by CornedBee
    Even then, posting a "what's up" 7 minutes after the initial post is useless. Yes, this is a rather high-traffic forum, but on other forums I know you have to wait hours for an answer, because the member base is rather small there.
    oke, im sorry. but i still dont has an asnwer on my question ...

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    But you do, actually. You never gave jaar a value. Plus your code won't work anyway unless you make it more like what hk_mp5kpdw suggested. His code allows you to input a value and does a range check.

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    14
    I did test this script:

    Code:
    #include <iostream>
    
    using namespace std;
    
     
    
    int main(){
    
     int jaar;
    do
    {
        cout<<"Jaar:";
        cin>> jaar; 
    } while( jaar > 1 || jaar < 2004 );
    cin.get();
    
    }
    and when i press a non numeric character its exit, why i dont know

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Wt... you mean this program ever closed? I can't believe that. An infinite loop if I ever saw one.

    Read up on the logical and comparison operators and what they do. Then think why your program can't work.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Dec 2004
    Posts
    8
    Code:
    cin>>Jaar;
    while(!cin)
    {
       cin.clear();
       cin.ignore(100, '\n');
       cout<<"Please reenter answer."<<endl;
       cin>>Jaar;
    }

  15. #15
    Registered User
    Join Date
    Dec 2004
    Posts
    14
    Thanks terran9 that script worked, dident know what cin.ignore was for ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM