Program does not execute expected flow

Asked

Viewed 77 times

1

After the "Hello, my name eh Locao......" for me, in logic, I should continue, and if the user wrote 'talk' he would say "Oh, so Voce wants to talk?!". But at that moment it stops, just doesn’t appear anything after that, then if I write anything it goes to the last if and 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;
}
  • I created another project, and now mentions unspecified path! Before it didn’t work when it gave F5, now neither will this

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

1 answer

3

Since you are using C++, why not use the type string existing in the default language library? Like this:

#include <iostream>
#include <string>
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;
}

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

I took advantage and organized the code better.

Using the type string you can use the comparison operator. The way you were using compared two references which are obviously different. I did not compare two texts. This only occurs with the operator overload == in kind string.

Maybe it is not running as well as you want but then it is a problem of the adopted logic and only you can say about it, at least there is no longer a programming error preventing the flow is correct. I wouldn’t use the cin <<assunto within the condition of if, I don’t even know what the point of this is, it’s probably mistaken use.

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

Browser other questions tagged

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