Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write a switch statement with strings in C++? [duplicate]

Tags:

c++

I'm working on a small project on Dev-C++. I'm trying to make a bot to ask you some questions, but I can't use the switch statetments with the strings. Every time I try to do so it shows error! I also tried to change the srings to normal int variables but when I the code runs all at once after answering the first question! Does anyone knows how to fix any of these situations?

Here is my code:

// #include "stdafx";

#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>

using namespace std;

int main()
{
    string comida;
    string nome;
    string idade;
    string pais;

    cout << "Ola, o meu nome e Aleksandar. Qual e o teu nome?" << endl; //Ask for nome
    cin >> nome; //Recieve variable nome

    cout << "Es de que pais, " << nome << "?" << endl; //Ask for pais
    cin >> pais; //Receive pais
    cout << pais << " e um pais bonito. " << "Eu sou de Portugal!" << endl; 

    cout << "Quantos anos tens " << nome << "?" << endl; //Ask for idade
    cin >> idade; //Receive variable idade

    switch (idade) {
       case 21:
          cout << "O meu irmao tambem tem 21 anos!" << endl;
          break;
    }
    cout << "Eu tenho 28" << endl;

    cout << "Qual e a tua comida preferida?" << endl; //Ask for comida
    cin >> comida; //Receive variable comida

    cout << "Tambem gosto muito de " << comida << ". Mas gosto mesmo e de Vatruchka!" << endl;
    cout << "Xau " << nome << "!" << endl;
}
like image 664
Mário Leitão-Teixeira Avatar asked Oct 31 '25 13:10

Mário Leitão-Teixeira


1 Answers

A switch will not compile when non-intergal (i.e. string, float, bool, vector, etc...) data-types are assigned to the switch's case: statements. Furthermore, its necessary that the values assigned to the case: statements are const.

In other words:

Switches must use "constant integral data-types" (i.e. 'enum', 'int', 'char', etc...), and cannot implament strings as conditional statements, however; that is not the same as saying that strings cannot be used in a conditional statement, they very much can be & often are — see the example below:


    std::string s;
    std::cin >> s;

    if (s == "Yes")
    {
       std::cout << "You said yes!" << std::endl;
    } 
    else if (s == "No")
    {
       std::cout << "You said no?"  << std::endl;
    }
    else 
    {
       std::cout << "You said something I don't understand"  << std::endl;
    }
So to finish this answer, you can see that you can achieve the same thing you could with a switch statement using if/else blocks. It may, or it may not, be ideal for your situation, but this is how C++, and switch statements work, so your stuck with it — like it, or not...
like image 143
Mats Petersson Avatar answered Nov 03 '25 04:11

Mats Petersson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!