Program does not perform expected flow

0

After "Hello, my name is Locao ......" for me, in logic, should continue, and if the user wrote 'talk' he would say "Oh, so you want to talk ?!". But at that moment it stops, it simply does not appear anything after that, then if I write anything it goes to the last if and it ends.

#include <iostream>

using namespace std;



    //Variaveis
    char assunto[50] = "conversar";



    //Logica
    int main()
    {
    cout << "Ola, meu nome eh LOCAO, e estou aqui para te ajudar, o que voce deseja?" << endl ;
    cin >> assunto;
    if (assunto == "conversar")
        cout<<"Oh! Entao você quer conversar?! Sobre o que?"<< endl;

    if (cin >> assunto)
        cout << "Ah sim, isso eh muito legal" << endl;

    system("pause");
    return 0;
}
    
asked by anonymous 31.07.2015 / 00:22

1 answer

2

Since you're using C ++, why do not you use the string type in the default language library? So:

#include <iostream>
using namespace std;

int main() {
    cout << "Ola, meu nome eh LOCAO, e estou aqui para te ajudar, o que voce deseja?" << endl ;
    string assunto = "conversar";
    cin >> assunto;
    if (assunto == "conversar")
        cout << "Oh! Entao você quer conversar?! Sobre o que?" << endl;

    if (cin >> assunto)
        cout << "Ah sim, isso eh muito legal" << endl;

    return 0;
}

See running on ideone . I took advantage of and organized the code better.

Using the type string you can use the comparison operator. The way I was using it compared two references that obviously are different. He did not compare two texts. This only occurs with the overload of the == operator on type string .

Maybe it is not performing well as you want but there is a logic problem adopted and only you can say about it, at least there is no more programming error preventing the flow is correct. I would not use cin <<assunto within the condition of if , nor do I know what the purpose of this is probably misuse.

If you insist on using a array of char then the comparison has to be made with strcmp() ". The direct comparison will be comparing the pointers that point to the text and not the text itself.

    
31.07.2015 / 00:50