If you are using C ++, use the language, do not mix with C. Then use cin
for data entry and string
to save texts. It gets easier there.
One of the problems is that it looks like you're saving up to 23 names, but you're actually saving a name with up to 23 characters, since a string in C is an array of char
. Changing to type string
which is already a text, the array is being used to save a list of names.
The copy is also being made without much criterion (besides using C). He copied what he needed, but deep down the list had all the elements, although some would be left with trash, since there was no copy of the name. This logic needed to be changed.
In a real code I would change several other things. It would create a framework for storing all the data together, would use a Vector
instead of array , among other small changes and reorganizations.
It looks like this:
#include <iostream>
#include <locale.h>
#include <string.h>
#include <stdio.h>
using namespace std;
int main() {
setlocale(LC_ALL, "portuguese");
int idade, cont = 0;
string pro[23];
string nome[23];
string nr[23];
for (int i = 0; i < 2; i++) {
cout << "Digite o seu " << i + 1 <<"º nome, profissão e idade: ";
cin >> nome[i];
cin >> pro[i];
cin >> idade;
if (idade >= 18) {
nr[cont++] = nome[i];
}
}
cout << "número de pessoas maior de 18 anos: " << cont << endl;
for (int i = 0; i < cont; i++) {
cout << "nome: " << nr[i] << endl;
}
return 0;
}
See running on ideone .