I will consider that the statement is exactly this. You are not doing what the statement asks. Note that it has two parts:
- "read a 50-element vector" (you’re doing much more than that on loop that should be very simple;
- "print the vector by disregarding the repeated values" (you are printing everything).
It may sound silly, but it makes a difference. Programming is like this, you have to be aware of all the details. There this second loop is that you should check if a value already exists.
Solving this, you will automatically not have the problem of loop get stuck.
Without modifying much and optimizing in the best way possible (but optimised in the most obvious way) I did a quick test that solves the way it was proposed. I didn’t get to do any extensive tests. I also didn’t modify your code much or improve the formatting of the output, I think this you know how to turn.
#include <iostream>
using namespace std;
#define MAX_ITENS 50 //número de itens a serem lidos se quiser modificar para testes
int main() {
int vet[MAX_ITENS];
bool repetido;
for(int i = 0; i < MAX_ITENS; i++) {
cout << ("Numero: ");
cin >> vet[i];
}
for(int i = 0; i < MAX_ITENS; i++) {
repetido = false;
for(int j = 0; j < i; j++) { //verifica se existem iguais apenas entre os anteriores
if (vet[j] == vet[i]) {
repetido = true;
break; //tem repetido, não precisa continuar com a procura
}
}
if (!repetido) {
cout << vet[i] << endl;
}
}
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
I will consider that the statement is exactly this. It already begins that you are not doing what the statement asks. Note that it has two parts: 1) "read a 50 element vector" (you are much more than that in loop which should be very simple; 2) "print the vector disregarding the repeated values" (you are printing everything). It may sound silly, but it makes a difference. Programming is like this, you have to be aware of all the details. There this second loop is that you should check if that value already exists. So probably if you do this, you will already know how to fix the problem loop.
– Maniero