TÓPICO

Presentacion error. C++.HELP ME

[B]-Meyer-Nahuel-UTN FRSF 2021-AEDD perguntou 1 year 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.

  • [BCC BAURU] Rodrigo Rossetti respondido 1 year 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++;
    }
  • [B]-Meyer-Nahuel-UTN FRSF 2021-AEDD respondido 1 year ago

    Muchisimas gracias por la ayuda <3

  • [BCC BAURU] Rodrigo Rossetti respondido 1 year 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