0
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main () {
    string vet[10], aux;
    std::vector<char> name;
    int count=0, sum=0; 

    while (count<10) {
        getline(cin, aux);

        for (int i=0; i<aux.length(); i++)  name.push_back(aux[i]);

        for (int i=0; i<name.size(); i++) {
            name[i] = tolower(name[i]);                                 //para garantir que todos os caracteres estão em minúsculo  
            if (name[i] > 96 && name[i] < 123 && name[i] == 32)         //faixa de decimais das letras minúsculas e espaço em caso de nome composto
                sum += name[i];
        }

        char v[name.size()];                                            //vetor auxiliar criado para realizar a conversão de vetor de char para string

        for (int i=0; i<name.size(); i++)   {
            v[i] = name[i];
        }

        while (vet[sum%10] != "\0")                                     //para evitar colisão
            sum++;

        vet[sum%10] = (string) v;                                       //conversão para string e => K mod m = K % m em C++
        cout << vet[sum%10] << endl;
        count++;
        sum = 0;

        for (int i=0; i<name.size(); i++)   v[i] = '\0';

        name.clear();
    }

    cout << endl;

    for (int i=0; i<10; i++)    cout << vet[i] << endl;

    return 0;
}

This code is using the Hashing concept to store names inside of an array.

My question is:

Everytime that I try to insert a name with 8, 16 or 24 characters, while converting from char array to string, the compiler always put another 3 characters in front of them. If I try a name with 9, 17 or 25 characters, the compiler always put another 2 characters in front of them. And if I try a name with 10, 18 or 26 characters, the compiler always put another character in front of them.

Why does it happens? Is there a way to prevent it?

I need all the names to be exactly (but in lower case) as they were inserted on the input, but sorted according to the Hashing logic.

Thanks in advance!

Leonardo
  • 163
  • 9
  • 3
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: **[How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver Feb 22 '16 at 13:42
  • FYI: `char v[name.size()];` is not standard C++ when the array size is not a constant expression. – crashmstr Feb 22 '16 at 13:45
  • The condition of the if statement in line 19 will never be true. Maybe changing the last && with an || but I don't get your intent – Bob__ Feb 22 '16 at 13:53
  • @Bob__ Yea, it is ||. Hahaha The idea is to accept only lower case characters and space (32 on ascii table). – Leonardo Feb 22 '16 at 13:56
  • @NathanOliver Thanks for answering. I'll check it out. – Leonardo Feb 22 '16 at 13:59
  • 1
    **To others reading this**: You guys totally discourage people to ask on SO when unvoting questions like this. I see no problem with my post. – Leonardo Feb 22 '16 at 13:59

1 Answers1

2

The problem is here:

char v[name.size()];

As it was pointed out this is not standard C++ ...

Regardless, you can fix it like:

std::string v;    
v.resize(name.size());

More or less it has the same effect as your char array, except that it does not use a char array.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167