How to use a function to check the requirements imposed on a variable in C++?

Asked

Viewed 43 times

0

I want to make a code to train structs. In this case, since the number of cars is 4, I want the choice variable to be limited in the closed range from 1 to 4 "[1,4]".

So the logic is:

While this variable is less than 0 or more than 4, you will ask to type again. Hence I used a while.

I tried to use a while alone, but it didn’t work, so I decided to use a function, which checks the value of the choice variable. If it meets the requirements, it returns 1 (true), else, it returns 0 (false).

#include <iostream>
#include <list>

using namespace std;

struct Carros{
    int coresDisp,motores,modelos,ano;
    string nome;
};




bool verificaEscolha(int esc); //prototipando a função




int main(){

    int escolha;

    list <string> cores;
    cores.push_front("Vermelho");
    cores.push_front("Azul");
    cores.push_front("Verde");
    cores.push_front("Amarelo");
    cores.push_front("Preto");
    cores.push_front("Branco");
    cores.push_front("Cinza");

    list <string> motores;
    motores.push_front("2.0");
    motores.push_front("1.5");
    motores.push_front("1.0");

    list <string> modelos;
    modelos.push_front("Completo");
    modelos.push_front("Medio");
    modelos.push_front("Basico");

    list <int> ano;

    for(int i=2005;i<=2020;i++){
        ano.push_front(i);
    }


    Carros car1;
    Carros car2;
    Carros car3;
    Carros car4;

    car1.nome = "Vulcano";
    car2.nome = "Tornado";
    car3.nome = "Furia";
    car4.nome = "Ninjasso";


    cout << "Carros disponiveis: \n\n\n";
    cout << car1.nome << "[1]     " << car2.nome << "[2]     "<< car3.nome<< "[3]     "<< car4.nome<<"[4]     ";

    cout << "\n\n     :               ";
    cin >> escolha;

    verificaEscolha(escolha);

    while(verificaEscolha(escolha)==false){
        cout << "Essa escolha naum eh valida, tente novamente: ";
        cin >> escolha;
        verificaEscolha(escolha);
    }

    return 0;
}

bool verificaEscolha(int esc){
    if(0<esc<=4){
        return 1;
    }
    else{
        return 0;
    }
}

Ignore the other pieces of code.

1 answer

0


The solution when something doesn’t work is to investigate what is wrong, to learn how it is right, not to complicate more and cause more mistakes.

Your code has too many things, it’s not about functional programming as you’ve classified the question, and what you really want is just to do a while simple, just gotta do it right, don’t know what was wrong because it wasn’t in the question.

I switched from list to vector because I’m sure you don’t want a list, I imagine you don’t even know the implications of using a list that is more complex and less performative. And I organized the code a little bit.

Something tells me that the code has other conceptual problems, but I can’t talk about without understanding it more deeply.

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

struct Carros {
    string nome;
    int coresDisp, motores, modelos, ano;
};

int main() {
    vector<string> cores = { "Vermelho", "Azul", "Verde", "Amarelo", "Preto", "Branco", "Cinza" };
    vector<string> motores = { "2.0", "1.5", "1.0" };
    vector<string> modelos = { "Completo", "Medio", "Basico" };
    vector<int> ano;
    for (int i = 2005; i <= 2020; i++) ano.push_back(i);
    Carros car1 = { "Vulcano" };
    Carros car2 = { "Tornado" };
    Carros car3 = { "Furia" };
    Carros car4 = { "Ninjasso" };
    cout << "Carros disponiveis:\n\n";
    cout << car1.nome << "[1]     " << car2.nome << "[2]     " << car3.nome << "[3]     " << car4.nome << "[4]     ";
    cout << "\n\n     :               ";
    while (true) {
        int escolha = 0;
        cin >> escolha;
        if (escolha > 0 && escolha < 5) break;
        cout << "Essa escolha naum eh valida, tente novamente: ";
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Thank you. But the goal was to train lists, because it is part of the video classes I am watching. He didn’t pass that exercise, but I invented doing.

  • 1

    Train wrong you will only get used to the mistake, never use list where it is not necessary, this is a mistake.

Browser other questions tagged

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