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

  • SEARCH
  • Home
  • 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 7069723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:28:49+00:00 2026-05-28T05:28:49+00:00

I have been writing a program which asks you to input two integers, which

  • 0

I have been writing a program which asks you to input two integers, which then lists all integers from the smaller of the two integers entered to the larger of the two integers entered (inclusive). I want the program to put a period after the last integer in the output, and I have found ways to do this without using a for loop, but I want to understand why this code doesn’t work (it just outputs the larger integer with a period after it).

#include <iostream>

int main()
{
std::cout << "Enter two integers, pressing <ENTER> after each integer." << std::endl;
int num1, num2, lower, upper;
std::cin >> num1 >> num2;
if (num1 > num2)
{
    upper = num1;
    lower = num2;
}
else
    if (num1 < num2)
    {
    upper = num2;
    lower = num1;
    }
    else
        if (num1 = num2)
        {
            upper = num1;
            lower = num1;
        }
std::cout << "All integers between " << lower << " and " << upper << " are:" << std::endl;
for (int val = lower; val <= upper; ++val)
{
    if (val = upper)
    {
        std::cout << val << "." << std::endl;
        ++val;
    }
    else
    {
        std::cout << val << std::endl;
        ++val;
    }
}
return 0;

}

If the two integers entered are 1 and 5, why does this output
5. instead of 1 2 3 4 5.?

  • 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-05-28T05:28:50+00:00Added an answer on May 28, 2026 at 5:28 am

    First, you are using the assignment operator = where you should use the comparison operator ==. Second, the for loop already increments val due to the third statement of the for loop (for (...; ...; ++val)). As such, there is no need to increment val from within the body of the loop.

    Also, considering that you want to print all results in a single line, you should output a space after each iteration rather than std::endl. Note that the last iteration is an exception as you will want to output a period rather than a space. In the fixed version below, I have used the ternary operator in the loop body to accomplish this.

    std::cout << "Enter two integers, pressing <ENTER> after each integer." << std::endl;
    int num1, num2, lower, upper;
    std::cin >> num1 >> num2;
    if (num1 >= num2)
    {
        upper = num1;
        lower = num2;
    }
    else if (num1 < num2)
    {
        upper = num2;
        lower = num1;
    }
    
    std::cout << "All integers between " << lower << " and " << upper << " are:" << std::endl;
    for (int val = lower; val <= upper; ++val)
    {
        std::cout << val << ((val == upper) ? "." : " ");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a program which will preform texture synthesis. I have been away
I have been writing a program that watches a directory and when files are
Since I have been writing a big python program for my project. I have
Good day! I have met problem with synchronizing threads. I am writing program which
I have been writing a program that is quite complex compared to what I
I am writing a java program which would act upon the input which would
I am writing a very performance intense program and have been using C, but
I've been writing a program using directx11, and I have written a basic camera
I understand object oriented programming, and have been writing OO programs for a long
I have been writing C++ Console/CMD-line applications for about a year now and would

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.