samedi 28 février 2015

How can I write a switch statement with strings in 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 " << idade << "!" << 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;

return 0;


}



Aucun commentaire:

Enregistrer un commentaire