# C++ Nested If



## GavinVG (Sep 22, 2011)

* I have to make sure that no unneeded tests are made. That is, if you find that the GPA is that for Suspended, then do not additionally test for the other conditions. Once you have found a match, when finished displaying the results, do not subject that set of input data to additional test
conditions.
Would i have to put if and else statements to condense the program?
also the output has to look like this 

      ID                   GPA              Status
123456789            3.25          Dean's List


(those are random id and GPA and are aligned under eachother spaced apart evenly)
*

#include <iostream> //for cout
#include <iomanip> //for endl
using namespace std;

int main()
{
	int id;
	double GPA;

	cout<<"Enter Student ID: ";

		cin>> id;

		cout<<endl;

		cout<<"Enter Current GPA: ";
			cin>>GPA;

			cout<<id<<endl;

	if(GPA <1.0)
		cout<<"Suspended";
	if(GPA >= 1.0 && GPA < 2.0)
		cout<<"Probation";
	if(GPA >= 2.0 && GPA < 3.0)
		cout<<"Satisfactory";
	if(GPA >= 3.0 && GPA < 4.0)
		cout<<"Dean's List";
	if(GPA >= 4.0)
		cout<<"President's List";

	cout<<endl;





	system("PAUSE");

	return 0;
}


----------



## Kreij (Sep 22, 2011)

Take the test out of the main method and put it in another method.
Call the method from main.
If any test returns a true value, return to the calling method.
You can return a value if needed.


```
int main ()
{
    string result = GetGPALevel (3.5f);
    cout<<result;
}

private string GetGPALevel(float GPA)
{
    if (GPA < 1.0f) return "Suspended";
    if (GAP ......) return "whatever";

    // if value does not match any if statment
    return "Value out of bounds";
}
```

There are several ways of doing this.


----------



## GavinVG (Sep 22, 2011)

I have a new question: If the "ID" entered by the user has more than 9 numbers precision wise  than i have to make it output and error message how would i do that? using a while statement


----------



## Kreij (Sep 22, 2011)

It depends upon the type of the variable that the user is entering.

If it's a string, just check the length of the input string.

If it's an integer, you can use something like "if (ID > 999999999)"

If it's a decimal value, (999.99999) then convert to a string and check length.


----------

