3
Follow the code with my suspicions:
Header:
class Name_pairs
{
public:
void read_names();
void read_ages();
void print() const;
void sort(); // Ordenar os nomes com as idades
private:
std::vector<std::string>name;
std::vector<double>age;
};
#endif
Implementation:
void Name_pairs::read_names()
{
cout << "Entre com os nomes desejados, digite 'SemNome' para finalizar a lista." << endl;
string NomesVetor;
while (cin >> NomesVetor && NomesVetor != "SemNome")
{
for (size_t x = 0; x < name.size(); ++x) // confere se há nomes duplicados
{
if (name[x] == NomesVetor) cout << "Nome repetido." << endl;
name.push_back(NomesVetor);
}
}
}
void Name_pairs::read_ages()
{
for (size_t x = 0; x < name.size(); ++x)
{
cout << "Nome de " << name[x] << ": " << endl;
double IdadesVetor;
cin >> IdadesVetor;
age.push_back(IdadesVetor);
}
}
void Name_pairs::print() const
{
for (size_t x = 0; x < name.size(); ++x)
{
cout << "Nome: " << name[x] << ", Idade: " << age[x] << endl;
}
}
void Name_pairs::sort()
{
vector<string> strCopia = name;
vector<double> dbCopia = age;
std::sort(begin(name), end(name));
for (size_t x = 0; x < name.size(); ++x)
{
for (size_t y = 0; y < name.size(); ++y)
{
if (name[x] == strCopia[y]) age[x] = dbCopia[y];
}
}
}
Main:
using namespace std;
int main()
{
Name_pairs objeto;
objeto.read_names();
objeto.read_ages();
system("pause");
return 0;
}
The error is when I enter the names and finish the input by typing 'Semnome', the program just closes. I’m afraid the error is in the function Read_names()
that may not be giving continuity to the read_ages()
or in itself Main.cpp
, since I’m starting classes now and I don’t understand very well yet the calls.
It is not clear what the mistake is.
– Pablo Almeida
Oh yes, sorry. The error is that the program terminates after entering the names and giving the Semnome. It simply ends.
– Marv