A. Assume time is entered on the hour. Assume that time is already declared. Cod
ID: 3599252 • Letter: A
Question
A. Assume time is entered on the hour. Assume that time is already declared. Code a fall-through switch statement that will print the following messages:
When the value in time is 900: “Take an extra 20% off!”
When the value in time is 1000: “Take an extra 20% off!”
When the value in time is 1100: “Take an extra 20% off!”
When the value in time is 1200: “Take an extra 20% off!”
When the value in time is any other value: “Oops! You missed the morning sale!”
B. Code a for loop with the switch structure from A. and allow 3 attempts. Prompt for the user to enter an on-the-hour military time. When the time entered is in the correct range for the morning sale make sure you exit the loop (not the program) after the message is printed. Print the message “No more attempts left!” if it is the last attempt for an invalid entry.
Explanation / Answer
Question A
Answer:
switch(time) {
case 1000:
case 1100:
case 1200:
case 900: cout<<“Take an extra 20% off!”;<<endl; break;
default: cout<<"Oops! You missed the morning sale!"<<endl;
}
Question B
Answer:
for(int i=0;i<3;i++) {
switch(time) {
case 1000:
case 1100:
case 1200:
case 900: cout<<“Take an extra 20% off!”;<<endl; break;
default: cout<<"Oops! You missed the morning sale!"<<endl; break;
}
}
cout<<"No more attempts left!"<<endl;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.