Need help with assignment

Hi guys! I had this assignment which asks to calculate the average of numbers divisible by 5 in a range entered by the user.

I have solved it and all, but the problem is I want the input to be only in numbers,,, I tried to make it with an if statement and make the last else open for any weird inputs, and I built my main code inside the first else if, but what I did there just fails,

I searched the internet but only found thing that made me ask more, like:

1
2
cin.fail();
cin.clear();


What I did:

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
// Calculating the average of numbers divisible by 5 in a range entered by the user
#include <iostream>
using namespace std;

int main()
{
	// Variables
	int rangeBig, rangeSmall, counter = 0, sum = 0;

	// Inputs
	cout << "Enter the smallest number in the range: ";
	cin >> rangeSmall;
	cout << "Enter the biggest number in the range: ";
	cin >> rangeBig;

	// Conditions
	if (rangeSmall >= rangeBig) { cout << "Both ranges can't be equal, must only contain numbers, and the smallest number in the range can't be bigger than its biggest!" << endl; }
	else if (rangeBig > rangeSmall)
	{
		for (rangeSmall;rangeSmall <= rangeBig;rangeSmall++) // For loop
		{
			if (rangeSmall % 5 == 0) // Nested if
			{
				sum += rangeSmall;
				counter++;
			}
		}

		// Output
		cout << "Average of numbers divisible by 5 = " << sum / counter << endl;
		return sum / counter;
	}
	else { cout << "You must enter a number!" << endl; }
}
http://www.augustcouncil.com/~tgibson/tutorial/iotips.html#directly
1
2
3
4
5
6
7
8
9
10
11
12
13
// try, try again, until you do get a number:
while( !(std::cin >> rangeSmall) )
{
  std::cin.clear();
  std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
}

// try, try again, until you do get a number that is large enough:
while( !(std::cin >> rangeBig and rangeSmall < rangeBig) )
{
  std::cin.clear();
  std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
}



Btw, you do not need a loop for computing the average. Sum of arithmetic serie has an equation.


Line 31: why does the program return the average to the calling shell? The convention is to return 0 on successful exit.

Line 30: dividing an int with an int creates an int and that discards the fraction.
Force floating point math: static_cast<double>(sum) / count

Line 30. What if range was from 1 to 3? Division by 0.
Ok,

Line 30: What if I changed the start value from 0 to 1? That might fix it without altering the correct result?

dividing an int with an int creates an int and that discards the fraction.
Force floating point math: static_cast<double>(sum) / count
Can't get what you meant here.

-Assignment is about loops, so gotta solve them in loops.
Last edited on
If there are no numbers, then there is no sum and no average.
You can test after the loop whether there were any numbers (i.e. if count is larger than 0).

Division test:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
    std::cout << "integer division:\n";
    for ( int foo = 0; foo < 18; ++foo )
        std::cout << foo << "/7 = " << foo / 7 << '\n'; 

    std::cout << "\n\nfloating point division:\n";
    for ( int foo = 0; foo < 18; ++foo )
        std::cout << foo << "/7 = " << static_cast<double>(foo) / 7 << '\n'; 

    return 0;
}

Simpler example: average of 5 and 10 is (5+10)/2 == 15/2, isn't it? Your way the answer is 7, but I do recommend 7.5
Topic archived. No new replies allowed.