Displaying an error message using IF statement

Good Day,

I'm writing a program that prompts the user for two integers, and then proceeds to print the numbers in that range. However, I would like to have it display an error message if the second integer is greater than the first. I'm able to do it by just creating two while loops, one n1 > n2, and the other n1 < n2. But how could I do this with an if statement?

With my current code, it works if the input is in the right order. But if I do it incorrectly on purpose, it displays the error message and prompts for input. But if the input is true then it doesn't move on to printing the numbers in that range.

Any help would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

int main(){

	std::cout << "Please choose two integers " << std::endl;

	int n1 = 0, n2 = 0;
	std::cin >> n1 >> n2;

	if(n1 > n2){
		std::cout << "ERROR! the first input must be smaller than the second "
			  << std::endl;
		std::cin >> n1 >> n2;
	} else{
	std::cout << "The integers between "
		  << n1 << " and " << n2
		  << " are " << std::endl;

        while(n1 < n2 - 1){
		n1++;
		std::cout << n1 << std::endl;
	}
	}

	   return 0;
}
Last edited on
Step 1 is consistent indentation so you can visually see the flow of the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main()
{
  std::cout << "Please choose two integers " << std::endl;

  int n1 = 0, n2 = 0;
  std::cin >> n1 >> n2;

  if (n1 > n2) {
    std::cout << "ERROR! the first input must be smaller than the second " << std::endl;
    std::cin >> n1 >> n2;
  } else {
    std::cout << "The integers between " << n1 << " and " << n2 << " are " << std::endl;

    while (n1 < n2 - 1) {
      n1++;
      std::cout << n1 << std::endl;
    }
  }
  return 0;
}

Now it's easy to see that if you prompt the user for re-input, the new values are not going to make it into the top-level else clause you have.


Step 2, don't try to write the whole thing at once.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
  std::cout << "Please choose two integers " << std::endl;
  int n1 = 0, n2 = 0;

  do {
    std::cin >> n1 >> n2;
    if (n1 > n2) {
      std::cout << "ERROR! the first input must be smaller than the second " << std::endl;
    }
  } while ( n2 <= n1 );

  std::cout << "The integers between " << n1 << " and " << n2 << " are " << std::endl;

  return 0;
}


$ g++ foo.cpp
$ ./a.out 
Please choose two integers 
1 2
The integers between 1 and 2 are 
$ ./a.out 
Please choose two integers 
2 1
ERROR! the first input must be smaller than the second 
1 2
The integers between 1 and 2 are 

Start with the smallest bit of code that could work, then build on it one step at a time.
There's no point even attempting the while(n1 < n2 - 1) until you have your input loop sorted out.
I'm not sure exactly what you're trying to achieve?

Possibly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main() {
	int n1{}, n2{};

	do {
		std::cout << "Please choose two integers (smaller one first): ";
		std::cin >> n1 >> n2;

	} while ((n1 > n2) && (std::cout << "ERROR! the first input must be smaller than the second\n"));

	std::cout << "The integers between " << n1 << " and " << n2 << " are\n";

	while (n1 < n2 - 1)
		std::cout << ++n1 << ' ';
}


or perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <algorithm>

int main() {
	int n1{}, n2{};

	std::cout << "Please choose two integers: ";
	std::cin >> n1 >> n2;

	if (n1 > n2)
		std::swap(n1, n2);

	std::cout << "The integers between " << n1 << " and " << n2 << " are\n";

	while (n1 < n2 - 1)
		std::cout << ++n1 << ' ';
}

Salem C,
Hello, thanks for the help. In regard to formatting, I'll keep that in mind when writing code. Is there a list of conventions I can follow?

I'm using c++ primer to learn, and I haven't gotten to do/while statements yet.

What I'm asking is if it is possible to create this:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
  std::cout << "Please choose two integers " << std::endl;
  int n1 = 0, n2 = 0;

  do {
    std::cin >> n1 >> n2;
    if (n1 > n2) {
      std::cout << "ERROR! the first input must be smaller than the second " << std::endl;
    }
  } while ( n2 <= n1 );

  std::cout << "The integers between " << n1 << " and " << n2 << " are " << std::endl;

Only using while and if/else statements?

Seeplus,
The exercise in the book I'm using asks me to write a program that prompts the user for two inputs, then prints each number within the range of those two numbers. For example, 3 and 9 as input, 4 5 6 7 8.
Additionally, using an if statement, the program should output an error message if the first number inputted is larger than the second.

Looking over the code you provided, there are some useful statements that make things much easier.
Last edited on
> Is there a list of conventions I can follow?
https://en.wikipedia.org/wiki/Indentation_style
Basic rules:
1. For your own code, pick a style you like, be consistent in how you use it.
2. If you're editing existing code, be consistent with what's there already.
Yes - as per my first code suggestion.
unrelated but this is a design thing you should go ahead and learn now:
IF you can fix an error easily, FIX IT FOR THE USER, rather than complain about it and force the user to comply.

this is a prime example: you can detect the problem (n1 and n2 are out of order) and you can fix it easily (std::swap). So, just fix it and proceed is a good approach. If your goal is to learn to do input validation, my comments do not apply; but in a real world program, few things are more annoying than having the software tell you "I know what is wrong, and I know how to fix it, but I am going to make you do that!".
Salem C,
Alright, thank you. I'll work on these conventions now, early on, so I don't have to unlearn incorrect formatting later.

Jonnin,
I completely agree. If I were writing code for someone then I would want the user experience to be as usable as possible, tending to the users experience is most important if you want people to actually use your product.
But what I'm trying to learn goes beyond the error message, just wrapping my head around loops and statements as per the exercise's original purpose.
Last edited on
Topic archived. No new replies allowed.