How to clear the array name so it doesn’t end up with the rest of the previous strings

Asked

Viewed 1,554 times

3

#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;
}
  • What is your doubt?

  • How do I clean Vector, since when typing a new chain of charters the previously written string remained. For example, I type the string "avocado" and the return is "etacaba". Resuming, when I insert a chain, called "house", the return is etaasac, that is, there is the permanence of the previous chain, being noticed by the three last charters.

  • You want to read 100 strings, is that it? Or you want to read a string with 100 characters?

  • We’re talking about your code, if you don’t even know what he has to do.

  • It will print the string, typed by the user, backwards and forwards...

  • What is this parada?

  • It is what determines the size of the Vector.

  • 100 strings with 100 charters.

  • You have to decide what you’re doing.

Show 4 more comments

2 answers

2

The code is too complicated and mixes things from C that is not ideal. Use a string same 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];
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

0

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

memset(nome,'\0', 100);
  • Thanks, but I got it sorted.

Browser other questions tagged

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