### help with the exercise

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.

 ``123456789101112131415`` ``````#include int main() { std::cout << "Enter two numbers:" << std::endl; int v1 = 0, v2 = 0; std::cin >> v1 >> v2; while(v1>v2){ std::cout << v2 << std::endl; --v1; } return 0; } ``````
Last edited on
I think you meant to say std::cout << v1 << std::endl; Since that's the number you're changing, otherwise you're repeating the same number
I replaced the v2 with a v1 and it still does the same thing. It asks me to enter two numbers and does nothing after that.
@lurksalot

Try this way.

 ``1234567891011121314151617181920212223242526`` ``````// Exercise 1_11.cpp : main project file. #include 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; }``````
The book hasn't talked about the if loops yet though.
if is a statement not a loop. It's pretty much says what it does.
If it is something...then do something
 ``1234`` ``````if( some_is_true ) { do_something; }``````
Oh I meant, we haven't learned anything about "if" and "for" statements.
@Lurksalot

Here's the same program, but without using the if statement.

 ``1234567891011121314151617181920212223`` ``````// Exercise 1_11.cpp : main project file. #include 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; }``````
Oh okay, thanks a lot!
what I would do is something like this
 ``1234567891011121314151617181920212223242526272829`` ``````#include int main() { std::cout << "Enter two numbers: " << std::endl; int v1 = 0 , v2 = 0; std::cin >> v1 >> v2; while( v1 != v2 ) { v1 > v2 ? ++v2 : ++v1; //if v1 > v2 add to v2 otherwise add to v1. } return( 0 ); } /**or**/ void add_to_smaller( int &v1 , int &v2 ) { if( v1 < v2 ) ++v1; else ++v2; } int main() { std::cout << "Enter two numbers: " << std::endl; int v1 = 0 , v2 = 0; std::cin >> v1 >> v2; while( v1 != v2 ) add_to_smaller( v1 , v2 ); }``````

Topic archived. No new replies allowed.