Home > Software design >  v2 output to stop before my first variable
v2 output to stop before my first variable

Time:01-08

I'm trying a very basic C exercise:

Write a program that prompts the user for two integers. Print each number in the range specified by those two integers.

This is my program:

#include <iostream>

int main()
{
    std::cout << "Write two numbers: " << std::endl;
    int v1 = 0, v2 = 0;
    std::cin >> v1 >> v2;
    std::cout << "The numbers between " << v1 << " and " << v2 << " are: " << std::endl;

    while (v1 > v2)
    {
        std::cout <<   v2 << std::endl;
    }
    
}

My output shows like this:

Write two numbers: 
10
5
The numbers between 10 and 5 are: 
6
7
8
9
10

And my questions is, how do I output just from 6 to 9, without the 10?

I'm with the basics, so I don't need for or loops or other things more advanced, just a while and v2.

CodePudding user response:

while(v1 != v2 - 1)
{
    std::cout <<   v1 << std::endl;
}

CodePudding user response:

I finally added this code in order to work and to read both cases as a comment suggested before:

#include <iostream>

int main()
{
    std::cout << "Write two numbers: " << std::endl;
    int v1 = 0, v2 = 0;
    std::cin >> v1 >> v2;
    std::cout << "The numbers between " << v1 << " and " << v2 << " are: " << std::endl;

    while (v2 < v1 &&   v2 < v1)
    {
        std::cout << v2 << std::endl;
    }

    while (v1 < v2 &&   v1 < v2)
    {
        std::cout << v1 << std::endl;
    }

}

If it is too newbie, let me know, maybe there is another way to make it look simpler (just with while).

  •  Tags:  
  • Related