the price. The user should input an integer in the range of the choices you give them (for example, a user cannot input 3 if you only have 2 choices) and in response should be prompted to for the next set of choices. Once they make the final selection, the total should be printed to them as an integer with proper formatting as shown in the sample run. Sample run: Select a category: (1) Brass (2) Woodwind (3) Percussion 2 Select an instrument: (1) Flute (2) Saxophone 2 Your instrument will be $425. Ensure you are doing input validation, output Please enter a valid input. if the user inputs something they cannot and terminate the program. Make sure to submit your code on Coderunner (accessed through Canvas) as well as the final Zip file submission.
You must use switch statements instead of if-else statements. If-else statements will not be accepted for this problem.
You want to learn to play an instrument, but you need to know how much it's going to cost to buy it. The music store has the following table on their website:
Write a menu-driven program that asks the user to input an instrument category and then an instrument. The program should give the user the price.
The user should input an integer in the range of the choices you give them (for example, a user cannot input 3 if you only have 2 choices) and in response should be prompted to for the next set of choices. Once they make the final selection, the total should be printed to them as an integer with proper formatting as shown in the sample run.
Sample run:
Select a category: (1)Brass (2)Woodwind (3)Percussion 2 Select an instrument: (1)Flute (2)Saxophone 2 Your instrument will be $425.
Ensure you are doing input validation, output Please enter a valid input. if the user inputs something they cannot and terminate the program.
Make sure to submit your code on Coderunner (accessed through Canvas) as well as the final Zip file submission.
The file should be named as instrument.cpp.
Code:
#include <iostream> //including headers
#include <string>
using namespace std;
int main() {
//Manu of first choice
printf("Select a category: (1)Brass (2)Woodwind (3)Percussion\n");
int choice1; //variable for storing choice
int choice2;
scanf("%d", & choice1); //taking user input
switch (choice1) { //switch statement over choice1
case 1: //case for Brass
printf("\nSelect an instrument: (1)Trumpet (2)Trombone\n");
scanf("%d", & choice2); //nested choice2
switch (choice2) { //switch statement over choice2
case 1:
printf("Your instrument will be $350"); //printing price as per both choice
break;
case 2:
printf("Your instrument will be $400");
break;
default:
printf("Wrong Input"); //if user input other than that it shows this error
return 0;
}
break;
case 2: //case for Woodwind
printf("\nSelect an instrument: (1)Flute (2)Saxophone\n");
scanf("%d", & choice2);
switch (choice2) {
case 1:
printf("Your instrument will be $325");
break;
case 2:
printf("Your instrument will be $425");
break;
default:
printf("Wrong Input");
return 0;
}
break;
case 3: //case for Percussion
printf("\nSelect an instrument: (1)Snare Drum (2)Cymbals\n");
scanf("%d", & choice2);
switch (choice2) {
case 1:
printf("Your instrument will be $275");
break;
case 2:
printf("Your instrument will be $200");
break;
default:
printf("Wrong Input");
return 0;
}
break;
default:
printf("Wrong Input");
return 0;
}
}
Step by step
Solved in 2 steps with 1 images