I need help with an assignment I am working on. Here is the assignment:

count all the even numbers between X and Y, where X and Y are entered by the user.  Do not include X and Y.
EXAMPLE 1:
Please enter an integer: 0
Please enter another integer: 10
The number of even numbers between 0 and 10 is 4

EXAMPLE 2:
Please enter an integer: 10
Please enter another integer: 0
The number of even numbers between 10 and 0 is 4

Note that the numbers can be entered in any order (that is, the lower one does not have to come first).

I have it just about finished but it is not working properly. Each time I run it, I don't get 4, it keeps changing. I know it's gotta be something small that I am missing, but could you help me. Here is what I have:

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    int min, max, number = 0;
    unsigned seed = (unsigned long) (time(NULL));
    srand(seed);
    cout << "Please enter an integer: ";
    cin >> min;

    cin.ignore();
    cout << "Please enter another integer: ";
    cin >> max;
    int range = max - min + 1;
    for (int counter = 0; counter <= number; counter++)
    {
        int number = rand()/100%range + min;
        cout << "The number of even numbers between " << min << " and " << max
            << " is " << number;
            break;

    }
    cout << endl;

}

What is wrong with this program that the number printed keeps changing?

Recommended Answers

All 2 Replies

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
	int min, max, number = 0;
	unsigned seed = (unsigned long) (time(NULL));
	srand(seed);
	cout << "Please enter an integer: ";
	cin >> min;

	cin.ignore();
	cout << "Please enter another integer: ";
	cin >> max;
	int range = max - min + 1;
	for (int counter = 0; counter <= number; counter++)
	{
		int number = rand()/100%range + min;
		cout << "The number of even numbers between " << min << " and " << max
			<< " is " << number;
		break;

	}
	cout << endl;

}

I don't understand what this problem has to do with random numbers and why you are generating them. You are getting different results each time because you are changing number each time through the loop. You are displaying number, which is a random number, so it's going to change. I don't see anywhere where you test to see whether any integer is even or odd, which is what you are supposed to be counting.

Get the random numbers out of the program since there is nothing random about the problem. If you want to do this by using a for-loop (not necessary in my view; this is a fairly uncomplicated calculation that doesn't need any loops), you'll need to set up a counter, initialize it to 0, then set up a for-loop based on min and max. Inside the for loop, test some integer for evenness. If that integer is even, increment your counter.

thanks!! I appreciate it : )!

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.