In: Computer Science
*C++*
/*
This program reads a movie rating from 1 to 10 from the
user and prints a corresponding word for the rating.
Programmer: Your name here
Date: Current date here
*/
#include
#include
using namespace std;
int main()
{
int rating; // user entered rating
cout << "Enter your movie rating (an integer from 1 to 10): ";
cin >> rating;
cout << "\nThe movie was ";
// Select the appropriate word for the number
switch (rating)
{
case 1 : cout << "Horrible (:<)!";
case 5 : cout << "So So!";
case 10: cout << "Fabulous!";
}
cout << "\n\n";
return 0;
}
Fix the program so that inputs of 1, 5 and 10 work properly.
2. Change the case values to characters, ‘1’, ‘5’ and ‘10’. What happens when the program is run and why?
Change the program back by removing the quotes.
1ans:
while enter 1 the 1 whole case statements executed because we can't use break statement.
if we didn't use break statement all case statements executed from given case.
#output:
#fix the problem code just add break statement after case ending
#code:
#include<iostream>
using namespace std;
int main()
{
int rating; // user entered rating
cout << "Enter your movie rating (an integer from 1 to 10): ";
cin >> rating;
cout << "\nThe movie was ";
// Select the appropriate word for the number
switch (rating)
{
case 1 :
cout <<
"Horrible (:<)!";
break;
case 5 : cout << "So So!";
break;
case 10: cout << "Fabulous!";
break;
}
cout << "\n\n";
return 0;
}
#add quotes code but we take rating is integer variable but when we add quotes to cases it can convert to characters
so the cases never be checked so we just get normal output without cases data
#code:
#include<iostream>
using namespace std;
int main()
{
int rating; // user entered rating
cout << "Enter your movie rating (an integer from 1 to 10): ";
cin >> rating;
cout << "\nThe movie was ";
// Select the appropriate word for the number
switch (rating)
{
case '1' :
cout <<
"Horrible (:<)!";
break;
case '5' : cout << "So So!";
break;
case '10': cout << "Fabulous!";
break;
}
cout << "\n\n";
return 0;
}
#output:
but the code will be executed properly by change int rating to char rating
code:
#include<iostream>
using namespace std;
int main()
{
char rating; // user entered rating
cout << "Enter your movie rating (an integer from 1 to 10): ";
cin >> rating;
cout << "\nThe movie was ";
// Select the appropriate word for the number
switch (rating)
{
case '1' :
cout <<
"Horrible (:<)!";
break;
case '5' : cout << "So So!";
break;
case '10': cout << "Fabulous!";
break;
}
cout << "\n\n";
return 0;
}
#output::
#only print so so meagse:
#include<iostream>
using namespace std;
int main()
{
int rating; // user entered rating
cout << "Enter your movie rating (an integer from 1 to 10): ";
cin >> rating;
cout << "\nThe movie was ";
// Select the appropriate word for the number
switch (rating)
{
case 1:
goto A;
cout <<
"Horrible (:<)!";
case 4 : A: cout << "So So!";
break;
case 10:
goto A;
cout <<
"Fabulous!";
}
cout << "\n\n";
return 0;
}
#add the more cases:
#include<iostream>
using namespace std;
int main()
{
int rating; // user entered rating
cout << "Enter your movie rating (an integer from 1 to 10): ";
cin >> rating;
cout << "\nThe movie was ";
// Select the appropriate word for the number
switch (rating)
{
case 1:
cout <<
"Horrible (:<)!";
break;
case 4 : cout << "So So!";
break;
case 10:
cout <<
"Fabulous!";
break;
case 2:
cout<<"Awesome";
break;
case 3:
cout<<"superb";
break;
case 7:
cout<<"good";
break;
case 8:
cout<<"dangerous";
break;
case 9:
cout<<"worst";
break;
}
cout << "\n\n";
return 0;
}
#enter 12 the output is:
#12 case is not define so not give any case statement
#check before switch error proper error
#include<iostream>
using namespace std;
int main()
{
int rating; // user entered rating
cout << "Enter your movie rating (an integer from 1 to 10): ";
cin >> rating;
cout << "\nThe movie was ";
// Select the appropriate word for the number
if(rating==1 || rating==4 || rating==10){
switch (rating)
{
case 1:
cout <<
"Horrible (:<)!";
break;
case 4 : cout << "So So!";
break;
case 10: cout << "Fabulous!";
break;
}
}
else{
cout<<"The movie was not rated properly!";
}
cout << "\n\n";
return 0;
}
#output:
#cout << "\nThe movie was ";
this statement already in switch statement so there is no change
#if you have any doubt comment below...