I'm trying to do exercise 1.11 on the c++ primer which is
Write a program that prompts the user for two integers. Print each number in the range specified by those two integers.
I have tried to write the program and have gotten it to run, but not the way I want it to. Sometimes, it just spammed the lowest number nonstop. Sometimes it kept increasing or decreasing infinitely. Another one would be if I enter 2 and 6 for the numbers, it would should 2 6 2. There's also another one where it just spams some big number where I have no idea is coming from. Sometimes, when I put my two numbers in, it does nothing and I have to press another key to close it.
I have tried to look for the solution online,and I have found one, but it had weird statements in it. I have barely learned the "while" loop in c++ primer so no "for", "if" loops.
Here is the program where it asks me to put the two numbers it and it just ends there.
// Exercise 1_11.cpp : main project file.
#include <iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << "v1 = " << v1 << " and v2 = " << v2 << std::endl;
if(v1>v2) // Switch the numbers around if second number is smaller than first
{
int x=v2;
v2=v1;
v1=x;
std::cout << "Now v1 = " << v1 << " and v2 = " << v2 << std::endl;
// Just to show the numbers were reversed if first number higher than second
}
while(v1 <= v2)
{
std::cout << v1 << std::endl;
v1++; // Increase v1 until it exceeds v2 value
}
return 0;
}
// Exercise 1_11.cpp : main project file.
#include <iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
while(v1 < v2)// This section used only when v2 is larger than v1
{
std::cout << v1 << std::endl;
v1++; // Increase v1 until it exceeds v2 value
}
while(v2 <= v1)// This section used only when v1 is larger than v2
{
std::cout << v2 << std::endl;
v2++; // Increase v2 until it exceeds v1 value
}
return 0;
}