Getline reading, not le a string and tb when accessing an array that does not exist or size insufficiency

Asked

Viewed 79 times

0

Well, that’s a player rating of a game of two-on-two, where it ranks the match in which it stopped.

#include<iostream>
using namespace std;

int main()
{
    int N,Ri,Ei,partidas,tamanho,contador,posicao1,i,achar,total1,total2;
    string palavra;

    cin>>N;

    partidas=N/2;
    achar=0;
    int demora[N+5],classificacao[N+5],rapidez[N+5],total[N+5];

    contador=0;
    while(contador<N)
    {//Lê a quantidade de jogadores
        cin>>Ri>>Ei;

        demora[contador]=Ri;
        rapidez[contador]=Ei;
        classificacao[contador]=0;

        contador++;
    }

    cin.ignore();
    getline(cin,palavra);
    //cout<<palavra<<" esse é o tamnho"<<endl;

    tamanho=palavra.length();
    contador=1;//partida de numero 1

    total[0]=(rapidez[0]*tamanho)+demora[0];
    for(i=1;i<N;i++)
    {//vai me cada vetor e classifica os jogadores
        total[i]=(rapidez[i]*tamanho)+demora[i];

        if(total[i]>total[i-1])
        {//classifica primeira variavel perdedor
            classificacao[i]=contador;
            classificacao[i-1]=contador+1;//vai pra proxima rodada
            i++;
        }
        else
        {//classifica a segunda variavel perdedor
            classificacao[i-1]=contador;
            classificacao[i]=contador+1;//vai pra proxima rodada
            i++;
        }
    }

    contador=2;//Partidas dois se houver entra no while
    while(partidas!=1)
    {//

        //para as partidas
        i=0;
        while(i<N)
        {//percorre s o vetor


            while(achar!=1)
            {//achar o primeiro
                if(classificacao[i]==contador)
                {//o primeiro participante
                    posicao1=i;
                    total1=(tamanho*contador*rapidez[i])+demora[i];
                    achar=1;
                }
                i++;
            }
            //continuar procurando aprtir do primeiro
            while(achar!=2)
            {//achar o primeiro
                if(classificacao[i]==contador)
                {//o primeiro participante

                    total2=(tamanho*contador*rapidez[i])+demora[i];
                    achar=2;

                    if(total1>total2)
                    {//
                        classificacao[i]=contador+1;//vai pra proxima rodada
                    }
                    else
                    {//classifica a segunda variavel perdedor
                        classificacao[posicao1]=contador+1;//vai pra proxima rodada
                    }
                }
                i++;
            }
        }
        achar=0;
        contador++;//qual partida esta
        partidas/=2;//dimuniu as partidas
        tamanho*=2;//cada rodada o tamanho da string muda
    }

    for(int i=0;i<N;i++)
        {

            if(i==N-1)
            {
                cout<<classificacao[i]<<endl;
            }
            else
            {
                cout<<classificacao[i]<<" ";
            }
    }
}

example of input that should be:

4

4 1

3 1

2 1

1 1

ABCD

sometimes prints normal, but does not consider string size other times of Runtime error.

  • Did not notice the problem, try to be as detailed as possible. "Sometimes prints normal" - and sometimes not print normal prints what ? And what line of code gives the wrong impression ? what while that "goes to infinity" ? Attention to the interlude of getline and cin >> that through line breaks and types used may not function as hold.

  • It is the entry of the line break that does not read the getline

  • I did a little test and I didn’t see any problem with the cin and getline. See the test on ideone. The cin.ignore(); that you have before the getline already consumes the line break that lags behind so this part has no problem.

  • but you did not put the line breaks so https://ideone.com/vmgQAz and gave error

  • Your last example of ideone has no input values. You put the input values in the notes. However, you can give Enter blank in a reading with cin >> an integer which only when the integer is written will be read, but in a getline a blank enter is valid and is an empty string. For this reason 1 1 and ABCD can’t have an extra Enter.

No answers

Browser other questions tagged

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