Question

In: Computer Science

Written in C++. I need to create function printShort exactly like this description: "printShort ( )...

Written in C++. I need to create function printShort exactly like this description: "printShort ( ) - prints the Date in the format m/d/yy (no leading zeros for month and day, and two last digits for year with leading zeros (use fill digit ‘0’) if necessary). Use the accessors to get the values of the data members. " Right now it is showing the correct output for month and day, but still printing a 4 digit year. How would I only print the last two digits of the variable year? For example, with March 31, 2020 the year's value is set to '2020', but it should be written 3/31/20

Function code:

void Date::printShort(){

    cout<<getMonth()<<"/";
    cout<<getDay()<<"/";
    cout<<setfill('0')<<setw(2)<<getYear();

}//end printShort

Solutions

Expert Solution

For this, I suggest you to use %, i.e., modulo operator, which is an arithmatic operator in C++.

The modulo division operator produces the remainder of an integer division.

Syntax: If x and y are integers, then the expression:

x % y

produces the remainder when x is divided by y.

Return Value:

  • If y completely divides x, the result of the expression is 0.
  • If x is not completely divisible by y, then the result will be the remainder in the range [1, x-1].
  • If x is 0, then division by zero is a compile-time error.

Example:- (i) 2020 % 100 will give us 20 as result, i.e., the last two digits of the number

                  (ii) 252 % 10 will give us 2 as result, i.e., the last digit of the number.

Modified function definition:-

void Date::printShort(){

    cout<<getMonth()<<"/";
    cout<<getDay()<<"/";
    cout<<setfill('0')<<setw(2)<<(getYear()%100);

}//end printShort

Explanation:- We are using modulo operator for the value returned by getYear() method divided by 100. This will give us the last two digits of the year.

I have also test this on some given input-output, assuming function definitions on my own.

You can check my assumed code below here:-

Output for the same is:-

Thank you.

Happy Learning...


Related Solutions

I need a randomized quicksort function written in c++ or java with dual pivots and a...
I need a randomized quicksort function written in c++ or java with dual pivots and a partition function
(C++) I need to Create a Copy function of a Binary Search Tree recursively providing these...
(C++) I need to Create a Copy function of a Binary Search Tree recursively providing these structure emplate <typename T> class Tree {    struct TreeNode    {        T mData;        TreeNode* mLeft = nullptr;        TreeNode* mRight = nullptr;        TreeNode* mParent = nullptr;        bool mIsDead = false;        TreeNode()        {        }        TreeNode(T tData) : TreeNode()        {            mData = tData;...
I need this written in C # ASAP Write a C# console program that continually asks...
I need this written in C # ASAP Write a C# console program that continually asks the user "Do you want to enter a name (Y/N)? ". Use a "while" loop to accomplish this. As long as the user enters either an upper or lowercase 'Y', then prompt to the screen "Enter First and Last Name: " and then get keyboard input of the name. After entering the name, display the name to the screen.
Hello I need this written in C# with a few comments thanks Modify the Patient class...
Hello I need this written in C# with a few comments thanks Modify the Patient class to reference a demographic object that contains the patient phone number, email and next of kin. Create a demographic class to store this information. Make sure to create a constructor in the class for this information. Modify the Patient constructor to pass in this additional information at the time of instantiation. Modify the display method to display all the data HERE IS THE CODE...
I need the output of the code like this in java First we create a new...
I need the output of the code like this in java First we create a new building and display the result: This building has no apartments. Press enter to continue......................... Now we add some apartments to the building and display the result: This building has the following apartments: Unit 1 3 Bedroom Rent $450 per month Currently unavailable Unit 2 2 Bedroom Rent $400 per month Currently available Unit 3 4 Bedroom Rent $1000 per month Currently unavailable Unit 4...
I need to create a monthly loan Calculator in C++. I know the formula but can't...
I need to create a monthly loan Calculator in C++. I know the formula but can't figure out what's wrong with my code. Any clarification would be appreciated! // Only add code where indicated by the comments. // Do not modify any other code. #include <iostream> #include <cmath> using namespace std; int main() {    // ---------------- Add code here --------------------    // -- Declare necessary variables here              --        int years = 0; //n     int LoanAmount =...
I need to convert the following into C++. The general idea is to create an application...
I need to convert the following into C++. The general idea is to create an application that can support 10 ID's and 10 grades. It needs to calculate the grade average of said ID, and determine if it is an below or above a certain average, ergo A or C. string[10] studentIDArray int[10] gradeArray int averageGrade for(int i = 0; i < gradeArray; i++) { averageGrade += gradeArray[i] } averageGrade = averageGrade / sizeof(gradeArray) for(int i = 0; i <...
The equation for a simple consumption function is written as C = a + bY. The...
The equation for a simple consumption function is written as C = a + bY. The letter a represents the ________ part of consumption. The letters bY represent the ________ part of consumption. When graphing a consumption function, the vertical intercept is given by the letter ________ , and the slope of the function is given by the letter ________.
I need to create a code in C++ that first has a menu animation of game...
I need to create a code in C++ that first has a menu animation of game Pacman, a score label in the map, and a bar that have the lives of pacman in the map.
I have the function call showGroceries(list); I need to create a new prototype above int main...
I have the function call showGroceries(list); I need to create a new prototype above int main that provides: return type function name parameter(s) type(s) Then copy-and-paste the prototype below int main. Give the parameter a name and then implement the function so that it takes the vector of strings and displays it so that a vector with the values: {"milk", "bread", "corn"} would display as: Grocery list 1. milk 2. bread 3. corn However, if there is nothing in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT