How to clear the vector name so that it does not stay with the rest of the previous strings

2
#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;
}
    
asked by anonymous 16.03.2017 / 02:52

2 answers

2

The code is too complicated and it mixes stuff from the C that is not ideal. Use% w / w% and then everything becomes very simple. See:

#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 running on ideone . And at Coding Ground . Also I put it in GitHub for future reference .

    
16.03.2017 / 03:41
0

You can try using memset to copy the end of string in all positions.

memset(nome,'
memset(nome,'%pre%', 100);
', 100);
    
16.03.2017 / 19:49