C/C++ How to Define the Missing Function in a Dog License Number Program?

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
AI Thread Summary
The discussion focuses on defining the missing function `CreateLicenseNum` in the `DogLicense` class. The goal is to compute the license number using the formula `(100000 * customID) + licenseYear`. Initially, there is confusion about variable declaration and scope, with a user mistakenly trying to declare a new variable instead of using the existing class member `licenseNum`. After clarification, the correct implementation is provided, where `licenseNum` is assigned the calculated value based on the provided `customID` and the previously set `licenseYear`. The final code snippet successfully resolves the issue, demonstrating the proper use of class member variables.
ineedhelpnow
Messages
649
Reaction score
0
i have another question i need help with :o
Define the missing function. licenseNumber is created as: (100000 * customID) + licenseYear. Sample output:

Dog license: 77702014
Sample program:
Code:
#include <iostream>
using namespace std;

class DogLicense{
   public:
      void   SetYear(int yearRegistered);
      void   CreateLicenseNum(int customID);
      int    GetLicenseNum() const;
   private:
      int    licenseYear;
      int    licenseNum;
};

void DogLicense::SetYear(int yearRegistered) {
   licenseYear = yearRegistered;
   return;
}

// FIXME: Write CreateLicenseNum()
<STUDENT CODE> 

int DogLicense::GetLicenseNum() const {
   return licenseNum;
}

int main() {
   DogLicense dog1;
  
   dog1.SetYear(2014);
   dog1.CreateLicenseNum(777);
   cout << "Dog license: " << dog1.GetLicenseNum() << endl;

   return 0;
}

Below, do not type an entire program. Only type the portion indicated by the above instructions (and if a sample program is shown above, only type the <STUDENT CODE> portion.)

normally if i look at something long enough I am able to figure it out but this one just isn't clicking. how do i do it? :confused:
 
Technology news on Phys.org
ineedhelpnow said:
normally if i look at something long enough I am able to figure it out but this one just isn't clicking. how do i do it? :confused:

Hey! (Smile)

For CreateLicenseNum() we need to the same thing as is already there for SetYear().
But instead of simply copying the parameter, we need to calculate with the given formula what to store. (Thinking)
 
:confused: so something like this?
Code:
void DogLicense::CreateLicenseNum(int licenseNumber) {
   licenseNumber =  (100000 * customID) + licenseYear;
   return;
}
 
ineedhelpnow said:
:confused: so something like this?
Code:
void DogLicense::CreateLicenseNum(int licenseNumber) {
   licenseNumber =  (100000 * customID) + licenseYear;
   return;
}

Yep. Something like that. (Smile)
It's just that the parameter should be customID instead.
Just like it says in the class definition.
Otherwise it won't compile. (Worried)
 
you mean like this
Code:
void DogLicense::CreateLicenseNum(int customID) {
  int licenseNumber =  (100000 * customID) + licenseYear;
   return;
}
 
ineedhelpnow said:
you mean like this
Code:
void DogLicense::CreateLicenseNum(int customID) {
  int licenseNumber =  (100000 * customID) + licenseYear;
   return;
}

Yep. (Smile)

Except that you've introduced an 'int' that shouldn't be there.
 
but if i don't put int i get the message that 'licenseNumber’ was not declared in this scope. don't i have to use int to introduce the variable?
 
ineedhelpnow said:
but if i don't put int i get the message that 'licenseNumber’ was not declared in this scope. don't i have to use int to introduce the variable?

The class contains a data member named [m]licenseNum[/m], which is the one that needs to be initialized.
Perhaps you can change 'licenseNumber' into 'licenseNum'? (Wondering)
 
it finally worked!
Code:
void DogLicense::CreateLicenseNum(int customID) {
  licenseNum =  (100000 * customID) + licenseYear;
   return;
}

blowing-kiss-thank-you-smiley-emoticon.gif
 
Back
Top