Exercise 1.11

+1 Orvin Welchez · August 13, 2014
I'm trying to work an exercise from the book "Primer 5th edition"
(Write a program that prompts the user for two integers. Print each number in the range specified by those two integers).


When I type the 1 and 10 for example, the program only print up to number 9 and the same happens when I do 10 to 1, it only print up to number two always missing the last number. Any help will be greatly appreciated, Thank you!

Here is the code:


#include <iostream>

int main ()
    {
        int v1 = 0, v2 = 0;

    std::cin >> v1 >> v2;

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

        }

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

Post a Reply

Replies

Oldest  Newest  Rating
0 Orvin Welchez · August 14, 2014
I found the following code super easy to understand and you provided me with a nice trick with swapping the numbers with "if" and "else" statements, I truly appreciate your help Brandon, Thank you!


#include <iostream>
int main ()
{
int number1 = 0, number2 = 0;
int lowNumber = 0, highNumber = 0;

std::cin >> number1 >> number2;

if(number1 > number2){
lowNumber = number2;
highNumber = number1;
}else{
lowNumber = number1;
highNumber = number2;
}

while(highNumber >= lowNumber)
{
std::cout << lowNumber << std::endl;
lowNumber++;
}
return 0;
}
+1 Brandon Elliott · August 13, 2014
I see what you're talking about now with the code blocks causing some issues. Strangely enough, it doesn't do that in internet explorer (I posted those replies while I was at work, and they enforce a strict browser policy)

Now that I'm on google chrome at home, I see what you're talking about
0 Orvin Welchez · August 13, 2014
Wow how dummy I am lol, I'm sorry I got all confused, so &lt and &gt get replace by the "less than" and "greater than" signs.
I will try to name variables better for future reference and thank you for the advice, I'll try to stick to it.

I have done Bucky's C++ videos up to number 41 but then I saw someone suggesting this "C++ Primer 5th Edition Book" and I'm starting over to make sure that I haven't missed anything important and I kinda like this book so far.
+1 Brandon Elliott · August 13, 2014
i don't see the characters you're talking about but i'm assuming they are the greater than and less than signs.

#include <iostream>
int main ()
{
int number1 = 0, number2 = 0;
int lowNumber = 0, highNumber = 0;

std::cin >> number1 >> number2;

if(number1 > number2){
lowNumber = number2;
highNumber = number1;
}else{
lowNumber = number1;
highNumber = number2;
}
while(highNumber >= lowNumber)
{
std::cout << lowNumber << std::endl;
lowNumber++;
}
return 0;
}


you can see in the code the 2 inputted numbers are saved to variables lowNumber and highNumber according to which one is higher or lower than the other. this makes it so the user can input for example 0 and 10 or input 10 and 0 and it will still print out 0-10 either way.

you could also make it so that if you enter 0 and 10 it will print out 0-10 and if you enter 10 and 0 it will print out 10-0.

like this:

#include <iostream>
int main ()
{
int number1 = 0, number2 = 0;

std::cin >> number1 >> number2;

if(number1 > number2){
while(number1 >= number2)
{
std::cout << number1 << std::endl;
number1++;
}
}else{
while(number1 <= number2)
{
std::cout << number2 << std::endl;
number2--;
}
}

return 0;
}
0 Orvin Welchez · August 13, 2014
For loops have not been introduced before that exercise and even though I have some experience working with For loops, I'm trying to go by the rules and do it with only the requested knowledge.

I'm trying to understand this &gt and &lt but I have no clue what it is: 


std::cin &gt;&gt; lowNumber &gt;&gt; highNumber;

std::cout &lt;&lt; i &lt;&lt; std::endl;

std::cout &lt;&lt; i &lt;&lt; std::endl;
+1 Brandon Elliott · August 13, 2014
This is how I would do it with for loops.

#include <iostream>

int main ()
{
int number1 = 0, number2 = 0;
int lowNumber = 0, highNumber = 0;

std::cin >> number1 >> number2;

if(number1 > number2){
lowNumber = number2;
highNumber = number1;
}else{
lowNumber = number1;
highNumber = number2;
}

for(int i = lowNumber; i < (highNumber + 1); i++)
{
std::cout << i << std::endl;
}
return 0;
}


sorry i originally got confused about what you were trying to do in your code with the 2  different loops
you always want to practice good variable naming. it makes your code more readable
0 Orvin Welchez · August 13, 2014
I tried adding the = sign but when I run the program it also prints 11 then it goes back to 10 and returns 0.
0 Brandon Elliott · August 13, 2014
it's because 10 is not < 10 and 1 is not > 1

you'd want to change your while loops to be

while (v1<=v2)

and

while (v1>=v2)
  • 1

C++

107,260 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator