Okay, this roughly works, but the flaw seems to be with the
else
clause. I don't understand the loop condition enough to figure this out, but I want 2 things:
- The
else
clause to bring the user back to the
if
clause (that is, once they enter the integers correctly the calculation takes place, and
- Once the calculation has been completed, the program pauses to exit on prompt.
So how would I go about fixing it, and on a side note what is the cleanest way of getting that pause? I usually use
system ("pause")
, but that seems like a waste of processing plus it only works under Windows.
(The worst that can happen here is that I change the
if
to a
while
and remove the
else
, plus the necessary rearrange. I could do that, I just want it to work even if people enter the integers the wrong way.)
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
|
#include <iostream>
using namespace std;
int main()
{
int num1 = 0;
int num2 = 1;
cout << "Please select two integers (whole numbers), and enter them as prompted." << endl;
cout << "Please enter the smallest integer: ";
cin >> num1;
cout << "Please enter the largest integer: ";
cin >> num2;
if(num1 <= num2)
{
int num3 = 0;
for(int i=num1+1; i<num2; i++)
{
num3 += i;
}
cout << "The total of all integers between " << num1 << " and " << num2 << " is " << num3 << ".";
cin.get();
cin.get();
}
else(num1 > num2);
{
cout << "Please re-enter the integers in the opposite order." << endl;
cout << "Please enter the smallest integer: ";
cin >> num1;
cout << "Please enter the largest integer: ";
cin >> num2;
}
}
|