Question

In: Computer Science

*C++* /* This program reads a movie rating from 1 to 10 from the user and...

*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;

}

  1. Run your program and enter the number 1 for the movie rating. What does the program output and why?

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.

  1. Have the inputs 4, 5 and 6 all give the So So message. Do this without adding another cout statement!
  2. Add cases for the other values, 2, 3, 7, 8 and 9. Use your own ratings for these. Test your program.
  3. . Run your program with an input of 12. What happens and why?
  4. Add a case that catches all illegal values and prints an appropriate message. It should print: The movie was not rated properly!
  5. The statement:     cout << "\nThe movie was ";
  6. was placed before the switch statement. For what cases does this statement execute?
  7. How does this placement of the statement save you typing time?

Solutions

Expert Solution

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...


Related Solutions

Write a C++ program that reads numbers from the user until the user enters a Sentinel....
Write a C++ program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following: Output the sum of all even numbers Output the sum of all odd numbers Output the count of all even numbers Output the count of all odd numbers You must use loops and numbers to do this. You must not use any arrays or vectors for this program.
Design and implement a program that reads a series of 10 integers from the user and...
Design and implement a program that reads a series of 10 integers from the user and prints their average. Read each input value as a string, and then attempt to convert it to an integer using the Integer.parseInt method. If this process throws a NumberFormatException (meaning that the input is not a valid number), print an appropriate error message and prompt for the number again. Continue reading values until 10 valid integers have been entered.
IN C++ Write a program that reads in int values from the user until they enter...
IN C++ Write a program that reads in int values from the user until they enter a negative number like -1. Once the user has finished entering numbers, print out the highest value they’ve entered, the lowest value they’ve entered, and the total number of numbers they’ve entered. The negative number they entered should not be taken as one of the values entered.
Write a C++ or Java program that reads an input graph data from a user. Then,...
Write a C++ or Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number ofvertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2...
1. Design and implement a program that reads a series of integers from the user and...
1. Design and implement a program that reads a series of integers from the user and continually prints their average after every reading. The input stops when the user enters “stop” as the input value. Read each input value as a string, then check if it is “stop” or not. If the string is not “stop”, then, attempt to convert it to an integer using the Integer.parseInt method. If this process throws a NumberFormatException, meaning that the input is not...
Using C++ Write a program that reads a text from the keyboard until the user presses...
Using C++ Write a program that reads a text from the keyboard until the user presses the “Enter” key. Your program must count the number of uppercase alphabets (A through Z), lowercase alphabets (a through z), digits (0 through 9) and any other characters. The other character count value should NOT include the “Enter” key. Display the count on the screen. You must use the in-built C++ character I/O functions in this program.
In this assignment, you will be given a functioning program, called minor3.c, that simply reads user...
In this assignment, you will be given a functioning program, called minor3.c, that simply reads user input keys and echoes them back to the screen using the producer-consumer paradigm. The single producer thread reads user input keys and adds them to the shared buffer while two consumer threads read the added keys from the buffer and echo them back to the screen. To complicate matters, each key is read and echoed by exactly one consumer thread. A shared variable, called...
Write a Java program that reads an input graph data from a user. Then, it should...
Write a Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number of vertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2 0...
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
In Python b) Modify your program that reads 3 grades from the user (and computes the...
In Python b) Modify your program that reads 3 grades from the user (and computes the average and letter grade) so that it uses a while loop to read the 3 grades. In the loop body, you will just read one grade and update other variables appropriately. The loop header will ensure 3 iterations. c) Modify your program in part b so that it asks the user how many grades there are and uses a while loop to read that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT