Home > Blockchain >  How do I make my setfunction run through my if loop?
How do I make my setfunction run through my if loop?

Time:05-08

I am working on a program and I need to make a class about cars. I need to add setFunctions that only allow a car year manufacture of 1930 -2030 as input, anything else will be automatically set as 2012. My code looks like this:

// code will be following
void setYear(int yr) 
{
    if (yr > 2030 || yr < 1930){
        yr = 2012;
    }
    else {
        year = yr;
    }
}
// end of segment.

I also have code for setSpeed which cannot be less than 0 as follows

// code following
void setSpeed(int spd) 
{
    if (spd < 0){
        spd = 0;
    }
    else {
        speed = spd;
    }
}

The problem is, when I try change my variables for my objects, the set functions don't work as desired. I believe the problem is with my constructor,

// constructor with default parameters
car(int yr = 2012, string mk = "unknown", int spd = 0)
{
    year = yr;
    make  = mk;
    speed = 0;
}

My code basically does the project as required, it's just not exactly what is asked.

This is what my code is supposed to do:

Create a program that illustrates C classes. Your program should do the following:

Write a class named Car that has the following member variables:

  • year (An int that holds the car’s model year. The year must be between 1900-2030 (inclusive)).

  • make (A string that holds the make of the car.)

  • speed (An int that holds the car’s current speed. The speed must be 0 or greater. ) In addition, the class should have the following member functions:

  • Constructor - The constructor should accept the car’s year and make as arguments and assign these values to the object’s year and make member variables. The constructor should initialize the speed member variable to 0. Notice that this constructor will have 2 parameters: one for the year and one for the make of the Car.

When you define a Car object, you will need to send the two arguments the constructor is expecting:

Car myCar(2002, "Jetta");

In the constructor you need to assign a value to each of the member variables:

  • Since year has specific values that are allowed, call the setYear function with the value sent.
  • Set make to the value passed.
  • Set speed to zero. You can do this with a simple assignment statement or you can call the setSpeed function.

Get functions:
Appropriate access functions should be created to allow values to be retrieved from an object’s year, make, and speed member variables. There will be three get functions, one for each member variable. Each of these functions return one thing and have NO parameters.

Set functions:
There will be three set functions, one for each member variable. Each of these functions receive one thing and return nothing.

  • The speed cannot be less than zero. Have your set function protect the speed so it is never negative. If a negative value is sent, simply set the speed to zero.
  • The year must be between 1900 and 2030 (inclusive). If a year outside the range is sent, set the year to 2012. No validation of the make is required.

Accelerate:
The accelerate function should add 5 to the speed member variable each time it is called. This member function returns nothing and has no parameter. It simply does its job: each time it is called, it adds 5 to speed.

Brake:
The brake function should subtract 5 from the speed member variable each time it is called. If the speed is already zero, no change is to be made to speed. This member function returns nothing and has no parameter. It simply does its job: each time it is called, it subtracts 5 from speed unless speed is already at zero. Then it makes no change.

Notes:
Demonstrate the class in a program that creates a Car object, and then calls the accelerate function five times. After each call to the accelerate function, get the current speed of the car and display it.

Then, call the brake function seven times. After each call to the brake function, get the current speed of the car and display it. Each time you display the speed include the year and make of the vehicle in the output label.

Now, set the year, make, and speed to 2009, Jeep, and 75 respectively. Once again call the accelerate function five times. After each call to the accelerate function, get the current speed of the car and display it. Then, call the brake function seven times. After each call to the brake function, get the current speed of the car and display it. Each time you display the speed include the year and make of the vehicle in the output label.

Finally, set the year, make, and speed to 1827, Jetta, and -30 respectively. Using the get functions, print out the year, make, and speed.

This is my code:

      #include <iostream>
      #include <cstdlib>
      #include <string>
      using namespace std;

      // car class declaration
      class car
      {
      private:
int year;
string make;
int speed;

public:
//creation of set functions 

void setYear(int yr) 
{
if (yr > 2030 || yr < 1930){
    yr = 2012;
}
else {

    year = yr;
}
}

void setMake(string mk) {
    make = mk;
}

void setSpeed(int spd) 
{
if (spd < 0){
    spd = 0;
}
else {
    speed = spd;
}
}
// constructor with default parameters
 car(int yr = 2012, string mk = "unknown", int spd = 0)
 {
     year = yr;
         make  = mk;
         speed = 0;
 }

 // Accessors ( The get functions)

 int getYear()
 { return year; }

 string getMake()
 { return make; }

 int getSpeed()
 { return speed; }

 // Mutators
 void accelerate ()
 {  speed  = 5;  }

 void brake()
 {

    if ( speed >= 5)
        speed -=5;
    else
        speed = 0;
}

};

int main()
{
// creation and display of first object of car class
car hotRod (2020, "Ford");
hotRod.setSpeed(40);
 
 cout << "I've spotted a " << hotRod.getYear()<<" "
      << hotRod.getMake() << " car, going 40 kmph!\n\n";
      
// accelerator function to run 5 times
cout << "It is accelerating \n\n";
for (int run =0; run < 5; run  )
{

 hotRod.accelerate();
 cout << hotRod.getYear()<<" " << hotRod.getMake()
 << " current speed: " << hotRod.getSpeed() << "kmph.\n";
}
 cout << endl;
 
 // breaking function to run 7 times
 cout << "Now it's 'breaking... " << endl;
 for(int brk = 0; brk < 7; brk  )
 {
     hotRod.brake();
     cout << hotRod.getYear()<<" "<< hotRod.getMake()
     << "  Current speed: " << hotRod.getSpeed() << "kmph.\n";
}
// intermediary input to break the program apart
// this allows for better display of program
string x;
cout << endl << endl << endl << "Would you like to know what car I saw next? ";
cin >> x;

// creation and display of second object in car class        
car hotRod2 (2009, "Jeep");
hotRod2.setSpeed(75);

     cout << endl << endl <<endl
     << "I've spotted a " << hotRod2.getYear()<<" "
      << hotRod2.getMake() << " car, going 75kmph!\n\n";
// accelerator function to run 5 times
cout << "It is accelerating \n\n";
for (int run =0; run < 5; run  )
{

 hotRod2.accelerate();
 cout << hotRod2.getYear()<<" " << hotRod2.getMake()
 << " current speed: " << hotRod2.getSpeed() << "kmph.\n";
}
 cout << endl;
 
 // Breaking function to run 7 times
 cout << "Now it's 'breaking... " << endl;
 for(int brk = 0; brk < 7; brk  )
 {
     hotRod2.brake();
     cout << hotRod2.getYear()<<" "<< hotRod2.getMake()
     << "  Current speed: " << hotRod2.getSpeed() << "kmph.\n";
     
     
 }
     string y;
cout << endl << endl << endl << "Would you like to know what car I saw next? ";
cin >> y;

// creation of 3rd object of car class   
car hotRod3;
hotRod3.setMake("Jetta");
hotRod3.setYear(1827);
hotRod3.setSpeed(-30);

     cout << endl << endl <<endl
     << "I've spotted a "
     <<hotRod3.getYear()
     <<" "
     << hotRod3.getMake() << " car, going "
     << hotRod3.getSpeed()
     << "kmph! \n\n";

 // end of program
 return 0;
 }

CodePudding user response:

setYear() and setSpeed() are both making the same mistake. They are not assigning any values to the class data members if the input values are out of range.

Try this instead:

void setYear(int yr) 
{
    if (yr > 2030 || yr < 1930){
        yr = 2012;
    }
    year = yr;
}

void setSpeed(int spd) 
{
    if (spd < 0){
        spd = 0;
    }
    speed = spd;
}

Also, your constructor should be calling the set... functions, otherwise the creator can pass in unacceptable values that you are taking as-is:

car(int yr = 2012, string mk = "unknown", int spd = 0)
{
    setYear(yr);
    setMake(mk);
    setSpeed(spd);
}

In fact, the instructions specifically tell you to do exactly this:

In the constructor you need to assign a value to each of the member variables:

  • Since year has specific values that are allowed, call the setYear function with the value sent.
  • Set make to the value passed.
  • Set speed to zero. You can do this with a simple assignment statement or you can call the setSpeed function.
  • Related