0

I wrote the following program with the instructions in the top comments: ( I am using visual studio on vista)

#include <iostream>

using namespace std;

int main()
{
	int num1, num2;
	int total = 0;

	cout << "Please enter an integer between 0 and 100: ";
	cin >> num1;
	int numx = num1;

	if (num1 < 0)
	{
		cout << "Please read and follow the directions!" << endl;
	}
	else if (num1 > 100)
	{
		cout << "Please read and follow the directions!" << endl;
	}
	else
	{
	cout << "Please enter an integer between 50 and 100: ";
	cin >> num2;
	}

	while (num1 <= num2)
	{
		total = total + num1;
		num1++;
	}
	if ((num1 >= 0) && (num1 <= 100) && (num2 >= 50) && (num2 <= 100))
	{
		cout << "The total of the integers between " << numx << " and " << num2 << " is " << total << endl;
	}
	else
	{
		cout << "Please read and follow the directions!" << endl;
	}
	
}

I am having trouble: if the user enters a number less than 0 or greater than 100, the output needs to be "Please read and follow the directions!" And, the program should stop there. It should not go on to "Please enter an integer between 50 and 100." I am really stumped. Any help would be greatly appreciated.

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by joed13k1941
0

something like

if( num < 0 || num > 100)
{
    cout << <message to user>
    return 1;
}

But normally instead of just exiting the program it should loop back and prompt for input again.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.