Thread: function loop help

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    5

    function loop help

    I'm writing a calendar program and I need help with a function called getMonth().

    It's supposed to return a single value as the output: the month number that the user entered. It also ensures that the month is in the acceptable range (1 ≤ month ≤ 12) . If the user enters a month outside that range, then getMonth() keeps prompting him or her until an acceptable value is found.

    Here's what I came up with:

    Code:
    int getMonth()
    {
       int month;
       cout << "Enter a month number: ";
       cin >> month;
       for (month = 1; month = 12; month++)
       {
          int month;
          if (month > 12)
             cout << "Month must be between 1 and 12";
       }
       return month;
    }
    the problem is that whenever I enter in any number, regardless of whether it fits the parameters of 1 and 12, it goes through an infinite cycle.

    I DON'T KNOW WHAT TO DO!!!!
    please help

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    That should be "month == 12" (note the double equal signs).

    Also, declaring another "month" inside the for loop is going to mess up your comparison. Don't you want to compare 12 against the "month" you created at the beginning of the main function instead?
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    ok, so I revised it some, and used a while statement instead of a for.

    Code:
    int getMonth()
    {
       int month;
       cout << "Enter a month number: ";
       cin >> month;
       while ( month < 1 || month > 12)
       {
          cout << "\nMonth must be between 1 and 12";
       }
       return 0;
    }
    but this still runs through the string forever. What am I doing wrong?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You are not modifying "month" inside the while loop. Therefore once you go into the loop, there is no way to exit. You might want to read about a do/while loop for this sort of problem since it's better suited.

    Code:
    #include <iostream>
    
    int main(void)
    {
        int month;
        do  
        {   
            std::cout << "Enter a month number: ";
            std::cin >> month;
        } while(month < 1 || month > 12);
        std::cout << "You entered: " << month << std::endl;    
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    I really do not know how to modify month. I know that this is extremely basic, but im extremely new to programming. how do I modify the variable so that the loop can exit?

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    I really do not know how to modify month. I know that this is extremely basic, but im extremely new to programming. how do I modify the variable so that the loop can exit?
    Did you actually read the code example that was posted for you? You could even compile and run it, it will all become clear!
    But in any case just think about it properly, in your original code you are saying literally that "WHILST the month number fits this condition keep running" And if you look, Nothing in your code does anything to change 'month'.
    Once the loop has started the program cannot go anywhere except keep executing the loop until something happens to stop it E.G. fail the condition in the While statement.
    In the example that was posted, the user is asked to input a value for month, everything will now work as the condition that is keeping the loop running has a chance to be changed and thus exit loop.
    Also make sure you initialise (ie set a value of 0 or whatever) your variable before using it to control a while loop, or any other loop.
    In general always initialise variables, unless you like unneccesary debugging!
    Last edited by rogster001; 02-25-2011 at 02:59 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by akahizzle View Post
    I really do not know how to modify month. I know that this is extremely basic, but im extremely new to programming. how do I modify the variable so that the loop can exit?
    Current loop logic:

    WHILE month < 1 || month > 12
    PRINT "Month must be between 1 and 12"
    REPEAT

    Now modify that logic so it's right.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function (modules) and loop help.
    By GCNDoug in forum C Programming
    Replies: 5
    Last Post: 04-07-2007, 12:43 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM