c++ – How to clean up the name array so it doesn't have the rest of the previous strings

Question:

#include <iostream>
#include <string.h>
using namespace std;

int main() {
    int condicao;

    cin >> condicao; //Determina o tamanho do vetor //


    while(condicao != 0){

        char nome[100];

        for(int iniciar = 0;iniciar < condicao;iniciar++){

            cin >> nome[iniciar];

        }


        for (int i = condicao - 1; i >= 0; i--) {

            cout << nome[i];

        }

        cout << endl;
        cin >> condicao;
    }
    return 0;
}

Answer:

The code is too complicated and mixes up things from C that aren't ideal. Use a string and then everything is very simple. Look:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string nome;
    cin >> nome;
    for (int i = nome.length() - 1; i >= 0; i--) {
        cout << nome[i];
    }
}

See working on ideone . And on repl.it. Also posted on GitHub for future reference .

Scroll to Top