Exit loop as soon as the string name equals 0

Asked

Viewed 88 times

1

#include <iostream>
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

using namespace std;

int main()
{
    setlocale(LC_ALL, "portuguese");

   int idade[20];
   string nome[20];
   char sexo[20],expe[20];

    for (int i=0; i <3; i++){

        do{
            cout << "digite o seu " << i+1 <<"º nome, idade, sexo e se tem experiência no serviço: ";
            cin >> nome[i];
            cin >> idade[i];
            cin >> sexo[i];
            cin >> expe[i];

        }while (strcmp(nome, ab)); /* está linha é que está com problema*/

    }

    return 0;
}
  • 1

    "this line is in trouble" -> what problem? That is, what error occurs? P.S.: wouldn’t be the variable ab, that is not defined anywhere?

  • There are some problems, the first is that this does not seem to have any goal. Why do you need to do this? Explain the goal, the whole logic must be wrong. And I’ve told you not to mix C with C++.

  • is that I don’t know straight what is C and what is C++. The logic is to exit the loop once the "name" variable is set to 0.

  • 1

    Start studying these things. Shooting away everywhere, without a goal, without a path mapped out, won’t get you anywhere. You will delude yourself into thinking you are learning something and will only be following formulas that will be of no use in practice. What you’re trying to do doesn’t make sense, the problem is there. But if you insist I’ll fix the syntax.

1 answer

2


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

int main() {
    setlocale(LC_ALL, "portuguese");
    int idade[20];
    string nome[20];
    char sexo[20], expe[20];
    for (int i = 0; i < 3; i++) {
        do {
            cout << endl << "digite o seu " << i + 1 << "º nome, idade, sexo e se tem experiência no serviço: ";
            cin >> nome[i];
            cin >> idade[i];
            cin >> sexo[i];
            cin >> expe[i];
        } while (nome[i] == "0");
    }
}

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

Some things can be improved. Ideally it would be better to use C++ features and avoid C’s.

  • I may be mistaken, but I think he just wants to popular information on this array. It would be a single FOR using the array size. This "0" he quoted must be from the index of the array, which for him 0 is the last.

Browser other questions tagged

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