2

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.?

1
  • The first line defense against using assignment instead of comparison by mistake is to always always use const for any value that does not change. In this case num1 and num2 should be put into const int& right after streaming from std::cin. If an assignment occurs, the compiler will warn you. The upper/lower should also be const and can be initialized with a max and min (standard library functions). E.g.: const upper = max( num1, num2 ); Commented Jan 14, 2012 at 15:52

8 Answers 8

3

First, you are using the assignment operator = where you should use the comparison operator ==. Second, the for loop already increments val due to the third statement of the for loop (for (...; ...; ++val)). As such, there is no need to increment val from within the body of the loop.

Also, considering that you want to print all results in a single line, you should output a space after each iteration rather than std::endl. Note that the last iteration is an exception as you will want to output a period rather than a space. In the fixed version below, I have used the ternary operator in the loop body to accomplish this.

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;
}

std::cout << "All integers between " << lower << " and " << upper << " are:" << std::endl;
for (int val = lower; val <= upper; ++val)
{
    std::cout << val << ((val == upper) ? "." : " ");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I hadn't studied or otherwise known about the ternary operator or the difference between = and ==... this was very helpful.
2

The problem is

if (val = upper)
{
    std::cout << val << "." << std::endl;
    ++val;
}

val = upper means you affect to val the upper value. Put 2 '=' so it will compare instead of affect. Like this

if (val == upper)
{
    std::cout << val << "." << std::endl;
    ++val;
}

Comments

1

Because

if(Val = upper)

Reassigns the value of the Val to be upper. What you meant was

if(Val == upper)

That could be it, anyway.

Comments

0

++val must be present only at for loop declaration level not inside if and else

for (int val = lower; val <= upper; ++val)
   std::cout << val << (val==upper ?  "." : " ") << std::endl; 

Comments

0

There are two errors in your program as mentioned by both delannoyk and Gillaume07. = is the assignment operator in c++ while == is the comparison operator. So almost always if statements are going to have comparison operator. Secondly have increment operation in both for loop statement and the if / else will result in addition of 2 to the counter variable after every iteration.

Comments

0

Easier way to do what you did there is to replace this:

for (int val = lower; val <= upper; ++val)
{
    if (val = upper)
    {
        std::cout << val << "." << std::endl;
        ++val;
    }
    else
    {
        std::cout << val << std::endl;
        ++val;
    }
}

with this:

for (int val = lower; val <= upper; ++val)
{
     std::cout << val;

}
cout<<"."<<std::endl;

Comments

0

Others already commented on a number of issues with this code. Here is my pet peeve: You need to check that the input actually was correct! That is, after reading from std::cin you need to check that this was successful. Entering a non-space and non-numeric character would otherwise leave the random garbage which is happens to be stored in your variables as is. The canonical way to read from from an std::istream looks like this:

if (std::cin >> num1 >> num2) {
    ...
}
else {
    possibly an report error here
}

Comments

0

"=" is used to assign the value of "upper" to "val". when comparing the values of "val" and "upper", the operator to be used should be "==" instead of "=". Therefore, the following code should be:

if (val == upper)
{
    std::cout << val << "." << std::endl;
    ++val;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.