Pregunta

I have to load few input lines to my program, but I don't know why it doesn't works for me :(

int n;
    string count;
    int *tab = new int[256];
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        getline(cin, count);
        cin.ignore();
        for (int x = 0; x < count.size(); x++)
            if(count[x]!=' ')tab[(int)count[x]]++;
    }

This is what I've already got, the program has to read number of lines to load, then load line, and count every letter, and do it for n lines. It freezes when I type the last line of input, I tried getline without cin.ignore() but it was same.

¿Fue útil?

Solución

"e.g. I type: 2 Hello World hello world and it stucks"

How do you type it? There has to be a newline beween every word, if not, this is only one line so the program still waits for the second line (because you entered 2).

If you want the spaces to be the delimiters, just replace getline(cin, count); by cin >> count; and let cin.ignore(); away in every case (see chris comment).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top