can't get only one statement to appear

I can't seem to make it so only one of the two statement appear.

In this program, when a user enters two numbers, if the first number entered is smaller, I want the first statement to appear. If the first number is larger I want the second statement to appear.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>

int main()
{
    std::cout << "Enter two numbers: ";
    std::cout << std::endl;
    int v1 = 0, v2 = 0, sum = 0, val;
    std::cin >> v1 >> v2;
    val = v1;
    while (val <= v2) {
        sum += val;
        ++val;
    }
    std::cout << "The sum of " << v1 << " to " << v2 << " inclusive is: " << sum;
    std::cout << std::endl;
    std::cout << "The sum of " << v2 << " to " << v1 << " inclusive is: " << sum;
    std::cout << std::endl;
    return 0;
}
Last edited on
closed account (48T7M4Gy)
You will need to have a decision loop, i.e. an if statement to make the decision which line to display.

if ( code here )
displace line 1
otherwise
display line 2
I did this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>

int main()
{
    std::cout << "Enter two numbers: ";
    std::cout << std::endl;
    int v1 = 0, v2 = 0, sum = 0, val;
    std::cin >> v1 >> v2;
    val = v1;
    while (val <= v2) {
        sum += val;
        ++val;
    }
    if ( v1 > v2) {
        std::cout << "The sum of " << v2 << " to " << v1 << " inclusive is: " << sum;
        std::cout << std::endl;
    }
    else {
        std::cout << "The sum of " << v1 << " to " << v2 << " inclusive is: " << sum;
        std::cout << std::endl;
    }

    return 0;
}


But I can't figure what to put in "if(code)." I feel like the "v1 >v2" is wrong.

When I run the program, the right statement shows up, but it[program] won't do the calculation.
Last edited on
closed account (48T7M4Gy)
Your if code is ok. The problem is in calculating the sum. So, pick two numbers say 3 and 4 and write down the response as each line is processed in the while loop, and you'll see that the sum is 0 for one of the cases where x<y or x>y.

Line 10 means the sum is always zero for val ( i.e v1 ) >v2

So if you want both answers to be the same then you'll have to swap v1 and v2 around.
Thanks, I finally got the program to do what I wanted!

Question, can this code be any more efficient than it is now?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>

int main()
{
    std::cout << "Enter two numbers: ";
    std::cout << std::endl;
    int v1 = 0, v2 = 0, sum = 0, val;
    std::cin >> v1 >> v2;
    val = v2;
    while (val <= v1) {
        sum += val;
        ++val;
    }
    if (v1 > v2) {
        std::cout << "The sum of " << v2 << " to " << v1 << " inclusive is: " << sum;
        std::cout << std::endl;
    }
    else if (v2 > v1) {
        val = v1;
        while (val <= v2) {
        sum += val;
        ++val;}
        std::cout << "The sum of " << v1 << " to " << v2 << " inclusive is: " << sum;
        std::cout << std::endl;
    }

    return 0;
}
closed account (48T7M4Gy)
Great stuff.

Yeah you could do the test first, swap the numbers around, calculate the sum and display the answer That way you only have one set of while/cout statements not two.
Here's what I wanted the program to do:

1. User inputs two integars
2. Program forces smaller integar to go first in calculation.
3. The program will calculate smaller to bigger integar to find result even if user inputs bigger integar first.

ex.

If I input 20 then 10, the program will say:

"10 to 20 is...."

If I input 10 then 20, the program will say:

"10 to 20 is..."


The last code I wrote does that(what I wanted). But can I make that more efficient? Or is that pretty solid code?

Last edited on
closed account (48T7M4Gy)
That's the idea :-) Try it out. The only thing to watch is to avoid duplicating code when it isn't necessary. This applies to the message display. You're on the right track.
Topic archived. No new replies allowed.