Thread: A question about for loops

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question A question about for loops

    I am using a for loop to count down from 10 to 0 it's working to count down from 10 to 1 but when the program cames to the 0 then the program freezes by any reason can anyone help me find out what's wrong.

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int number[2];
        cout << "Enter number: ";
        cin >> number[0];
    if (number[0] == 1)
    {
        for (int i = 10; i < 20; i--) //Print the numbers from 10 down to zero
        {
            if (i > 0) //If i greater than zero then print this
            {
                cout << "This is your " << i <<  " try! " << endl;
                cin >> number[1];
                if (i == 0) //If i is equal to zero then exit
                {
                    cout << "You exited!" << endl;
                }
                if (number[1] == 2)
                {
                    cout << "You Won!" << endl;
                }
            }
        }
    }
        return 0;
    }

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    your program is a bit messed up, but the answer to your question is you have an infinite for loop.

    Try outputting i after the outer if statement closing brace within the loop and see what happens
    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'"

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    What is your program supposed to do exacly?

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    OP's program is supposed to represent a game where there is a 'winning number' and the user has ten attempts to guess it correctly. The output is attempting to count down the number of tries remaining / used after each guess
    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'"

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    but the answer to your question is you have an infinite for loop
    Not quite an infinite loop, due to wrap-around, but a very long loop for sure.

  6. #6
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    @rogster001: Thanks

    @OP:

    1)
    > intnumber[2];
    > cin >> number[0];

    Why would you use an array under such circunstances? Considering the input is supposed to be solely 1 number, you'd be better off doing it this way:
    int number;
    cin>>number;

    2)
    >
    if(number[0] == 1)

    I don't understand this piece of code. The program is supposed to close if the user inserts a number other than 1?

    3)
    Your loop itself is kinda messy. You'd be better off doing something like this:

    Code:
    //why from 0 to 9(Or from 1 to 10)? i equals to the number of tries and should beggin with the 1st try, which is i=1 or i = 0 + 1
    for (int i = 0; i < 10; i++)
        {
            //Logicly the number of attempts goes from the lowest to the highest, ain't that right?
            //eg. 1st attempt, then 2nd attempt, etc etc.
            switch (i)
            {
            case 0:
                std::cout << "This is your 1st attempt\n";
                break;
            case 1:
                std::cout << "This is your 2nd attempt\n";
                break;
            case 2:
                std::cout << "This is your 3rd attempt\n";
                break;
            default:
                std::cout << "This is your " << i + 1 <<"th attempt\n";
                break;
            }
            
            //Here you get to input a number everytime the loop goes
            int number;
            std::cout << "Enter a number: ";
            std::cin >> number;
            std::cin.ignore();
    
    
    
    
            //The following if conditions check whether the 10 attempts have been reached or if the user inserted the correct number (2)
            if (number == 2)
            {
                std::cout<<"You won!";
                break;
            }
            
            if (i == 9)
            {
                std::cout<<"You lose";
                break;
            }
        }


    Last edited by Khabz; 04-16-2013 at 02:40 PM.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    So your loop start at ten, goes down (i.e. 10, 9, 8 ...) and keeps going so long as the number is less than 20.
    Is the problem obvious yet?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    //Logicly the number of attempts goes from the lowest to the highest, ain't that right? //eg. 1st attempt, then 2nd attempt, etc etc.
    Yes thats right, but the OP had the logic / language back to front, in more ways than one. Probably the idea was to count down like having a number of lives remaining
    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'"

  9. #9
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    Hmm I changed my code a bit and this one works to print you exited when the number is 0 but I want the program to stop when number is 0 but instead the program continues to count down -1, -2 I want when the code is 0 you have no more tries and you have lost. The changed code looks like this:

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int number[2];
        cout << "Enter number: ";
        cin >> number[0];
    if (number[0] == 1)
    {
        for (int i = 10; i < 20; i--) //Print the numbers from 10 down to zero
        {
            if (i > 0) //If i greater than zero then print this
            {
                if (number[1] == 2)
                {
                    cout << "You Won!" << endl;
                }
            }
                cout << "This is your " << i <<  " try! " << endl;
                cin >> number[1];
                if (i == 0) //If i is equal to zero then exit
                {
                    cout << "You exited!" << endl;
                }
        }
    }
        return 0;
    }

  10. #10
    spaghetticode
    Guest
    Quote Originally Posted by DecoratorFawn82 View Post
    I want the program to stop when number is 0
    Tell it to do so then. Spell out the head of your loop in plain English. What does your loop say? What do you want it to say?

    "for an integer named 'i' set to 10, as long as ... , execute the loop's body, then ..."

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Read iMalc's reply again, and get rid of the array. Declare new variables as needed close to where you need them.
    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.

  12. #12
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    you still have the logic in your for loop wrong!
    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'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question concerning for loops
    By Programmer_P in forum C++ Programming
    Replies: 36
    Last Post: 05-26-2009, 05:11 PM
  2. Question about loops
    By lroberts1016 in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2008, 07:01 PM
  3. question on GCD and for loops
    By dals2002 in forum C Programming
    Replies: 9
    Last Post: 03-15-2008, 01:59 AM
  4. Question about loops.
    By Hulag in forum C Programming
    Replies: 4
    Last Post: 02-17-2005, 05:37 AM
  5. question about while loops
    By volk in forum C++ Programming
    Replies: 4
    Last Post: 03-22-2003, 09:21 AM