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
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).
– Maniero