Answer:
The C++ function definition for printAttitude can be seen below.
void printAttitude(int n)
{
switch(n)
{
case 1: cout<<"disagree"<<endl; break;
case 2: cout<<"no opinion"<<endl; break;
case 3: cout<<"agree"<<endl; break;
default: break;
}
}
Explanation:
It is notable that this method takes an int parameter.
Since it yields no value, it has a void return type.
The console displays messages based on the integer parameter, n, using a switch statement.
Output conditions are specified in different cases.
The default case takes care of values where no output is needed.
Each case concludes with a break statement to end the execution of the program.
This function can be utilized within a program as illustrated.
PROGRAM
#include <iostream>
using namespace std;
void printAttitude(int n)
{
switch(n)
{
case 1: cout<<"disagree"<<endl; break;
case 2: cout<<"no opinion"<<endl; break;
case 3: cout<<"agree"<<endl; break;
default: break;
}
}
int main() {
// variables to hold respective value
int n;
// user input taken for n
cout<<"Enter any number: ";
cin>>n;
// calling the function taking integer parameter
printAttitude(n);
return 0;
}
OUTPUT
Enter any number: 11
1. The user input is taken for the integer parameter.
2. Inside main(), the method, printAttitude(), is called with the user-input as a parameter.
3. If a user enters 11, which does not match any of the case values, the default case is executed, which does nothing and the break statement runs.
4. Thus, nothing is displayed for the input of 11.
5. When the user inputs 3, the corresponding case block is executed since it matches the case values.
6. The program produces the following output when the user inputs 3.
OUTPUT
Enter any number: 3
agree
The program concludes with a return statement.