Hi there!
I am just starting out in c++ and so far, its going ok. I have a small problem in a simple program that is frustrating me, so I am hoping someone will point me in the right direction of where I've gone wrong.
The program basically prompts the user to input two integer values, then outputs all the odd numbers between these two integer values (and their squares), and also outputs all the even numbers between the two inputted values, along with the sum of these even numbers.
I have the program working sufficiently, so long as the first value entered by the user is smaller than the second value entered. Thats the small problem I've encountered; it should not make a difference between the size of the values entered. I think i need to change the condition in the for loop, but I've tried various conditions without success
Here is the program:

#include <iostream>
#include <conio.h>
#include <iomanip>

using namespace std;

int main()
{
    int a,b,a1,b2,square; //declaring variables
    int sum = 0;
    cout << "Enter two integers: " << endl;  //user inputs two integers
    cin >> a >> b;
    cout << "The numbers entered are: " << endl;  //displays the integers listed
    cout << "1. " << a << endl;
    cout << "2. " << b << endl;        
    a1 = a;
    b2 = b;
    //for odd numbers
    cout << "The odd numbers between " << a << " and " << b << 
           ", along with their squares are:" << endl;
    for (a++;a < b;a++)
    
    {
          if ((a%2) !=0)
          {
               cout << setw(4) << a;
               square = a * a;  //formula for finding the square of an integer
               cout << " = " << square;
          } //end if
    } //end for
    cout << endl << endl;
    
    //for even numbers
    cout << "The even numbers between " << a1 << " and " << b2 << 
          " are:" << endl;
    for (a1++;a1 < b2;a1++)
    {
       if((a1%2) == 0) 
       { 
           cout << setw(4) << a1; 
           sum += a1;  //formula for finding the sum of the even integers
       } //end if    
    } //end for
    cout << endl; 
    cout << "The total sum of all the even numbers is: " << sum;
    cout << endl << endl;
    getch();
    return 0;
}//end main
Salem commented: Congratulations on being able to use code tags on your first post. Keep up the good work. +20

Dani AI

Generated

The core issue is that the loop headers depend on and mutate the original input variables, so the loops only work when the first input is smaller than the second. and were right to suggest swapping; a clearer pattern is to compute a stable start/end (min/max) and iterate with a separate loop counter. That preserves the original inputs for display and makes the logic robust regardless of input order.

#include <iostream>
#include <algorithm>

using namespace std;

int a, b;
cin >> a >> b;

int start = min(a, b);
int end   = max(a, b);
long long sum = 0;

for (int i = start + 1; i < end; ++i) {
    if (i % 2 != 0)
        cout << i << " = " << static_cast<long long>(i) * i << '\n';
    else {
        cout << i << ' ';
        sum += i;
    }
}
cout << "\nTotal sum of evens: " << sum << '\n';

Notes and troubleshooting tips: choose exclusive vs inclusive boundaries deliberately (use start+1 / < end for "between", or start / <= end to include endpoints). Avoid using the input variables themselves as loop counters (the a++ in the original for header is fragile). Use long long (or int64_t) for squares if inputs may be large to prevent overflow. Prefer standard helpers (std::min/std::max or std::swap) over nonportable headers like <conio.h>/getch(); for console pause, std::cin.get() or running from a terminal is portable. Formatting can remain with setw if alignment is desired.

and pointed at the right fixes; ’s post appears accidental. Given ’s follow-up that the program now works, the patterns above will keep it correct and easier to maintain.

Recommended Answers

All 5 Replies

After the cin statement if a < b then just swap them so that you don't have to code for that condition.

//--

cikara21: Huh? Is that supposed to mean something?

In both your 'for' loops your condition needs 'a' to be smaller than 'b', so before your 'for' loop but after your call for user input 'cin', you need a condition which checks the values entered, and then ensures that a is smaller than b

for example

//check to ensure that a is smaller than b
if(a > b)
{
	int temp = a;
	a = b;
	b = temp;
}

your code should now work fine even if a is greater than b

Hi all,

Just want to say thanks for your help, sorry it took so long to get back. I got the program working just fine.

Thanks again
n.m

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.