Code for earlier than expected

Asked

Viewed 628 times

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.

  • 2

    It is not clear what the mistake is.

  • Oh yes, sorry. The error is that the program terminates after entering the names and giving the Semnome. It simply ends.

1 answer

4


The problem is on this line:

name.push_back(NomesVetor);

It is in the wrong place and is not adding anything since it only adds if it is a repeated name, which is not what you want and should not be testing it. Thus:

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);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Note that he is reporting that he is repeating and is not stopping it. Maybe he is missing a continue there.

The code as a whole is not quite the way you usually do it, but this is another problem.

  • 1

    I was going to answer, but you were faster. Here’s my +1. :)

  • I am testing some methods and researching more practical ones, the for inside the for is replaceable and I will do it later. Thank you very much for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.