Stuck in an infinite while loop. Help.

This is a discussion on Stuck in an infinite while loop. Help. within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> #include <conio.h> int main(){ int firstyer = 0; cout<< "Enter -1 to stop program "; cin>>firstyer; while ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    Stuck in an infinite while loop. Help.

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    
    
    int main(){
    
    int firstyer = 0;
    
          cout<< "Enter -1 to stop program ";
          cin>>firstyer;
    while (firstyer != -1)
    
          if (firstyer==1600|| firstyer == 2000||firstyer== 1756||firstyer ==1832||firstyer==544||firstyer == 3380)
          {
          cout<<"The year is a leap year"<<endl;
          }
          else if (firstyer==1700 || firstyer == 2033||firstyer==1962)
          {
          cout<< "The year is not a leap year"<<endl ;
          }
          else
          {
          cout<< "Number not applicable, enter a valid year.  1600, 1700, 2000, 1756, 2033, 1962, 1832, 544, 3380 only"<<endl;
          }
    
          cout<< "Enter -1 to stop program ";
          cin>>firstyer;
         getch();
         return 0;
    }
    I get an infinite message after one of the IFs is true. I need it to repeat the loop by asking to enter a new number after one of the IF statements is true until -1 is entered.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    You didn't put braces around the code you want to be inside the loop. Just indenting it isn't enough. How Pythonic of you

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    first remove the .h's in the include statements, as this is c++ not c. next, your while loop needs block braces around the code block you want to loop, which is why you get an infinite loop now. it only reads the input once then keeps looping, but the value is never changed (inputted again).

    also having conio makes it windows-only code.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Wow I can't believe I didn't see that. Thnx guys. I need more practice in recognizing stupid mistakes like this.

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    No matter how much practice you get, you will still always make stupid mistakes, just with practice you stop making the ones that are easy to find

    Last stupid mistake I made was yesterday, used a pointer to a double as an input to scanf which was recieving a flloat and couldnt figure out why every value I got was printing out as zero, lol.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by abachler View Post
    just with practice you stop making the ones that are easy to find
    No, you won't. You'll just find them faster.
    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

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    A bit of both, I think. I certainly don't type ('s instead of {'s by mistake, but I do pass the wrong parameters to realloc() (sorry about the language) on occasion. And with practise you do find and fix errors more quickly, for sure.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Confusing ( and { is so hard on a German keyboard that I don't think I've ever done that in my life
    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

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Mmm hmm, it's tough on an ordinary qwerty one too. I don't think I've ever done it. But I've seen some beginning posters do it . . . and even an advanced OpenGL progammer once.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    > Confusing ( and { is so hard on a German keyboard that I don't think I've ever done that in my life
    I have bad memories of trying to write code on a Spanish keyboard (being used to a UK keyboard). Either the braces or parentheses were so far off "normal" to make writing code a really tricky task
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It's the braces, I think. The parentheses are merely one to the side. (If the Spanish follows the general continental European base layout.)
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  2. infinite loop with signal chaining
    By zxcv in forum C Programming
    Replies: 1
    Last Post: 04-18-2008, 11:14 AM
  3. My program's in an infinite loop!
    By Peach in forum C Programming
    Replies: 3
    Last Post: 02-24-2008, 01:58 PM
  4. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  5. infinite loop problem
    By Gil22 in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2003, 03:24 PM

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