jeudi 11 février 2021

switch statement outputting default statement even when not needed

When I run my code it will give the output to one of my cases, but will then also output the default statement even though it is not needed. I have no idea how to fix this. I have tried putting the default statement only in the last switch statement which makes it only output the default one time instead of multiple times, however it will still output the default statement once even when it doesn't need to. #include using namespace std;

int main() {
int x , b , t , s , p;
cout << "Enter Type of Sport: 1.Biking 2.Camping 3.Hiking 4.Rock Climbing" << endl;
cin >> x;
switch (x) {
case 1:
cout << "Choose type of Equipment : 1.Mountain Bike 2.Road Bike" << endl;
cin >> b;
break;

case 2: 
cout << "Choose type of Equipment : 1.2-Person Tent 2.4-Person Tent" << endl; 
cin >> t;
break;

case 3:
cout << "Choose type of Equipment : 1.Summer Boots 2.Winter Boots" << endl;
cin >> s;
break;

case 4:
cout << "Choose type of Equipment : 1.Bouldering Pad 2.Top Rope Pad" << endl;
cin >> p;
break;

default:
cout << "Invalid Input." << endl;
break;

}

switch(b) {
case 1: {
cout << "Price of Mountain Bike is $500." << endl;
break;
}
case 2: 
cout << "Price of Road Bike is $400." <<endl;
break;

default:
cout << "Invalid Input." << endl;
break;

}

switch(t) {
case 1: 
cout << "Price of 2-Person Tent is $200." << endl;
break;

case 2: 
cout << "Price of 4-Person Tent is $300." <<endl;
break;

default:
cout << "Invalid Input." << endl;
break;

}

switch(s) {
case 1:
cout << "Price of Summer Boots is $100." << endl;
break;

case 2: 
cout << "Price of Winter Boots is $150." <<endl;
break;

default:
cout << "Invalid Input." << endl;
break;

}

switch(p) {
case 1:
cout << "Price of Bouldering Pad is $100." << endl;
break;

case 2: 
cout << "Price of Top Rope Pad is $60." << endl;
break;

default:
cout << "Invalid Input." << endl;
break;

}

return 0;
}

Aucun commentaire:

Enregistrer un commentaire