I would like to be able to return the cycle but including this code the while equation (age0)

3

I want to make this code when it is greater than 25 and less than zero (negative numbers) I return to the beginning of the program "write age"

#include <iostream>
#include <conio.h>
using namespace std;

main()
{

  int edad;
  do{
    cout<<"escribir edad "; cin>>edad;
    if (edad>0 && edad<=25) {
      cout<<"su edad es " <<edad;
    }
    else if (0>edad)
    {
      cout<<"la edad no es correcta \n";
    }
    else if (edad>25)
    {
      cout<<"la edad no es correcta \n";
    }
  }
  while(edad>25);
  system("PAUSE");
  return 0;

  getch();
}
    
asked by edwin_ucli 19.11.2016 в 00:38
source

2 answers

6

Try like that. yes in the while the condition is already greater than 25 >25 and less than 0 <0 , it is not necessary to check again with if , to validate this would be like this.

#include <iostream>
#include <conio.h>
using namespace std;

 main()
 {
 int edad=0;
 do{
   cout<<"escribir edad "; 
   cin>>edad;
  }
  while(edad>25 || edad <0);
  cout<<"su edad es " <<edad;

 return 0;
}
    
answered by 19.11.2016 / 00:48
source
0

For this, you can use the negation of a valid expression, with the exclamation point ! .

  • This expression is if the age is between 0 and 25. edad>0 && edad<=25
  • To deny that without changing the plus or minus sign > , you can use the ! , in this way, grouping in parentheses and using the exclamation mark. !(edad>0 && edad<=25)

Code:

#include <iostream>
#include <conio.h>
using namespace std;

main()
{
    int edad;
    while( !(edad>0 && edad<=25) )
    {
        cout<<"La edad no es correcta.\n";
        cout <<"Escribir edad: "; cin>>edad;
    }
    cout<<"Su edad es " <<edad;
    system("PAUSE");
    getch();
    return 0;
}
    
answered by 23.09.2018 в 17:25