problem with nested loops
-
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; } } }
-
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; } } }
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
-
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
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; } }
}
-
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; } } }
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
-
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
-
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
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 -
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:doh:
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder