Make an If in c++ by limiting the answers

Asked

Viewed 55 times

0

The user types the year, month, day and temperature, but I need to limit the answers.

  • ano may not be less than 1850 and greater than 2021
  • mes may not be less than 1 and greater than 12, dia may not be less than 1 and cannot be greater than 31
  • temperatura may not be less than -50 and greater than 65.

I tried that check but it won’t ...

cout<<"\nPor favor digite o dia: "; 
cin>>dia[i];
cout<<"\nPor favor digite o mês: ";
cin>>mes[i];
cout<<"\nPor favor digite o ano: ";
cin>>ano[i];
cout<<"\nPor favor digite a temperatura: ";
cin>>temperatura[i]; 
cout<<"\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"; 
if ((dia[i] > 30) || (mes[i] > 12) || (ano[i] < 1850 > 2021)) { 
    cout<<"\nAlgo esta inválido"; 
}

The program prints a message saying something is invalid.

  • You could put in the rest of the code so we can get an overview of the problem?

  • Apart from you not being validating some things, this is invalid: ano[i] < 1850 > 2021. You need to check one part at a time: ano[i] < 1850 || ano[i] > 2021.

  • good afternoon, so I did not put whole because it is a switch and this incomplete, I ask this information to the user and thereafter he chooses an option that will use this information for example average temperatures...

  • cool maybe that I did wrong even not to check one thing at a time... thank you

2 answers

0

To restrict an input to a specific value, you need to read the input within a loop.

If the input matches the range you are waiting for, you break the loop, otherwise the loop continues.

int dia = 0;

while(true) {
  cout << "Insira o dia (01-31): ";

  int entrada = 0;
  cin >> entrada;

  if(entrada >= 1 && entrada <= 31) {
    dia = entrada;

    break;
  } else {
    cout << "Entrada invalida.\n";
  }
}

-1

Well, since you left only part of the code, I had to take over a few things to be able to work on a solution for you, as you left the data as an array, and built the solution based on that, using an array int of only 2 elements to facilitate viewing, and the use of a bool to build a loop to keep asking for data until everything is right.

#include <iostream>
#include <locale.h>

using namespace std;

int main(){
    
    setlocale(LC_ALL,"Portuguese");
    
    int dia[2], mes[2], ano[2], temperatura[2];
    bool errado;
    //Vai pedir dados para os dois arrays, facilmente modificavel de usar uma variavel ou define no lugar do 2
    for(int i=0; i<2 ;i++){
        do{
            cout<<"Set de dados " << i+1 << "\n";
            
            cout<<"\nPor favor digite o dia: ";
            cin>>dia[i];
            cout<<"\nPor favor digite o mês: ";
            cin>>mes[i];
            cout<<"\nPor favor digite o ano: ";
            cin>>ano[i];
            cout<<"\nPor favor digite a temperatura: ";
            cin>>temperatura[i]; 
            cout<<"\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"; 
    
            if ( (dia[i] > 30 || dia[i] < 1) || (mes[i] > 12 || mes[i] < 1) || (ano[i] < 1850 || ano[i] > 2021) || (temperatura[i] < -50 || temperatura[i] > 65)  ) { 
                cout<<"\nAlgo esta inválido! Digite novamente!\n";
                errado = true;
                i--;
            }else errado = false;
        }while(errado);
        
    }
    
    return 0;
}

But as Rafael Tavares mentioned, you can not compare more than 2 things at the same time in C, you have to do one by one, as I exemplified

Browser other questions tagged

You are not signed in. Login or sign up in order to post.