Search code examples
c++nested-loops

loops missing the nested if


My loop is skipping the nested 3rd else if. What should I do so it does not skip it?

#include <iostream>

int main()
{
    int loop, i;
    loop = 0;    

    char choice, selection;

    std::cout << "Welcome" << std::endl;
    while (loop == 0)
    {
        std::cout << "Please a choice" << std::endl;
        std::cout << "a. Run" << std::endl;
        std::cin >> selection;
        if (selection == 'a')
        {
            for (i = 1; i < 5; i++)
            {
                std::cout << "run" << std::endl;

                std::cout << "do you want to continue running? (y/n)";
                std::cin >> choice;
                if (i < 5 && choice == 'y')
                {
                    continue;
                }
                else if (i < 5 && choice == 'n')
                {
                    break;
                }
                else if ( i > 5 && choice == 'y')
                {
                    std::cout << "Limit Reached"; 
                }
            }
        }                

I want it to print "limit reached" when i > 5, but somehow the loop skips it. What am I doing wrong?

Previous StackOverflow questions did not help much. I tried their solutions, but they did not solve my issue.

Edit:

For example: Nesting if statements inside a While loop? I have looked if I have initialized any of my int which might be messing my loop.

Looks like my else if didn't work because the condition I gave it would never be true. I feel stupid. But thanks everyone for your time. I really appreciate it.


Solution

  • Your loop runs only while i is in the range 1..4 inclusive, so i < 5 will always be true and i > 5 will never be true.

    You should check the value of i after the loop exits. You should also check the user's input to make sure it matches your expectations.

    Try something more like this:

    #include <iostream>
    #include <cctype>
    
    bool shouldContinue(const char *prompt)
    {
        char choice;
    
        do
        {
            std::cout << prompt << " (y/n)";
            if (!(std::cin >> choice)) throw ...;
            if (choice == 'y' || choice == 'Y' || choice == 'n' || choice == 'N') break;
            std::cout << "Invalid choice" << endl;
        }
        while (true);
    
        return (choice == 'y' || choice == 'Y');
    }
    
    int main()
    {
        bool loop = true;
        int i;
        char selection;
    
        std::cout << "Welcome" << std::endl;
    
        while (loop)
        {
            std::cout << "Please a choice" << std::endl;
            std::cout << "a. Run" << std::endl;
            std::cin >> selection;
    
            if (selection == 'a')
            {
                for (i = 1; i < 5; ++i)
                {
                    std::cout << "run" << std::endl;
    
                    if (!shouldContinue("do you want to continue running?"))
                        break;
                }
    
                if (i == 5)
                    std::cout << "Limit Reached"; 
            }       
    
            ...
        }
    
        return 0;
    }