0
I want to make a code to train struct
s. 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.
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.
– Vitor Corrêa
Train wrong you will only get used to the mistake, never use list where it is not necessary, this is a mistake.
– Maniero