Program that inputs 2 positive integers

I am new to c++/computer programming. I am able to output 2 positive integers and have it say invalid when a negative integers is entered. But I also need the terminal to ask the user to reenter a positive integer when a negative is entered.

int main() {

int number1, number2;
cout << "Please input two positive integers for number1 and number2: " << endl;
cin >> number1 >> number2;
if (number1 > 0 && number2 > 0) {
cout << "number1: " << number1 << ", " << "number2: " << number2 << endl;
}
else {
cout << "Error: Negative integer entered" << endl;
}

return 0;

}
perhaps do the repeated requests while either one or the number is ≤0
=)
Last edited on
You want to repeat something, aka loop: http://www.cplusplus.com/doc/tutorial/control/


You want to get two numbers. What if you shift from
GET num1 AND num2

into
GET num
num1 = num
GET num
num2 = num

Both approaches do yield two numbers.
Last edited on
Hello buckeye10,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

You have a good start to your program. Along the lines of what icy1 said I think this will work better for you.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <limits>

int main()
{
	int number1{}, number2{};

	std::cout << "Please input two positive integers for number1 and number2: " << std::endl;

	std::cout << "\n Enter number 1: ";
	std::cin >> number1;

	while (!std::cin || number1 < 0)
	{
		if (!std::cin)
		{
			std::cout << "\n Error: Invalid entry. Please enter an interger greater than 0: ";
			std::cin.clear();
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>. Clears the input buffer.
		}
		else
			std::cout << "\n Error: Negative integer entered. Please try again\n";

		std::cout << "\n Enter number 1: ";
		std::cin >> number1;
	}

	std::cout << "\n Enter number 2: ";
	std::cin >> number2;

	if (number1 > 0 && number2 > 0)
	{
		std::cout << '\n' <<  "number1: " << number1 << ", " << "number2: " << number2 << std::endl;
	}
	else
	{
		std::cout << "Error: Negative integer entered" << std::endl;
	}

	// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue";
	std::cin.get();

	return 0;
}

When posting a program it is best to include the "#includes" in your program. Someone might see something that you may have missed.

Line 6. It is not always necessary to initialize your variables, but it is a good idea. The empty {}s will initialize to zero or you can put any number that you may need inside the {}s.

Lines 10 and 11. As keskiverto points out sometimes it is better to input one number at a time and as you can see the code works best dealing with one number at a time.

Line 13. In the while condition the "!std::cin" will catch anything that is not a numeric value, i.e., "int" "double" and the others.

Line 18. Clears the state bits that caused "std::cin" to fail, so it can be used again.

Line 19. Clears the input buffer.

After the if/else statement the last two lines of the while loop allow you to reenter the number.

After the input of "number2" you should repeat the while loop.

This tends to make the final if/else unnecessary as the if and the else have been taken care of in the while loops.

The lines just before the "return" I and and many others need to keep the console window open before the program ends. It is also useful when developing the program. It can be commented out or removed when you have finished the program if you do not need it.

Hope that helps,

Andy
Topic archived. No new replies allowed.