TÓPICO

Presentacion error. C++.HELP ME

NahuelMeyer perguntou 4 years ago

include

using namespace std;

int main(){

int Cant=1,Max=0; //Cant= Cantidad de palabras, Max=La palabra mas larga

while(Cant!=0){

cin>>Cant;

string Pal[Cant];

for(int x=0;x<Cant;x++){
    cin>>Pal[x];
    if(Pal[x].length()>Max){Max=Pal[x].length(); }
}

for(int j=0;j<Cant;j++){

        for(int i=0;i<(Max-Pal[j].length()); i++  ){
            cout<<" ";
        }

    cout<<Pal[j]<<endl;

}

cout<<endl<<endl;   

    Max=0;

    }

return 0;   

}

Este tópico foi resolvido e não pode receber novas respostas.

  • brrossetti respondido 4 years ago

    You need to break after reading Cant if Cant is zero, otherwise you are going to print a endl at the end of the while loop:

    while (1) {
      cin >> Cant;
      if (Cant == 0) break;
      ...
    }

    You can use cout.width(Max) to set the maximum width of output instead of the for loop:

    for (int j = 0 ; j < Cant; j++) {
      cout.width(Max);
      cout << Pal[j] << endl;
    }

    You should print a newline BETWEEN two test cases, you can do something like:

    int k = 0;
    while (1) {
      if (k > 0) cout << endl;
      ...
      cout << endl;
      Max = 0;
      k++;
    }
  • NahuelMeyer respondido 4 years ago

    Muchisimas gracias por la ayuda <3

  • brrossetti respondido 4 years ago

    Dúvida resolvida. Código removido.

    My real solution is almost exacly like this, but I used a queue of strings instead of a string array