Number limits and range.

Hi,

I'm having trouble understanding numeric limits when trying to make a range.
It's essentially a program where you input 3 test scores, they have to be [0-100]
If you put anything lower than zero or higher than 100, it's supposed to say something like "Data value out of range.".

I know it has something to do with: <limits> and int_max, int_min.
But I have no clue as to where they go.

Here's my code:
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
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <limits>		// I know this is key to having a limit
#include <conio.h>
using namespace std;

int main(void)
{
	// 1 Input

	int testScore1, testScore2, testScore3;
	cout << "Input your 3 test scores in the range [0-100]" << endl;
	cout << "separated by spaces: ";
	cin >> testScore1 >> testScore2 >> testScore3; 	
	/////////////////////////////////////////////////////
	// Have trouble defining number limit parameters
	// I know it has something to do with 
	// int_min and int_max, but I can't figure it out...
	/////////////////////////////////////////////////////
	
	if ( !cin )
	{
		cin.clear();
		cin.ignore();

		cout << "Non-numeric input: Program terminating" << endl;
		cout << endl << "Press ENTER to finish...";
		cin.ignore(99,'\n');
		return 1;
	}

	// 2. Calculations with output
	if ( testScore1 >= 50 && testScore2 >= 50 && testScore3 >= 50 )  // If all grades are greater than or equal to 50, Pass
	{
	cout << "Grade: Pass :)";
	}
	else
	{
	testScore1 <= 50 && testScore2 <= 50 && testScore3 <= 50;	// If ALL grades are less than or equal to 49 , Fail
	(testScore1 + testScore2 + testScore3 / 3) <= 50;		// If AVERAGE of ALL grades less than 50, Fail
	testScore1 || testScore2 || testScore3 <= 40;			// If ANY test grade is less than 40, Fail
	cout << "Grade: FAIL :(";
	}
	
	// 3 Finish - Having trouble with console closing down, using <conio.h> to compensate
	getch();
}
Last edited on
I know it has something to do with: <limits> and int_max, int_min.


It doesn't. You're over thinking it.

This can be done with a very simple if statement.
I would write the code like this (This is just testScore1):

1
2
3
4
5
if (testScore1 >= 0 && testScore1 <= 100)
{
         //Write your calculations here.
}
else cout << "Data value out of range." << endl;


So it checks if testScore1 is between 0 and 100. If not, then it will display "Data value out of range." I don't think you would need extra ints for this operation (int_max and int_min). I hope this helped!

Thanx alpharens
Worked like a charm.

Makes me wonder why you can't find it anywhere in chapter 2, or the index.
Last edited on
Didn't want to start new topic over the same problem :P

I've managed to set a range and limit numbers higher than 100 or lower than 0,
but is there a way to make my code shorter?

I tried to make a specifier and float named anyTestScore
the result was something like this, but it didn't work. I think it's because I'm using float and int improperly.

1
2
3
4
5
6
7
8
9
10
	int anyTestScore = testScore1 || testScore2 || testScore3
	float anyTestScore = testScore1 || testScore2 || testScore3

	if ( anyTestScore < 0 || anyTestScore > 100 )
	{
		cout << endl << "Input out of range: Program terminating" << endl;
		cout << endl << "Press ENTER to finish...";
		cin.ignore(99,'\n');
		return 1;
	}


This is my working code, wondering if I can make it any shorter?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	if ( testScore1 < 0 || testScore1 > 100 )
	{
		cout << endl << "Input out of range: Program terminating" << endl;
		cout << endl << "Press ENTER to finish...";
		cin.ignore(99,'\n');
		return 1;
	}
	if ( testScore2 < 0 || testScore2 > 100 )
	{
		cout << "Input out of range: Program terminating" << endl;
		cout << endl << "Press ENTER to finish...";
		cin.ignore(99,'\n');
		return 1;
	}
	if ( testScore3 < 0 || testScore3 > 100 )
	{
		cout << "Input out of range: Program terminating" << endl;
		cout << endl << "Press ENTER to finish...";
		cin.ignore(99,'\n');
		return 1;
	}

I don't quite understand what you are trying to accomplish here, but I have noticed a few problems with your code.

First, I don't think you can use the OR (||) operator when declaring variables, you can only use it when comparing values (for example in an if statement).

Second, with your new int and float anyTestScore, there are no semicolons (;) at the end of the statement, which will also result in an error.

Third, you should either get rid of one of the anyTestScore variables or change the name of one of them because they both have the same identifier name, and that should result in an error.

To shorten the length of your working code, you can do this:
1
2
3
4
5
6
7
if ( (testScore1 < 0 || testScore1 > 100) || (testScore2 < 0 || testScore2 > 100) || (testScore3 < 0 || testScore3 > 100) )
{
	cout << "Input out of range: Program terminating" << endl;
	cout << endl << "Press ENTER to finish...";
	cin.ignore(99,'\n');
	return 1;
}

This configuration will work in this case, but if you plan each variable to do anything different, then you may have to revert it. Hope this helped!
1
2
int testScore1, testScore2, testScore3, /*a lot of typing*/, testScore42;
int testScore[42]; //magic 

http://cplusplus.com/doc/tutorial/arrays/
http://cplusplus.com/doc/tutorial/control/ (loops)

That will short your code. ;)

1
2
3
	testScore1 <= 50 && testScore2 <= 50 && testScore3 <= 50;	// If ALL grades are less than or equal to 49 , Fail
	(testScore1 + testScore2 + testScore3 / 3) <= 50;		// If AVERAGE of ALL grades less than 50, Fail
	testScore1 || testScore2 || testScore3 <= 40;			// If ANY test grade is less than 40, Fail 
c++ does not work that way.
If you want to test a condition you use if( condition ) where condition returns a bool value.
If you want to test several conditions, join them with Boolean operators (or, and, not)
By instance
1
2
if( cond1 or cond2 or cond3 )
  cout << "Something was true" << endl;


Now, you want to test all the values against one bias. If you don't use a loop, you will have to write the comparison in every condition (like in the first line) if( testScore <=40 || testScore2 <= 40|| testScore3 <= 40 ) /*statement*/

Another thing
if( (testScore1 + testScore2 + testScore3 / 3) <= 50 ) // If AVERAGE of ALL grades less than 50
_ Operator precedence: The division will be executed before than the sum.
_ Integer division returns an integer
Last edited on
Topic archived. No new replies allowed.