# Ratings from 0 to 100

4

Make a program in c ++ that asks me for a qualification and validates that it is valid between 0 and 100,   Once it is valid if it is greater than or equal to 70 it is approved, if it is less than 70 it is rejected, but if it is 0, it repeats the course,   if you get 100 you get a scholarship.

this is the code:

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

int main()
{
int calif;

/*
si es mayor o igual a 70, esta aprobado
si es menor a 70 esta reprobado
si saca 0 debe repetir curso
si saca 100 obtiene una beca
*/

cout<<"\n Ingrese la calificacion:  ";
cin>>calif;

if(calif==0)
{
cout<<"Repite curso";
}
else
if(calif>=70)
{
}
else
if(calif<=69)
{
}
else
if(calif>=100)
{
cout<<"Alumno con BECA";
}
return 0;
}
``````

The detail is that the 100 does not show it to me, they will know what I am wrong with or what is missing.

asked by Francisco Cardenas 20.07.2017 в 16:39
source

2

In this case, what is most recommended when verifying a condition with multiple options, is always to evaluate at the beginning the specific cases (0 and 100) and at the end those that would encompass a range. (greater than or equal to 70 || less than 70).

``````if ( calif == 0 ){
//Repite Curso
} else if ( calif == 100 ) {
//Obtiene Beca
} else if ( calif < 70 ) {
} else
//Aprobado ( ya que no se cumplio ninguna condicion anterior, es muy
//buena practica de programacion siempre poner los casos especificos al comienzo)
``````

Greetings, I hope you serve

source
5

The error occurs to you because the case of `100` is never checked since, when fulfilling the second condition `>= 70` , it executes that block and does not check more conditions since it was chained with `if-else` .

I suppose that the messages of `Repite curso` and `Alumno con beca` you want to show in addition to the messages of `Aprobado` or `Reprobado` . That is, in addition to showing the student's approved message, if you also get a scholarship, show that you have obtained it. And if the student is failing, if he also repeats a course, that shows both.

The code would look like this:

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

int main(void)
{
int calif;

/*
si es mayor o igual a 70, esta aprobado
si es menor a 70 esta reprobado
si saca 0 debe repetir curso
si saca 100 obtiene una beca
*/

cout << "\n Ingrese la calificacion:  ";
cin >> calif;

if (calif < 0 || calif > 100) {
cout << "ERROR: Dato introducido fuera de rango (0-100).\n";
return -1;
}

if (calif >= 70)
{

if (calif == 100) {
cout << "Alumno con BECA.\n";
}
}
else
{
cout << "Alumno Reprobado, LO SIENTO.\n";

if (calif == 0) {
cout << "Repite curso.\n";
}
}

return 0;
}
``````

Update : add the range of data entered in the code.

4

### Let's play a game.

We will play that I am your program and you have entered `100` within `calif` .

### We started.

I interpret `if(calif==0)` , I know that `calif` is `100` so the question is:

Is one hundred zero?

Obviously not, so I get into the branch `else` of which hangs another `if` .

I interpret `if(calif>=70)` , I know that `calif` is `100` so the question is:

Is one hundred more or equal to seventy?

Hey, yes! I enter the branch of `if` and I find that I have to show something on the screen, so I tell you:

``````Alumno Aprobado, FELICIDADES
``````

Since I have entered the `if` , I will not execute `else` , so I'm going to the end of this branch of conditions and I find `return 0` ;

Interpretation `return 0` , the program ends.

### Possible solution:

You can check that the number is exactly in a range instead of checking only one of the limits:

``````if (calif == 0)
{
cout<<"Repite curso";
}
else if (calif < 70 && !(calif <= 0))
{
}
else if (calif < 100 && !(calif <= 70))
{
}
else if(calif == 100)
{
cout<<"Alumno con BECA";
}
``````

1

The easiest thing is to do the checks in a strict ascending or descending order. Why?

Because in a chain `if - else` , in `else` we can discard everything that has already been evaluated in `if` .

Example with ascending order

``````if (calif == 0)
{
// repite curso
}
else if (calif < 70) // si llegamos aqui, calif != 0
{
}
else if (calif < 100) // si llegamos aqui, calif >= 70
{
}
else // si llegamos aqui, calif >= 100
{
// beca
}
``````

Example with decentent order

``````if (calif == 100)
{
// beca
}
else if (calif >= 70) // si llegamos aqui, calif != 100
{