### problem in c++ hello everyone, firstly happy holidays

now to the problem:hello everyone could you help fix my code it keeps repeating the same question and doesn't print the necessary results.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136`` `````` #include #include #include #define l 200 #include using namespace std; ///P inteiros, sendo o i-ésimo número a indicação da ordem de chegada do piloto ///i na corrida (o primeiro número indica a ordem de chegada do piloto 1 naquela ///corrida, o segundo número indica a ordem de chegada do piloto 2 na corrida, e ///assim por diante). struct Piloto { int identificador; int pontuacao = 0; }; void limpaMatriz(int matriz[l][l]) { for(int i = 0; i < l; i++) { for(int j = 0; j < l; j++) { matriz[i][j] = 0; } } } bool ordenaPilotos(Piloto a, Piloto b) { return a.pontuacao > b.pontuacao; } bool eOrdenaPorIdentificador(Piloto a, Piloto b) { return a.identificador < b.identificador; } int main() { setlocale(LC_ALL, "Portuguese"); while(1) { int corridas, pilotos; cout << "digite numero de corridas e de pilotos( caso queira sair digite 0 e 0)"<> corridas >> pilotos; if(pilotos <=0 and corridas <= 0) { break; } int posicoes_por_corrida[corridas][pilotos]; for(int i = 0; i < corridas; i++) { for(int j = 0; j < pilotos; j++) { cout<< "posiçoes por corridas"<> posicoes_por_corrida[i][j]; } } int sistemas; cout <<" sistema até 10 \n"<> sistemas; int descricao_sistemas[l][l]; limpaMatriz(descricao_sistemas); int posicao_final[sistemas]; for(int i = 0; i < sistemas; i++) { cout <<"posição final" <> posicao_final[i]; for(int j = 0 ; j < posicao_final[i]; j++) { cout <<"Descrição do sistema(até 100)\n"; cin >> descricao_sistemas[i][j]; } } Piloto piloto[pilotos]; for(int i = 0; i < pilotos; i++) { piloto[i].identificador = i + 1; } for(int h = 0; h < sistemas; h++) { for(int i = 0; i < corridas; i++) { for(int j = 0; j < pilotos; j++) { int indice = posicoes_por_corrida[i][j] - 1; if(indice < posicao_final[h]) { piloto[j].pontuacao += descricao_sistemas[h][indice]; } } } sort(piloto, piloto + pilotos, ordenaPilotos); int maior = piloto.pontuacao; int repeticao = 0; for(int i = 0; i < pilotos; i++) { if(maior == piloto[i].pontuacao) { repeticao++; } } if(repeticao == 1) { cout << piloto.identificador; } else if(repeticao > 1) { sort(piloto, piloto + repeticao, eOrdenaPorIdentificador); for(int i = 0; i < repeticao; i++) { cout << piloto[i].identificador << " "; } } cout << "\n"; for(int i = 0; i < pilotos; i++) { piloto[i].pontuacao = 0; } for(int i = 0; i < pilotos; i++) { piloto[i].identificador = i + 1; } for(int i = 0; i < pilotos; i++) { piloto[i].pontuacao = 0; } } } return 0; } `````` when i ran it it just exited after getting a value.
what I see:
your first loop gets 2 numbers.
if both are negative, it exits the loop. if not, it goes again.
however they are used (illegally, since array sizes CANNOT be variables) to craft an array where the size can't be negative.
it looks like it should say
do
cout << "prompt stuff\n";
cin >> corridas >> pilotos;
while(corridas <1 || pilotos <1); //must be 1 or more NOTE THE OR, AND is incorrect here!

also note that crridas & pilotos are LOCAL TO THE LOOP and should not continue to exist outside the loop. so the values you read in are discarded! Declare them outside the loop at main's scope.

{
int x;
}//x is destroyed here, it only exists in the scope (set of braces) where it was created.

note: see how I cleaned up the if/break + infinite loop just by changing the loop style and its condition? See how that is cleaner? Your idea is OK (your logic is the problem, not the idea) but it is clunky.
Last edited on could you explain me better?
what do you mean illegal?
the code has a bug, because when placing 2 variables with espõ between them, the error
Last edited on Line 49,65,78: In standard C++, array bounds must be constant values. Your array bounds are not constant values. That's not legal in standard C++. If the size of the array is not a fixed value that is known beforehand, i.e. at compile-time, and if you are programming in C++ (not just C), then it's probably the best to use a std::vector.

 ``12345678`` ``````void foo(const size_t n) { std::vector vec(n); for (size_t i = 0; i < n; ++i) { vec[n] = /*whatever*/; } }``````
Last edited on
Topic archived. No new replies allowed.