In: Computer Science
I am having a lot of difficulty on problem 2 I dont know how to use switch.
Branching and looping
Objectives:
1. Write a program that uses branching and a while loop. 2. Using boolean expressions and primitive variable types.
Note: if you don’t have a laptop, work with another person. When you are done with the exercise, show your TA that you both worked on the assignment together and you will both get credit for the work.
Problem 1:
Name your program Lastname_firstname_Recitation3_1.cpp.
For this recitation exercise, you are going to write a program
to prompt the user for the height of a building in feet (ft).
Then, use the height to print out the name of the building and its
height ranking using the following information:
Rank Height (in ft)
1 714
2 709
3 698
Building name Republic Plaza
1801 California Street Wells Fargo Center
The program should check the input with the information provided in the table above and print the name and ranking accordingly. If the input does not match any of the above mentioned heights then print out “Height does not match that of the three tallest buildings in Denver. Please enter a correct height. Would you like to try again? Enter Y/N”. If the user enters Y (yes), then prompt the user for the height of a building in feet (ft). If the user enters N (no), then exit out of the program.(Use a While loop)
Output:
714 ft is the height of Republic Plaza. It is the tallest building
in Denver.
Or,
709 ft is the height of 1801 California Street. It is the second
tallest building in Denver.
Or,
698 ft is the height of Wells Fargo Center. It is the third tallest
building in Denver.
Or,
Height does not match that of the three tallest buildings in
Denver. Please enter a correct
height. Would you like to try again? Enter Y/N.
Problem 2:
Name your program Lastname_firstname_Recitation3_2.cpp.
So you would have used the if and else statements in Problem 1 to achieve the desired output. For Problem 2 you need to make a small change which shows the implementation of switch statement to achieve the same output as in problem 1.
switch( controlling expression ) {
case Constant_1: Statement_Sequence_1;
break; case Constant_2:
Statement_Sequence_2;
break; ...
case Constant_n: Statement_Sequence_n;
break; default:
Default_Statement_Sequence;
}
To get credit for this lab exercise:
▪ Please zip both the files and submit your Lastname_firstname_Recitation3.zip file to Moodle under the ‘Recitation 3 Submit’ link.
▪ Show the TA your code and run your program before leaving the classroom.
// C++ code
#include <iostream>
using namespace std;
int main( )
{
int height;
char again;
// WHile loop
// PROGRAM 1
while(true)
{
cout << "\nEnter
Height: ";
cin >> height;
if(height ==
714)
cout << "714 ft is the height of Republic Plaza. It is the
tallest building in Denver.\n\n";
else if(height ==
709)
cout << "709 ft is the height of 1801 California Street. It
is the second tallest building in Denver.\n\n";
else if(height ==
698)
cout << "698 ft is
the height of Wells Fargo Center. It is the third tallest building
in Denver.\n\n" ;
else
cout << "Height
does not match that of the three tallest buildings in
Denver.\nPlease enter a correct height.\n\n";
cout << "Would
you like to try again? Enter (Y/N): ";
cin >> again;
if(again == 'N' ||
again == 'n')
break;
}
// WHile loop
// PROGRAM 2
while(true)
{
cout << "\nEnter
Height: ";
cin >>
height;
switch
(height)
{
case 714:
cout << "714 ft is the height of Republic Plaza. It is the
tallest building in Denver.\n\n";
break;
case 709:
cout << "709 ft is the height of 1801 California Street. It
is the second tallest building in Denver.\n\n";
break;
case 698:
cout << "698 ft is the height of Wells Fargo Center. It is
the third tallest building in Denver.\n\n" ;
break;
default:
cout << "Height does not match that of the three tallest
buildings in Denver.\nPlease enter a correct height.\n\n";
break;
}
cout <<
"Would you like to try again? Enter (Y/N): ";
cin >>
again;
if(again ==
'N' || again == 'n')
break;
}
return 0;
}
/*
output:
Enter Height: 768
Height does not match that of the three tallest buildings in
Denver.
Please enter a correct height.
Would you like to try again? Enter (Y/N): y
Enter Height: 709
709 ft is the height of 1801 California Street. It is the second
tallest building in Denver.
Would you like to try again? Enter (Y/N): y
Enter Height: 714
714 ft is the height of Republic Plaza. It is the tallest building
in Denver.
Would you like to try again? Enter (Y/N): y
Enter Height: 698
698 ft is the height of Wells Fargo Center. It is the third tallest
building in Denver.
Would you like to try again? Enter (Y/N): n
*/