+ 1

What is the error of this code?

#include <iostream> using namespace std; int main() { int age=42 ; switch (age) { case:16 cout << "so yung"; break ; case:42 cout << "Adult"; break; case:70; cout << "oldman"; break ; } return 0; }

10th Mar 2022, 12:38 PM
Mhdis
Mhdis - avatar
9 Respuestas
+ 2
Mhi tj the switch/case statement only checks for specific ages. It does not check for age ranges between the given numbers. To check for age ranges instead, use if/else. if (age<=16) cout << "so young"; else if (age<=42) cout << "Adult"; else if (age<=70) cout << "Middle-aged"; else cout << "Old man";
10th Mar 2022, 1:59 PM
Brian
Brian - avatar
+ 2
Semicolon error in case 3rd
11th Mar 2022, 7:33 PM
Himanshu
Himanshu - avatar
+ 1
Tnx🥺🥲
10th Mar 2022, 12:49 PM
Mhdis
Mhdis - avatar
+ 1
Brian I just wanted to learn how to write code with a switch. Please tell me the uses of the switch, thank you
10th Mar 2022, 7:18 PM
Mhdis
Mhdis - avatar
+ 1
Ok tnx FF990🙏🙏
11th Mar 2022, 8:55 AM
Mhdis
Mhdis - avatar
+ 1
switch (itemState) { case "working" : readyUnits++; break; case "uncalibrated" : item.calibrateFunction(); break; case "broken" : removeFromInventory(); case "damaged" : if (canBeRepaired) { repairWorkshop.add(item); } else { removeFromInventory(); } stripForParts(); // strips broken and !canBeRepaired
11th Mar 2022, 9:16 PM
Philipp Eamon
+ 1
int initialAggression = 2; int rudimentaryAI = initialAggression + currentAggression + diceRoll(numberOfDice: 1); void makeAImoreAggressive() { currentAggression++; } void makeAImoreAfraid() { initalAggression--; } switch (rudimentaryAI) { case 0 : case 1 : case 2 : case 3 : case 4 : case 5 : case 6 : computerPlayer.Retreat(); break; case 7 : case 8 : case 9 : case 10 : case 11 : computerPlayer.Advance(); break; default : computerPlayer.Fire(); // anything higher than 11 or lower than 0
11th Mar 2022, 10:09 PM
Philipp Eamon