C++ Fill in the missing code: #include<iostream> #include<string> using namespac
ID: 3807355 • Letter: C
Question
C++ Fill in the missing code:
#include<iostream>
#include<string>
using namespace std;
//void function that accepts one reference parameter: string
//function prompts the user for his/her name
void GetName(string& name);
//function that accepts three value parameter: int, int, int
//function returns true if the value n is between min and max, otherwise returns //false
bool IsValidInput(int n, int min, int max);
//void function accepts one reference parameter: int
//function displays the following menu and prompts the user for choice
//1) Enter your name
//2) Determine if a number is odd
//3) Square a number
//4) Exit
void DisplayMenu(int &choice);
//function that accepts one value parameter: int
//function returns true if n is odd, otherwise returns false
bool IsOdd(int n);
//void function that accepts one reference parameter: float
//function squares the value n
void SquareN(float &n);
int main()
{ int menuChoice = 0;
string userName = " ";
int num1 = 0;
float num2 = 0.0;
do
{
//call a function to display a menu (1 point)
//call a function to validate that menuChoice is between 1 and 4 (1 pt)
switch(menuChoice)
{
case 1:
//call a function to get username (1 point)
//display userName
break;
case 2:
//prompt user for num1
cout<<"Enter an integer: ";
cin>>num1;
//call a function to display whether or not num is odd ( 1 point)
break;
case 3:
//prompt for num2
cout<<"Enter a number: ";
cin>>num2;
//call a function to square num2 (1 point)
//display num2 squared
break;
case 4:
cout<<"Good bye"<<endl;
} //end switch
}
while(menuChoice!=4);
return 0;
}
//function definitions (4 pts each)
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
//void function that accepts one reference parameter: string
//function prompts the user for his/her name
void GetName(string& name)
{
cout<<"Please enter your name :";
cin>>name;
}
//function that accepts three value parameter: int, int, int
//function returns true if the value n is between min and max, otherwise returns //false
bool IsValidInput(int n, int min, int max)
{
if(n>=min&&n<=max)
return true;
return false;
}
//void function accepts one reference parameter: int
//function displays the following menu and prompts the user for choice
//1) Enter your name
//2) Determine if a number is odd
//3) Square a number
//4) Exit
void DisplayMenu(int &choice)
{
cout<<" ========MENU========= 1) Enter your name 2) Determine if a number is odd 3) Square a number 4) Exit Enter your choice: ";
cin>>choice;
}
//function that accepts one value parameter: int
//function returns true if n is odd, otherwise returns false
bool IsOdd(int n)
{
if(n%2==1)
return true;
return false;
}
//void function that accepts one reference parameter: float
//function squares the value n
void SquareN(float &n)
{
n=n*n;
}
int main()
{
int menuChoice = 0;
string userName = " ";
int num1 = 0;
float num2 = 0.0;
bool check;
do
{
//call a function to display a menu (1 point)
DisplayMenu(menuChoice);
//call a function to validate that menuChoice is between 1 and 4 (1 pt)
check=IsValidInput(menuChoice, 1, 4);
if(check==true)
{
cout<<"Input is valid"<<endl;
switch(menuChoice)
{
case 1:
GetName(userName); //call a function to get username (1 point)
cout<<"Your Name is "<<userName<<endl; //display userName
break;
case 2:
//prompt user for num1
cout<<"Enter an integer: ";
cin>>num1;
check=IsOdd(num1); //call a function to display whether or not num is odd ( 1 point)
if(check==true)
cout<<"Yes, Entered number is odd"<<endl;
else
cout<<"No, Entered number is even"<<endl;
break;
case 3:
//prompt for num2
cout<<"Enter a number: ";
cin>>num2;
SquareN(num2); //call a function to square num2 (1 point)
cout<<"The squared number is "<<num2<<endl; //display num2 squared
break;
case 4:
cout<<"Good bye"<<endl;
} //end switch
}
}while(menuChoice!=4);
return 0;
}
//function definitions (4 pts each)
Output:-
========MENU=========
1) Enter your name
2) Determine if a number is odd
3) Square a number
4) Exit
Enter your choice: 1
Input is valid
Please enter your name :phani
Your Name is phani
========MENU=========
1) Enter your name
2) Determine if a number is odd
3) Square a number
4) Exit
Enter your choice: 2
Input is valid
Enter an integer: 5
Yes, Entered number is odd
========MENU=========
1) Enter your name
2) Determine if a number is odd
3) Square a number
4) Exit
Enter your choice: 2
Input is valid
Enter an integer: 4
No, Entered number is even
========MENU=========
1) Enter your name
2) Determine if a number is odd
3) Square a number
4) Exit
Enter your choice: 3
Input is valid
Enter a number: 5.2
The squared number is 27.04
========MENU=========
1) Enter your name
2) Determine if a number is odd
3) Square a number
4) Exit
Enter your choice: 4
Input is valid
Good bye
Process exited normally.
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.