I have been writing a program which asks you to input two integers, which then lists all integers from the smaller of the two integers entered to the larger of the two integers entered (inclusive). I want the program to put a period after the last integer in the output, and I have found ways to do this without using a for loop, but I want to understand why this code doesn't work (it just outputs the larger integer with a period after it).
#include <iostream>
int main()
{
std::cout << "Enter two integers, pressing <ENTER> after each integer." << std::endl;
int num1, num2, lower, upper;
std::cin >> num1 >> num2;
if (num1 > num2)
{
upper = num1;
lower = num2;
}
else
if (num1 < num2)
{
upper = num2;
lower = num1;
}
else
if (num1 = num2)
{
upper = num1;
lower = num1;
}
std::cout << "All integers between " << lower << " and " << upper << " are:" << std::endl;
for (int val = lower; val <= upper; ++val)
{
if (val = upper)
{
std::cout << val << "." << std::endl;
++val;
}
else
{
std::cout << val << std::endl;
++val;
}
}
return 0;
}
If the two integers entered are 1 and 5, why does this output
5. instead of 1 2 3 4 5.?