0

BASIC C++ Switch

How make this in the correct way ? void intro_duels(int x) { switch(x) { case 1-10:cout<< "text"<<endl;break; case 11-20:cout<< "text?"<<endl;break; case 21-49:cout<< " text"<<endl; break; case 50-99: cout<< "text" << endl; break; case 100: cout<<"text"<<endl; break; return ; } } Also how return that "text" and no int to the main function ????

14th Feb 2017, 9:35 PM
Yuriy az
Yuriy az - avatar
2 Respuestas
+ 8
switch cannot check ranges. Use if/else for these. Ex: if (x >= 1&& x <= 10) cout << "text" << endl; else if (x >=11 && x <= 20) //follow this pattern To make intro_duels() return the text instead of print it, write the top like this: string intro_duels(int x) And make sure #include <string> //Allows you to use string datatype is at the top of your code. Replace your cout statements with return "text"; To call it, write something like this: string result = intro_duels(num); //Store it cout << intro_duels(num); //Print it immediately
14th Feb 2017, 10:05 PM
Tamra
Tamra - avatar
+ 1
you better use the if statment for this
14th Feb 2017, 9:47 PM
salman rahmani
salman rahmani - avatar