/*
Write a program that reads two integers num1 and num2 from the user, then displays the total of the all
of the numbers between num1 and num2 (inclusive). Your program should handle the following:
1. Make sure the numbers entered are between 0 and 100.
2. Make sure the second number is greater than or equal to the first number entered.
*/

I'm getting one error: illegal use of else without match if statement. I can't seem to figure out whats wrong? Can anyone help?

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    int num1, num2;
    long sum = 0;
    cout << "Please enter an integer between 0 and 100: ";
    cin >> num1;
    if (num1 >= 0 && num1 <= 100)

        cout << "Please enter an integer between " << num1 << " and 100: ";
        cin >> num2;
        else cout << "Please read and follow the directions!";

    if (num2 >= num1 && num2 <=100)

        while (num2 >= num1)
        sum += num1++;
        else cout << "Please read and follow the directions!";

    cout << "The total of the integers between " << num1 << " and " << num2 << "is: " << sum;

}

Recommended Answers

All 9 Replies

Multiple lines of code within if statements must be enclosed in brackets { and }

if( condition )
{
    // do this
    // do that
}
else
{
    // do something else
}

Ok, now I'm getting 2 errors. Do you see whats wrong?

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    int num1, num2;
    long sum = 0;
    cout << "Please enter an integer between 0 and 100: ";
    cin >> num1;
    if (num1 >= 0 && num1 <= 100)
    {
        cout << "Please enter an integer between " << num1 << " and 100: ";
        cin >> num2;
    }
    else 
    {   
        cout << "Please read and follow the directions!";
    }
    if (num2 >= num1 && num2 <=100)
    {
        while (num2 >= num1)
        {
            sum += num1++;
        }}
        else 
        {   
            cout << "Please read and follow the directions!";
        }
    cout << "The total of the integers between " << num1 << " and " << num2 << "is: " << sum;

}
commented: No code tags -- again +0
commented: No code tags -- again +24

If think you are leaving out the return value.
Put

return 0;

before the final brace terminating the main() function.
Unless you didn't post your entire code...

Ok, now I'm getting 2 errors. Do you see whats wrong?

This is your lucky day -- I meant to give you negative rep for not using code tags but instead gave you positive rep :)

If think you are leaving out the return value.
Put

return 0;

before the final brace terminating the main() function.
Unless you didn't post your entire code...

The return 0 is still giving me errors :(

I am getting no compile errors when running your code.
I ran the code successfully by changing the while to a for loop:

for(int i=num1; i<=num2; i++)
sum += i;

This for loop sums up the numbers between num1 and num2 inclusive.

#include <iostream>
#include <string>
using namespace std;

int main ()
{
	int num1, num2;
	long sum = 0;
	cout << "Please enter an integer between 0 and 100: ";
	cin >> num1;
	if (num1 >= 0 && num1 <= 100)
	{
		cout << "Please enter an integer between " << num1 << " and 100: ";
		cin >> num2;
	}
	else 
	{	
		cout << "Please read and follow the directions!";
	}
	if (num2 >= num1 && num2 <=100)
	{
		for (int i=num1; i<=num2; i++)
		{
			sum += i;
		}}
		else 
		{	
			cout << "Please read and follow the directions!";
		}
	cout << "The total of the integers between " << num1 << " and " << num2 << "is: " << sum;

}

do I have too many brackets? Still getting errors with your suggestion.

In this most recent attempt, you followed my second suggestion but not my first. You must set a return value.
Use

return 0;

before the last brace.

Also I was able to run the code by removing one of the braces after the for loop.

I realized that the error messages were not displaying properly in your last attempt. Here is the revised body of the program:

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

if (num1 >= 0 && num1 <= 100)
{
cout << "Please enter an integer between " << num1 << " and 100: ";
cin >> num2;
}

if( num2 >= num1 && num2 <=100)
{
for(int i=num1; i<=num2; i++)
sum += i;
cout << "The total of the integers between " << num1 << " and " << num2 << " is: " << sum<<endl;
}

else
{	
cout << "Please read and follow the directions!"<<endl;
}

This is much clearer and efficient than the code I originally proposed to you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.