Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9065017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:23:59+00:00 2026-06-16T16:23:59+00:00

I feel like I might be missing something here, but something is telling me

  • 0

I feel like I might be missing something here, but something is telling me that I might just be making this more difficult that it has to be, but from the book, “C++ Primer, 5th ed.,” I’m stuck on this problem:

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

Up until this time in the book, the only loop being used is the while loop, and no conditional expressions, like if, have been introduced. The problem would be simple if it were not for the fact that a user could put the values into the integers asked for in ascending or descending order, so simple subtraction WOULD find the difference, but without testing which way to increment.

How could I absolutely print the range between the numbers, guaranteeing not to just increment towards infinity without testing the outcome of such math and/or without comparing the two numbers? This is what I have; it works when the first value: v1 is less than or equal to the second: v2, but not otherwise:

#include <iostream>

int main()  
{  
    int v1 = 0, v2 = 0;  
    std::cout << "Enter two integers to find numbers in their range (inclusive): "  
              << endl;  
    std::cin >> v1 >> v2;  
    while (v1 <= v2)  
    {  
        std::cout << v1;  
        ++ v1;  
    }  
    return 0;  
}  

Any help would be greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:24:00+00:00Added an answer on June 16, 2026 at 4:24 pm

    Here is a simple rewrite where you use min and max to determine the range you want to iterate on:

    #include <iostream>
    
    int main()  
    {  
        int v1 = 0, v2 = 0;  
        std::cout << "Enter two integers to find numbers in their range (inclusive): " << std::endl;  
        std::cin >> v1 >> v2;  
        int current =  std::min(v1, v2);
        int max = std::max(v1, v2);
        while (current <= max)  
        {  
            std::cout << current << std::endl;  
            ++ current;  
        }  
        return 0;  
    }
    

    This also allows you to keep the two inputs “intact”, because you’re using another variable to iterate on the values.

    Also note that the ++current could be done on the exact same line it is printed if it was replaced by current++. The later would return the current value of current and THEN increment it.

    Edit

    Here’s what Michael suggested I believe, in working code:

    #include <iostream>
    
    int main()  
    {  
        int v1 = 0, v2 = 0;  
        std::cout << "Enter two integers to find numbers in their range (inclusive): " << std::endl;  
        std::cin >> v1 >> v2;  
        int increment = (v2 - v1) / std::abs(v2 - v1);
        int current = v1;
        int max = v2 + increment;
        while (current != max)  
        {  
            std::cout << current << std::endl;  
            current += increment;  
        }  
        return 0;  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like this might be really simple but I'm just not getting it
I might be missing something basic here, but I'm stumped on this error: model
Feel like this should be something easy that I'm missing. I have a table
I feel like I've only ever seen this here on SO, but I can't
I feel like I'm missing something obvious, but I can't work out why my
When it comes to indexing in XPath, I feel like I'm missing something here.
I'm just starting to use Eclipse and I feel like I'm missing something in
I feel like i'm going round in circles here and missing something really daft...
I feel like such a noob asking this but, for some reason a horizontal
I feel like there's a pretty simple way to do this, but I'm not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.