Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6247605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:54:28+00:00 2026-05-24T12:54:28+00:00

When I run the following code in Microsoft visual studio it outputs junk values

  • 0

When I run the following code in Microsoft visual studio it outputs junk values (memory addresses?) but when it is run in g++ it outputs what I intend it to (with a few changes like changing srand). How do I fix it to work in visual studio? I only have a few months coding experience and this issue has been bugging me for awhile now.

class Vehicle
{
    protected:
        int * vin;
        double * gasMileage;

    public:
        Vehicle();
        Vehicle(int v, double g);

        virtual void display(){cout<<"vin: "<<*vin<<endl<<"gasMileage: " <<*gasMileage<<endl;}
        virtual double calcGasUsed(int milesDriven){return *gasMileage * milesDriven;}

        int getVin(){return *vin;}
        double getGasMileage(){return *gasMileage;}

        void changeVin(int newvin) {vin=&newvin;}
        void changeGasMileage(double newGasMileage){gasMileage=&newGasMileage;}
        void drive();
};

class Suv:public Vehicle
{
    protected:
        bool *fourWDStatus;
        double * fourWDGasMileage;

    public:
        Suv();
        Suv(int v, double g,bool status, double fwdg);

        void display();
        double calcGasUsed(Suv&, int milesDriven);
        bool getFourWDStatus(){return *fourWDStatus;}
        double getfourWDGasMileage(){return *fourWDGasMileage;}

        void changeFourWDStatus(bool status) {fourWDStatus=&status;}
        void changeFourWDGasMileage(double newGasMileage){fourWDGasMileage=&newGasMileage;}

};
Vehicle::Vehicle()
{
    this->vin=0000;
    this->gasMileage=00;
}
Vehicle::Vehicle(int v, double g)
{
    this->vin=&v;
    this->gasMileage=&g;
}
Suv::Suv(int v, double g,bool status, double fwdg)
{
    this->vin=&v;
    this->gasMileage=&g;
    this->fourWDStatus=&status;
    this->fourWDGasMileage=&fwdg;
}
Suv::Suv()
{
    this->vin=0000;
    this->gasMileage=00;
    this->fourWDStatus=false;
    this->fourWDGasMileage=00;
}
void Suv::display()
{
    Vehicle::display();
    cout<<"fourWDStatus: "<<*fourWDStatus<<endl<<"fourWDGasMileage: "<<*fourWDGasMileage<<endl;
}
void Vehicle::drive()
{
    int r=rand()%10000;
    cout<<calcGasUsed(r)<<endl;

}
double Suv::calcGasUsed(Suv&, int milesDriven)
{
    double x;
    if (*fourWDStatus== true)
    {
        x= ((*fourWDGasMileage) * (milesDriven));
        return x;
    }
    else
    {
        x=((*gasMileage) * (milesDriven));
        return x;
    }
}

void main()
{
    cout << "test";
    srand(NULL);
    Suv A(300,12.2,false,16.6);
    Suv B(200,15.5,false,20.1);
    B.changeFourWDStatus(true);
    Vehicle C(111,20.5);
    C.drive();
    C.display();
    B.display();
    Vehicle arrOfCars[]={A,B,C};
    A.drive();
    B.drive();
    C.drive();
    system("pause");

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:54:29+00:00Added an answer on May 24, 2026 at 12:54 pm

    The members Vehicle::vin, Vehicle::gasMileage as well as Suv::fourWDStatus and Suv::fourWDGasMileage are not correctly initialized pointers.

    From the code you posted it seems you would be better off by changing them from pointers to regular variables (and adjusting the methods appropriately).

    EDIT: If using pointers is a part of assignment, you have to initialize them correctly. This means, in your case, allocating a memory in constructors. It could be this way:

    Vehicle::Vehicle()
    {
        this->vin = new int(0);
        this->gasMileage = new double(0.0);
    }
    
    Vehicle::Vehicle(int v, double g)
    {
        this->vin = new int(v);
        this->gasMileage = new double(g);
    }
    
    Suv::Suv(int v, double g,bool status, double fwdg) : Vehicle(v, g)
    {        
        this->fourWDStatus = new bool(status);
        this->fourWDGasMileage = new double(fwdg);
    }
    
    Suv::Suv() : Vehicle(0, 0.0)
    {
        this->fourWDStatus = new bool(false);
        this->fourWDGasMileage = new double(0.0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why whenever I compile and run the following code in Visual Studio 2008: double
When i run the following command in the visual studio command prompt: D:\Documents\DEV\SARPilot\Docs\eoschema\schema\OrderSchema>svcutil /t:code
We are getting the following warning from Code Analysis in Visual Studio 2010 and
The following code (packaged in a 'Console Application' Visual Studio project): using System; using
When I run the following code to test copying a directory, I get a
I want to run the following code: ajaxUpdate(10); With a delay of 1 second
When I run the following code: private void button1_Click(object sender, EventArgs e) { Bitmap
When I run the following code, I get the exception below: ''# NOTE: ExcelApp
When I run the following code, it only seems to be downloading the first
When I try to run the following code (from the REPL) in Clojure: (dotimes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.