Jump to content
Fórum Script Brasil
  • 0

duvidas com switch


Question

toda fez que executo este  programa ele so imprimi a pergunta eu respondo e ele fecha sem a resposta

#include <iostream>

using namespace std;

int main()
{
    cout << "ola tudo bem? " << endl;
    int a = 0;
    cin >> a;

    string b = ("tudo"),
           c = ("não");
    switch(a){
case 'b':
    cout << "que bom" << endl;
    break;
case 'c':
    cout << "ah, me desculpe" << endl;
    }

    return 0;
}

 

Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Não é possivel usar switch para comparar strings, somente chars, ints etc, ou seja tipos básicos. Para fazer o que você quer use if.
 

#include <iostream>

using namespace std;

int main() {
    cout << "ola tudo bem? " << endl;
    string a = "não",
           b = "tudo",
           c = "não";

    if(a == b) {
        cout << "que bom" << endl;
    } else if(a == c) {
        cout << "ah, me desculpe" << endl;
    }

    return 0;
}

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Cloud Computing


  • Forum Statistics

    • Total Topics
      148895
    • Total Posts
      644923
×
×
  • Create New...