151k views
1 vote
Write the definition of a method printAttitude, which has an int parameter and returns nothing. The method prints a message to standard output depending on the value of its parameter. If the parameter equals 1, the method prints disagree. If the parameter equals 2, the method prints no opinion. If the parameter equals 3, the method prints agree. In the case of other values, the method does nothing. Each message is printed on a line by itself.

User Macron
by
5.3k points

1 Answer

2 votes

Answer:

We can use nested else if.

Step-by-step explanation:

Nested else if are used when we have multiple checks on single variable and at the end if no check is passed then we execute the else part of nested if.

Code Example

#include<iostream> //for input and output

using namespace std;

void printAttitude(int value){

if(value == 1) {

cout<<"disagree";

}

else if( value ==2)

{

cout<<"no opinion";

}

else if( value ==3) {

cout<<"agree";

} else {

// method do nothing

}

}

int main()

{

int option;

cout<<"Enter your value :";

cin>>option;

printAttitude(option);

return 0;

}

Output

Case 1:

Enter your value :1

disagree

Case 2:

Enter your value :2

no option

Case 3:

Enter your value :3

agree

Case 4:

Enter your value :1

User Parachute
by
5.5k points