The loop of choice must end or continue in a menu

Asked

Viewed 90 times

1

I have two objects that receive parameters by the standard system entry, but when the user chooses to continue or exit, the loop does not obey the output and when obeying the output does not obey the continue, I will leave the part of the code, outside the classes because it is not relevant to the problem:

#include "setter,getter.h"

using namespace std;

int main(void)
{

  char str;
  int ob1;
  int ob2;
  cout << "selecione a opcao do objeto 1:";
  cin >> ob1;
  cout << "selecione a opcao do objeto 2:";
  cin >> ob2;

  apc *obj1 = new apc(ob1);
  apc *obj2 = new apc(ob2);

  cout << obj1->getPublicInt() << "\n";
  cout << obj1->getPublicStrings() << "\n";

  cout << obj2->getPublicInt() << "\n";
  cout << obj2->getPublicStrings() << "\n";

  while (1)
  {
    cout << "continuar ? s ou n"
         << "\n";
    cin >> str;
    
    if (str = 's' or 'S')
    {
      main();
    }
    if (str = 'n' or 'N')
    {
      system("pause");
      return 0;
    }
    else
    {
      cout << "opcao invalida!";
    }
  }
}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

I think you want this:

int main() {
    while (1) {
        char str;
        cout << "selecione a opcao do objeto 1:";
        cin >> ob1;
        cout << "selecione a opcao do objeto 2:";
        cin >> ob2;
        int apc *obj1 = new apc(ob1);
        int apc *obj2 = new apc(ob2);
        cout << obj1->getPublicInt() << "\n";
        cout << obj1->getPublicStrings() << "\n";
        cout << obj2->getPublicInt() << "\n";
        cout << obj2->getPublicStrings() << "\n";
        while (1) {
            cout << "continuar ? s ou n" << "\n";
            cin >> str;
            if (str == 's' || str == 'S') break;
            if (str == 'n' || str == 'N') {
                system("pause");
                return 0;
            } else cout << "opcao invalida!";
        }
    }
}

I put in the Github for future reference.

Do not call the function again, this creates recursiveness and can in more extreme cases break the application, even if it works in exercise, will learn wrong, make a loop that repeats the operation, as you already know how to do.

The loop that will be more internal is only to ensure that the person has typed something valid, so if he has typed invalid continues on loop Intern, otherwise get out. If it is to continue typing, it only comes out of loop and stays inside the other maintaining the repetition of the outermost loop, and this is done with a break. If you want to leave return works well because it will come out of the two ties.

There was error in the text comparison, the correct operator is the equal == and not the attribution =. And yet you have to make the full comparison when you put together an expression just like 'S' she is always true, just the expression str == 'S' can give true or false according to the value of the variable, the compiler does not guess what you want to do there, you are responsible for writing the right code. And don’t use or, use ||.

In exercise it is no problem but it is wrong to allocate memory and not to dislocate as it did, it needs a delete. Behold What is the purpose of the free function()?.

Nobody cares about this, but I’m going to say it like this: before you make some code a little more elaborate, learn the basic syntax, how the language is, the concepts of programming, if you train the error, that’s what you’ll always do. Most people nowadays do that and make a lot of mistakes.

And

Fiat 147 todo detonado andando pelas ruas

  • kkkkkk, helped a lot, I knew it was possible to be made this loop so, but n went through my head, the question of operating the if with attribution, was in my failures attempts to identify the error, but after I saw it was not, I went back to the comparison, I still can not understand why two comparisons have to be made, I ended up forgetting these concepts, and or, knew that could not be used, but why should not be used ?

  • Is considered obsolete.

Browser other questions tagged

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