How many loops?

This is a discussion on How many loops? within the C++ Programming forums, part of the General Programming Boards category; This isn't homework, I'm working through the Malik book on my own. Am I supposed to use one big loop ...

1. How many loops?

This isn't homework, I'm working through the Malik book on my own.

Am I supposed to use one big loop for this whole task, or a seperate loop for each sub-task?

Code:
```//******************************************************************************
// Malik Chapter 5 programming exercise 8
// Pretty stinkin' involved!
// 'Write a program that uses a while loop to perform the following steps:
//******************************************************************************

#include<iostrea>
using namespace std;

int main()
{
//a. Prompt the user to input two integers (smaller one first)
int firstNum, secondNum;

//b. Output all odd numbers between firstNum and secondNum.

//c. Output the sum of all even numbers between firstNum and secondNum.

//d. Output the number and their square between 1 and 10.

//e. Output the sum of the square of odd numbers between firstNum and secondNum.

//f. Output all uppercase letters.```

2. a probably needs a separate loop, so does f because it doesn't rely on firstNum and secondNum. b through e can be in the same loop, but it might be simpler to split them up into four loops. Try both and see.

3. Thanks Noir

I couldn't figure out how to use a loop for step a but the rest was easy enough.
This would make more sense using functions but that's the next chapter.
Code:
```#include<iostream>
using namespace std;

int main()
{

//a. Prompt the user to input two integers (smaller one first)
int firstNum, secondNum;
cout<<"Please enter two integers, smaller one first: ";
cin>>firstNum>>secondNum;
cout<<endl;

//b. Output all odd numbers between firstNum and secondNum.
int x = firstNum;
while(x <= secondNum)
{if(x%2)
cout<<x<<" ";
x++;}
cout<<endl<<endl;

//c. Output the sum of all even numbers between firstNum and secondNum.
int y = firstNum;
int sum = 0;
while(y <= secondNum)
{ if(!(y%2))
sum = sum + y;
y++;}
cout<<"Sum of even numbers between "<<firstNum<<" and "<<secondNum
<<" is "<<sum<<"."<<endl<<endl;

//d. Output the numbers and their square between 1 and 10.
int z = 1;
while(z <= 10)
{cout<<z<<" squared = "<<z*z<<endl<<endl;
z++;}

//e. Output the sum of the square of odd numbers between firstNum and secondNum.
int a = firstNum;
int sqr = 0;
sum = 0;
while(a <= secondNum)
{if(a%2)
{sqr = a * a;
sum = sum + sqr;}
a++;}
cout<<"The sum of the square of odd numbers between your numbers is "
<<sum<<"."<<endl<<endl;

//f. Output all uppercase letters.
char letter = 'A';
while(letter <= 'Z')
{cout<<letter; letter++;}

cin.ignore();
cin.get();
return 0;
}```

4. For a I was thinking of an error handling loop:
Code:
```while ( true ) {
cout << "Please enter two integers, smaller one first: ";

bool rc = cin >> firstNum >> secondNum;

if ( !rc ) {
cin.clear();
cin.ignore( 256, '\n' );
cout << "error, try again\n";
continue;
} else if ( firstNum > secondNum ) {
swap( firstNum, secondNum );
}
}```

5. I agree, error handling of input is important but I wasn't sure how to do it.

The loop you suggest keeps displaying

Please enter two integers, smaller one first:
no matter what the input is.

I don't understand some of your code so I don't know why it doesn't terminate.

6. First, there's no output expression, so even if the swap occurs, you're not going to see it on screen.

Second, "while(true)" is supposed to be infinite because the condition is hard coded. You'll either have to make it a conditional dependent on some variable or you'll have to embed a "break" statement somewhere inside the loop pending some fulfilled condition.