Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem with nested loops

problem with nested loops

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    longbowaj
    wrote on last edited by
    #1

    When i use the following code all works well for a while. If you enter the inner loop once it works fine but will not allow to enter it a second time. can someone explain this to me please or show me how to enter it again. thanks. #include using namespace std; int main () { int number; int testval = 0; while (true) { cout << "Pick a number 4 quits" << endl; cin >> number; if (number == 4) { return 0; } else if (number == 1) { while (testval != 5) { cout << "pick a new number: "; cin >> testval; if (testval == 1) { cout << "inner loop 1" << endl; } else if (testval == 2) { cout << "inner loop 2" << endl; } else { cout << "inner loop 3" << endl; } } } else if (number == 2) { cout << "outer loop 1" << endl; } else { cout << "outer loop 2" << endl; } } }

    M 2 Replies Last reply
    0
    • L longbowaj

      When i use the following code all works well for a while. If you enter the inner loop once it works fine but will not allow to enter it a second time. can someone explain this to me please or show me how to enter it again. thanks. #include using namespace std; int main () { int number; int testval = 0; while (true) { cout << "Pick a number 4 quits" << endl; cin >> number; if (number == 4) { return 0; } else if (number == 1) { while (testval != 5) { cout << "pick a new number: "; cin >> testval; if (testval == 1) { cout << "inner loop 1" << endl; } else if (testval == 2) { cout << "inner loop 2" << endl; } else { cout << "inner loop 3" << endl; } } } else if (number == 2) { cout << "outer loop 1" << endl; } else { cout << "outer loop 2" << endl; } } }

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Can you wrap your code in pre tags instead of code tags? Unindented code is hard to read (for me anyway :)) Mark

      "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

      L 1 Reply Last reply
      0
      • M Mark Salsbery

        Can you wrap your code in pre tags instead of code tags? Unindented code is hard to read (for me anyway :)) Mark

        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

        L Offline
        L Offline
        longbowaj
        wrote on last edited by
        #3

        sure thing

        #include using namespace std;

        int main () {
        int number;
        int testval = 0;

        while (true) {
        	cout << "Pick a number 4 quits" << endl;
        	cin >> number;
        	if (number == 4) {
        		return 0;
        	}
        	else if (number == 1) {
        		while (testval != 5) {
        			cout << "pick a new number: ";
        			cin >> testval;
        
        			if (testval == 1) {
        				cout << "inner loop 1" << endl;
        			}
        			else if (testval == 2) {
        				cout << "inner loop 2" << endl;
        			}
        			else {
        				cout << "inner loop 3" << endl;
        			}
        		}
        	}
        	else if (number == 2) {
        		cout << "outer loop 1" << endl;
        	}
        	else {
        		cout << "outer loop 2" << endl;
        	}
        }
        

        }

        1 Reply Last reply
        0
        • L longbowaj

          When i use the following code all works well for a while. If you enter the inner loop once it works fine but will not allow to enter it a second time. can someone explain this to me please or show me how to enter it again. thanks. #include using namespace std; int main () { int number; int testval = 0; while (true) { cout << "Pick a number 4 quits" << endl; cin >> number; if (number == 4) { return 0; } else if (number == 1) { while (testval != 5) { cout << "pick a new number: "; cin >> testval; if (testval == 1) { cout << "inner loop 1" << endl; } else if (testval == 2) { cout << "inner loop 2" << endl; } else { cout << "inner loop 3" << endl; } } } else if (number == 2) { cout << "outer loop 1" << endl; } else { cout << "outer loop 2" << endl; } } }

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Once testval has been set to five in the inner loop then the inner loop will never be entered again because testval is still 5. Try this:

          int main ()
          {
          int number;
          int testval = 0;

          while (true)
          {
          cout << "Pick a number 4 quits" << endl;
          cin >> number;
          if (number == 4)
          {
          return 0;
          }
          else if (number == 1)
          {
          while (testval != 5)
          {
          cout << "pick a new number: ";
          cin >> testval;

                  if (testval == 1) 
                  {
                     cout << "inner loop 1" << endl;
                  }
                  else if (testval == 2) 
                  {
                     cout << "inner loop 2" << endl;
                  }
                  else 
                  {
                     cout << "inner loop 3" << endl;
                  }
               }
               `**testval = 0;**`
            }
            else if (number == 2) 
            {
               cout << "outer loop 1" << endl;
            }
            else 
            {
               cout << "outer loop 2" << endl;
            }
          

          }
          }

          Mark

          "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

          L R 2 Replies Last reply
          0
          • M Mark Salsbery

            Once testval has been set to five in the inner loop then the inner loop will never be entered again because testval is still 5. Try this:

            int main ()
            {
            int number;
            int testval = 0;

            while (true)
            {
            cout << "Pick a number 4 quits" << endl;
            cin >> number;
            if (number == 4)
            {
            return 0;
            }
            else if (number == 1)
            {
            while (testval != 5)
            {
            cout << "pick a new number: ";
            cin >> testval;

                    if (testval == 1) 
                    {
                       cout << "inner loop 1" << endl;
                    }
                    else if (testval == 2) 
                    {
                       cout << "inner loop 2" << endl;
                    }
                    else 
                    {
                       cout << "inner loop 3" << endl;
                    }
                 }
                 `**testval = 0;**`
              }
              else if (number == 2) 
              {
                 cout << "outer loop 1" << endl;
              }
              else 
              {
                 cout << "outer loop 2" << endl;
              }
            

            }
            }

            Mark

            "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

            L Offline
            L Offline
            longbowaj
            wrote on last edited by
            #5

            ahhh i'm silly sometimes. thank you much for that i was going nuts truing to figure it out.

            1 Reply Last reply
            0
            • M Mark Salsbery

              Once testval has been set to five in the inner loop then the inner loop will never be entered again because testval is still 5. Try this:

              int main ()
              {
              int number;
              int testval = 0;

              while (true)
              {
              cout << "Pick a number 4 quits" << endl;
              cin >> number;
              if (number == 4)
              {
              return 0;
              }
              else if (number == 1)
              {
              while (testval != 5)
              {
              cout << "pick a new number: ";
              cin >> testval;

                      if (testval == 1) 
                      {
                         cout << "inner loop 1" << endl;
                      }
                      else if (testval == 2) 
                      {
                         cout << "inner loop 2" << endl;
                      }
                      else 
                      {
                         cout << "inner loop 3" << endl;
                      }
                   }
                   `**testval = 0;**`
                }
                else if (number == 2) 
                {
                   cout << "outer loop 1" << endl;
                }
                else 
                {
                   cout << "outer loop 2" << endl;
                }
              

              }
              }

              Mark

              "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

              R Offline
              R Offline
              realJSOP
              wrote on last edited by
              #6

              You just completed a homework assignment for him...

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              M 1 Reply Last reply
              0
              • R realJSOP

                You just completed a homework assignment for him...

                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                :doh:

                "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups