c++ code using code blocks, please make sure it compiles. copy and paste with an
ID: 3925947 • Letter: C
Question
c++ code using code blocks, please make sure it compiles. copy and paste with answers
This program is for practicing function calling and writing
When you see //---- that is where you need to add code
#include
#include
//This was used to get everything from keyboard as a string first, then
//convert them into the corresponding data type (int, double, etc) accordingly
//It is used to avoid the cin problem it may cause
#include
using namespace std;
//********** Function declaration *************************
//function #1: will simply display a menu on screen
//Note: for function declaration, all header must end with ;
void displayMenu();
//function #2: will take a user's name as input parameter and display, for example,
//'Welcome, John Smith.' on screen if user's name is John Smith
void welcomeMsg(string userName);
//function #3: takes an integer as input and return true if it's even number, otherwise
//return false
bool checkEven(int number);
//function #4: declare a function called 'computeAvg' here, it takes two double values
//as input parameter and will return the average of the two numbers
//----
//************ Function: main() ******************************
int main()
{
int choice; // Holds the user's menu choice
string input = "";
do
{
//call function #1 to display the menu on screen
//----
cout << "Enter your choice (1-4): ";
getline(cin, input);
stringstream inputStream(input);
inputStream >> choice;
//Choice validation while loop
while (choice < 1 || choice > 4)
{
cout << "The only valid choices are 1-4. Please re-enter: ";
cin >> choice;
}
if (choice != 4) //Choice 4 is to quit. If user does not want to quit, proceed accordingly
{
switch (choice)
{
case 1: //is for function #2 - welcomeMsg(...)
{
string name;
cout << "What's your name: ";
getline(cin, name);
//call function #2 to display 'Welcome, John Smith.' on screen
//if user's name is John Smith
//----
}
break;
case 2: //is for function #3 - checkEven(...)
{
int num;
cout << "Enter an integer and I will tell you whether it's even or not: ";
getline(cin, input);
stringstream inputStream(input);
inputStream >> num;
//call function #3, if the number user entered is even, display a message
//'The number is even' on screen, otherwise display 'The number is odd'
if (//----)
cout << "The number is even" << endl;
else
//----
}
break;
case 3: //is for function #4
{
double num1, num2;
cout << "Enter two numbers and I will compute their average " << endl;
getline(cin, input);
stringstream inputStream(input);
inputStream >> num1 >> num2;
//Declare a double variable called avg. Call function #4 by using
//num1, num2 as input arguments, save the result inside avg
//----
//----
cout << fixed << showpoint << setprecision(2);
cout << " The two number's average is: " << avg << endl;
}
break;
}
}
} while (choice != 4);
return 0;
}
//************ Function #1: displayMenu() ********************
void displayMenu()
{
cout << "************************************ ";
cout << " ASU CSE100 Lab #7 - Functions ";
cout << "1. Display Welcome Message ";
cout << "2. Check Even or Odd Integer ";
cout << "3. Compute Two Number's Average ";
cout << "4. Quit the Program ";
}
//************ Function #2: welcomeMsg() ****************************
//will take a user's name as input parameter and display, for example,
//'Welcome, John Smith.' on screen if user's name is John Smith
//Note: the header of the function does not end with ;
void welcomeMsg(string userName)
{
//----
}
//************ Function #3: checkEven() ***********************************
//function #3: takes an integer as input and return true if it's even number
//otherwise it returns false
//Note: the header of the function does not end with ;
bool checkEven(int number)
{
//----
//----
}
//************ Function #4: computeAvg() ***********************************
//function #4: declare a function called 'computeAvg' here, it takes two double
//values as input parameter and will return the average of the two numbers
//method's header
//----
{
//----
}
Explanation / Answer
#include<iostream.h>
#include<string.h>
#include<sstream.h>
using namespace std;
void displayMenu();
void welcomeMsg(string userName);
bool checkEven(int number);
double computeAvg(double,double);
int main()
{
int choice; // Holds the user's menu choice
string input = "";
do
{
displayMenu(); // display the menu on screen
cout << "Enter your choice (1-4): ";
getline(cin, input);
stringstream inputStream(input);
inputStream >> choice;
//Choice validation while loop
while (choice < 1 || choice > 4)
{
cout << "The only valid choices are 1-4. Please re-enter: ";
cin >> choice;
}
if (choice != 4) //Choice 4 is to quit. If user does not want to quit, proceed accordingly
{
switch (choice)
{
case 1:
string name;
cout << "What's your name: ";
getline(cin, name);
welcomeMsg(name);
break;
case 2:
int num;
cout << "Enter an integer and I will tell you whether it's even or not: ";
getline(cin, input);
stringstream inputStream(input);
inputStream>>choice;
if (checkEven(num)==true)
cout << "The number is even" << endl;
else
cout<<"The number is odd<<endl;
break;
case 3:
double num1, num2;
cout << "Enter two numbers and I will compute their average " << endl;
getline(cin, input);
stringstream inputStream(input);
inputStream >> num1 >> num2;
double avg;
avg=computeAvg(num1,num2);
cout << fixed << showpoint << setprecision(2);
cout << " The two number's average is: " << avg <<endl;
break;
}
}
} while (choice != 4);
return 0;
}
void displayMenu()
{
cout << "************************************ ";
cout << " ASU CSE100 Lab #7 - Functions ";
cout << "1. Display Welcome Message ";
cout << "2. Check Even or Odd Integer ";
cout << "3. Compute Two Number's Average ";
cout << "4. Quit the Program ";
}
void welcomeMsg(string userName)
{
cout<<"Welcome, "<<+username<<endl;
}
bool checkEven(int number)
{
if(number2== 0)
return true;
else
return false;
}
double computeAvg(double n1,double n2)
{
return((n1+n2)/2);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.